How to install PHP GD in Ubuntu

How to install PHP GD in Ubuntu

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to install PHP GD in Ubuntu

Attempting to enable the GD library on an older Ubuntu release, specifically 16.04 LTS as indicated by your PHP version string 7.0.15-0ubuntu0.16.04.4, often leads to dependency hell if the system's package manager is out of sync or missing critical build tools. The error logs you are seeing—E: Unable to locate package libgd followed by a cascade of unmet dependencies—are classic symptoms of trying to install a development library without the foundational C libraries required to compile it.

The immediate issue in your logs stems from libgd-dev. This package does not exist as a standalone binary; instead, it relies heavily on libgd3, which is a dependency that your system currently lacks or cannot resolve due to version mismatches. Furthermore, GD requires image processing libraries like libpng, libjpeg, libfreetype6, and libxpm. The error message explicitly lists these as missing. When you see Depends: libgd3 (= 2.1.1-4ubuntu0.16.04.6) but it is not going to be installed, it means the package manager cannot fulfill the strict version constraint because the base library isn't present in your repositories or has been removed by a previous cleanup.

Before attempting to force the installation, you must ensure your package lists are updated and that you have the necessary build-essential tools installed. These tools provide the compilers and linker flags required for apt to resolve dependencies correctly. Run the following sequence to prepare your environment:

sudo apt-get update
sudo apt-get install -f
sudo apt-get install build-essential libpng-dev libjpeg-dev libtiff-dev \
    libfreetype6-dev libxpm-dev libx11-dev libfontconfig-dev

The command apt-get -f install (or -f install) is crucial here. It tells the package manager to attempt to correct broken dependencies without installing new packages immediately, effectively acting as a "repair" step. Once these libraries are present, try installing the GD library again:

sudo apt-get install libgd-dev php-gd

In PHP 7, the extension is typically named php-gd. If you previously commented out the line ;extension=gd in your php.ini, you must uncomment it. The location of this file varies based on your Apache or Nginx setup:

  • Apache: Usually /etc/php/7.0/apache2/php.ini
  • CLI: Usually /etc/php/7.0/cli/php.ini
  • Nginx (via PHP-FPM): Usually /etc/php/7.0/fpm/php.ini

Search for the line and remove the leading semicolon:

extension=gd.so

After editing the configuration, you must restart your web server or PHP-FPM service for changes to take effect:

sudo systemctl restart apache2
# OR
sudo systemctl restart php7.0-fpm

Once restarted, verify that the GD functions are available by running php -m in your terminal. You should see gd listed in the output. If you still encounter the Call to undefined function imagecreatefromstring() error, ensure that the PHP process is actually using the updated configuration file and not a cached one from a previous installation.

There is a significant trade-off to consider with Ubuntu 16.04 and PHP 7.0. This combination is End of Life (EOL). The official repositories no longer receive security updates or new package versions. You might find that the latest GD library required by your application logic simply isn't available in the default main repository for this specific OS version. In production environments, relying on EOL software stacks introduces security risks and limits access to modern image processing features. If you are building a new application, migrating to a supported Ubuntu LTS (like 20.04 or 22.04) with PHP 8.x is the recommended path forward. This ensures access to up-to-date libraries and security patches without fighting dependency resolution errors.

For developers working on legacy systems where migration isn't an option immediately, sticking to the strict dependencies listed in the error logs is the only viable route. Do not ignore the missing libpng or libfreetype packages; attempting to bypass them will result in runtime failures when GD tries to process images. If you require a specific version of GD that isn't available in the standard Ubuntu repos for 16.04, compiling from source becomes necessary, but this requires a deep understanding of system headers and is generally discouraged unless absolutely required.

If you are using Laravel or another modern framework on this legacy stack, remember that many packages assume newer PHP versions. While laravelcompany.com offers resources on optimizing performance, their guides often assume a modern environment. Adapting these strategies to an EOL Ubuntu system requires careful auditing of every dependency to ensure compatibility with the older GD implementation available in the 16.04 repositories.

Our network