PHP GD library is a graphics library used to create dynamic images in PHP. By default, GD library is not installed in Ubuntu. If you want to use it, you need to install it first. In this article, we will guide you on how to install PHP GD on Ubuntu. The commands we will used to install PHP GD is compatible with most of the Ubuntu versions. All the commands listed here, were tested by us.
PHP GD Library [What is, Why it is Important]
Before dive into the installation, let’s understand what PHP GD library is and why it is important. The PHP GD library is a graphics library and it allows developers to create and manipulate images dynamically in PHP. It provides functions to create images from scratch, modify existing images, and output them in various formats like PNG, GIF, and JPEG. PHP GD is also used in many image resizing tools.
Prerequisites
Before, start with the installation process, make sure that you have the following prerequisites:
- A Ubuntu 18.04 or 20.04 server with sudo access
- Apache or Nginx web server installed
- PHP installed on the server
Step-by-Step Guide to Install PHP GD on Ubuntu
Follow these steps to install PHP GD library on Ubuntu
Step 1: Update the Package List
Before installing any new package, it is always recommended to update the package list. Run the following command to update the package list
sudo apt update
Step 2: Install GD Library
To install the PHP GD on Ubuntu, run the following command:
sudo apt install php-gd
This command will install the GD library and all its dependencies.
If you are installing for particular version such as PHP 7.4 or PHP 8.2, use the command by adding version number to the tail of PHP, for e.g: php7.4 or php8.4
This command will install the PHP GD library on Ubuntu and all its dependencies.
Step 3: Verify Installation
To verify installation, you can check PHP version or alternatively you can a create a sample PHP file to verify PHP GD extension installation. Let’s create a PHP file and we name it php_extension_check.php, in the website document root directory with below code snippet.
<?php
// Create a 100x100 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 navigate to http://websitename.com/test-gd.php in your web browser. If everything is set up correctly, you should see a 100×100 red square.
Step 4: Check PHP GD Version
To check the version of PHP GD installed on your system, run the following command:
php -i | grep GD
This will display information about the GD library, including the version number.
Step 5: Restart the Web Server
After installing the PHP GD library, you need to restart your web server to ensure that it make changes. You can do this by running the following command:
sudo service apache2 restart
or
sudo service nginx restart
depending on which web server you are using.
Uninstall PHP GD library from Ubuntu
If you ever need to uninstall PHP GD from Ubuntu system, you can do so by running the following command:
sudo apt remove php-gd
This command will remove the library and all its dependencies.
Conclusion
In this article, you have learnt how to install PHP GD on Ubuntu. The installation is very simple process that can be completed in a few easy steps. Once installed, you can use this library to create and manipulate images in your PHP applications. We hope that this article has been helpful to you.