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...

6 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...
- PHP GD Library: What It Is and Why Itâs Important
- Prerequisites
- How to Install PHP GD Extension on Ubuntu
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:
- 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:
- Operating System: A server running Ubuntu 18.04, 20.04, or a later version, with sudo privileges.
- Web Server: Apache or Nginx installed and configured on your system.
- 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.
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 format
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
Save the file and access it in your browser by navigating to:
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:
- How to Install PHP JSON Extension on Ubuntu / CentOS: 7 Easy Step
- How to Install PHP 8.4 on RHEL 9: Easy Steps
- How to Install PHP 8.4 on RHEL 9: Easy Steps
- How to Install PHP Extensions for WordPress: 3 Easy Steps
Learn PHP
- PHP for Beginners
- PHP for Beginners â Become a PHP Master â CMS Project
- Become a WordPress Developer: Unlocking Power With Code
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!
FAQs: How to Install PHP GD Extension on Ubuntu
The PHP GD library is a graphics library that allows developers to create, manipulate, and output images in various formats such as PNG, GIF, and JPEG. Itâs widely used for tasks like image resizing, cropping, adding watermarks, and creating dynamic image content.
No, the PHP GD library is not installed by default on Ubuntu. It needs to be manually installed using the command sudo apt install php-gd.
Yes, you can install the PHP GD library for a specific PHP version by appending the version number to the command. For example:
- For PHP 7.4:
sudo apt install php7.4-gd - For PHP 8.2:
sudo apt install php8.2-gd
You can create a PHP test file with GD functions and access it via your web browser. Alternatively, you can use the command php -i | grep GD to check if the GD library is listed in your PHP configuration.
Yes, restarting your web server (Apache or Nginx) is necessary to apply changes after installing the PHP GD library.
Yes, you can uninstall the PHP GD library using the command sudo apt remove php-gd. For specific versions, include the version number in the command, such as sudo apt remove php7.4-gd.
The PHP GD library supports multiple image formats, including PNG, GIF, JPEG, and WebP (depending on your configuration).
You need a server running Ubuntu (18.04, 20.04, or later) with sudo privileges, a web server (Apache or Nginx), and PHP installed and configured on your system.
The PHP GD library is compatible with most PHP versions. Ensure you install the version that matches your PHP installation.
The PHP GD library is not designed for creating animations. Itâs primarily used for creating and manipulating static images. For animations, consider using other libraries or tools like ImageMagick.
Website Vidya Team
Technology educator and researcher at Website Vidya.
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides











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