Website Vidya
Linux & ServersStep-by-Step Guide

How to Install PHP GD Extension on Ubuntu: 5 Easy Steps

The PHP GD library is a robust and versatile tool that provides developers with the ability to create and manipulate dynamic images using PHP. By default, this library is not included in a standard Ubuntu installation, so it needs to be man...

RR
Rajesh RayLead Systems Educator
Published:
5 min read
How to Install PHP GD Extension on Ubuntu: 5 Easy Steps
AI Executive SummaryCORE CIS AI

5 min read

The PHP GD library is a robust and versatile tool that provides developers with the ability to create and manipulate dynamic images using PHP. By default, this library is not included in a standard Ubuntu installation, so it needs to be man...

Key Highlights
  • PHP GD Library: What It Is and Why It’s Important
  • Prerequisites
  • How to Install PHP GD Extension on Ubuntu
Grounded in full engineering runbookCORE CIS Ai Engine

The PHP GD library is a robust and versatile tool that provides developers with the ability to create and manipulate dynamic images using PHP. By default, this library is not included in a standard Ubuntu installation, so it needs to be manually added to your system.

In this comprehensive guide, we will take you through the detailed steps on how to install PHP GD extension on Ubuntu. These instructions are tested and compatible with most Ubuntu versions, ensuring a seamless setup process.


PHP GD Library: What It Is and Why It’s Important

The PHP GD library is a graphics library allowing developers to create, manipulate, and output images in various formats such as PNG, GIF, and JPEG.

It supports a wide range of functions for image processing, making it an essential tool for developers working with dynamic image content. Here are some key features and use cases of the GD library:

Advertisement
  • Creating Dynamic Image Content: Generate images on the fly for applications like captchas or user profile placeholders.
  • Resizing and Cropping Images: Automate the process of image resizing and cropping, which is useful for galleries and thumbnails.
  • Adding Watermarks or Text Overlays: Protect your images or add branding through text or graphic watermarks.
  • Support for Multiple Formats: The GD library handles formats like PNG, GIF, JPEG, and even WebP, depending on your configuration.

Given its broad capabilities, the PHP GD library is vital for web developers who frequently work on projects involving images.


Prerequisites

Before proceeding with the installation process, ensure that you meet the following prerequisites:

  1. Operating System: A server running Ubuntu 18.04, 20.04, or a later version, with sudo privileges.
  2. Web Server: Apache or Nginx installed and configured on your system.
  3. PHP Installation: PHP installed and properly configured on your server.

Make sure these requirements are fulfilled to avoid potential issues during installation.


How to Install PHP GD Extension on Ubuntu

Step 1: Update the Package List

As a best practice, update your package list to ensure you have the latest information about available packages. Use the following command:

sudo apt update

This command updates the package manager’s database, ensuring the installation process is smooth and free of outdated dependencies.

Advertisement

Step 2: Install the GD Library

To install the PHP GD library, use the following command:

sudo apt install php-gd

If you are working with a specific version of PHP, include the version number in the command. For instance:

  • For PHP 7.4:sudo apt install php7.4-gd
  • For PHP 8.2:sudo apt install php8.2-gd

This command installs the GD library along with its necessary dependencies, preparing your system for image-processing tasks.

Step 3: Verify the Installation

To confirm that the PHP GD extension has been successfully installed, you can create a test PHP file. Place this file in your web server’s document root directory. For example, create a file named test-gd.php with the following content:

<?php// Create a 1200x630 image$image = imagecreatetruecolor(100, 100);// Set the background color to red$red = imagecolorallocate($image, 255, 0, 0);imagefill($image, 0, 0, $red);// Output the image in PNG formatheader('Content-Type: image/png');imagepng($image);imagedestroy($image);?>

Save the file and access it in your browser by navigating to:

Advertisement
http://your-server-ip/test-gd.php

If the GD library is installed correctly, a 100x100 red square will appear on your browser screen, confirming successful installation.

Step 4: Check the Installed GD Version

You can find out the version of the GD library installed on your system by running the following command:

php -i | grep GD

This command provides detailed information about the GD library, including its version and supported formats.

Step 5: Restart the Web Server

After completing the installation, it is crucial to restart your web server to apply the changes. Use the appropriate command depending on your web server:

For Apache:

sudo service apache2 restart

For Nginx:

sudo service nginx restart

Restarting ensures that the PHP GD library is properly loaded and ready for use.


Uninstalling the PHP GD Library

If you no longer require the PHP GD library, you can remove it from your system using the following command:

sudo apt remove php-gd

For specific PHP versions, include the version number in the command. For example:

sudo apt remove php7.4-gd

This will remove the GD library and any dependencies that were installed with it.

You may also like:

Learn PHP


Conclusion

In this detailed guide, we have explored how to install PHP GD extension on Ubuntu. From understanding the importance of the GD library to verifying its installation, this guide has provided a step-by-step approach to help you integrate image processing capabilities into your PHP projects. The installation process is straightforward and, once completed, opens up a world of possibilities for creating and manipulating images dynamically.

Whether you are building an image-heavy application or simply need to add a watermark to your photos, the PHP GD library is a reliable and efficient solution. We hope this guide has been helpful.

Happy coding and image processing!


RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Linux & Servers•

How to Install PDO Extension in PHP

PHP Data Objects (PDO) is one of the most important extensions in PHP for working with databases in a secure, consistent, and modern way. If you are building dynamic websites, APIs, CMS platforms, or enterprise applications, PDO is not opti...

RR
Rajesh Rai
Read More
Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout