How to Install PHP-GD in Ubuntu

Subodh Poudel Feb 02, 2024
How to Install PHP-GD in Ubuntu

Graphics Draw (GD) is an open-source library used to create and manipulate images. The library has support for many programming languages, including PHP.

The library is called php-gd for PHP. This tutorial will walk you through installing PHP GD in Ubuntu(LAMP stack).

Install PHP-GD in Ubuntu

The following steps of installation are for the Ubuntu distribution, and it also works for the Debian system. We will use the apt command to install the package.

First, we must fetch the system packages from the repository using the apt-get update command.

sudo apt-get update

The command will update the system package list. Now, the newest version of the packages can be installed.

Use the following command to upgrade the packages.

sudo apt-get upgrade

Now, the system is up-to-date. Next, use the apt show command to get the information about the php-gd package.

apt show php8.1-gd

The PHP version of the system used in this tutorial is PHP 8.1. Therefore, we checked whether the php8.-gd package is available.

Below is the output of the command.

apt-show

Note
This step is optional, but it’s still good to know about the package we are installing.

The final step is to install the php8.1-gd package with the command below.

sudo apt-get install php8.1-gd

The command shows some additional packages that need to be installed and asks your permission. You can grant the permission by pressing y.

After that, php-gd will be installed in your Ubuntu system.

Now, we need to verify the installation of php-gd in the system. To ensure its installation, navigate the following path and create the index.php file.

cd /var/www/html
touch index.php

Next, open the index.php file, write the following code, and save the file.

<?php
phpinfo();
?>

After that, start the web server with one of the following commands.

sudo systemctl start apache2
sudo /etc/init.d/apache2 start

Next, serve the file index.php to the server. The following output is seen on the webpage.

php info

Now press Ctrl+F in the webpage and type gd in the text field. You can see a table that displays the information of the gd package.

php gd

Thus, it verifies that php-gd has been installed in your Ubuntu system.

Subodh Poudel avatar Subodh Poudel avatar

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

LinkedIn