When trying to install the GD library for PHP image manipulation on Ubuntu 14.04, you're encountering a version mismatch between what the system expects and what's actually installed. The key error shows:
php5-gd : Depends: php5-common (= 5.5.9+dfsg-1ubuntu4)
but 5.5.10+dfsg-1+deb.sury.org~saucy+1 is to be installed
E: Unable to correct problems, you have held broken packages
This occurs because you've added third-party PHP repositories (likely Ondřej Surý's PPA) which provide newer PHP versions than Ubuntu's official repositories.
First, let's check your exact PHP version:
php -v
And verify installed PHP packages:
dpkg -l | grep php5
The cleanest approach is to install the GD library from the same repository as your PHP installation:
sudo apt-get install php5-gd=5.5.10+dfsg-1+deb.sury.org~saucy+1
If the exact version isn't available, try:
sudo apt-get install php5-gd -t saucy
Force the package manager to use available versions:
sudo apt-get -o Dpkg::Options::="--force-overwrite" install php5-gd
If package solutions fail, compile from source:
sudo apt-get build-dep php5
sudo apt-get source php5
cd php5-5.5.10/ext/gd/
phpize
./configure
make
sudo make install
After successful installation, verify with:
<?php
phpinfo();
?>
Search for "GD" in the output. To test functionality:
<?php
$im = imagecreate(100, 100);
$background = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 5, 20, 40, "GD Works!", $textcolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
If you're setting up a new server, consider:
sudo apt-get purge php5*
sudo apt-get install php5 php5-gd
Or upgrade to PHP 7+ where GD is included by default in most installations.
- Clear package cache:
sudo apt-get clean
- Fix broken packages:
sudo apt-get install -f
- Check repository priorities in
/etc/apt/preferences
The error message indicates a version mismatch between the required php5-common package (5.5.9) and the installed version (5.5.10). This commonly occurs when mixing packages from different repositories. Your dpkg output shows you're using packages from deb.sury.org while the system tries to install from Ubuntu's default repositories.
First, let's clean up any partially installed packages:
sudo apt-get autoremove
sudo apt-get -f install
Then install the correct version from the right repository:
sudo apt-get install php5-gd=5.5.10+dfsg-1+deb.sury.org~saucy+1
If the specific version installation fails, try these approaches:
Method 1: Reconfigure your repositories
sudo add-apt-repository -y ppa:ondrej/php
sudo apt-get update
sudo apt-get install php5-gd
Method 2: Compile from source (advanced)
sudo apt-get build-dep php5
sudo apt-get install php5-dev
pecl install gd
Create a test script to confirm GD is working:
<?php
if (extension_loaded('gd')) {
echo "GD is installed";
$info = gd_info();
print_r($info);
} else {
echo "GD not found";
}
?>
Once installed, you can use functions like:
// Create image from JPEG
$im = imagecreatefromjpeg("example.jpg");
// Add text to image
$text_color = imagecolorallocate($im, 255, 255, 255);
imagestring($im, 5, 10, 10, "GD Test", $text_color);
// Output image
header('Content-Type: image/jpeg');
imagejpeg($im);
- Check Apache/Nginx error logs for specific GD-related errors
- Verify PHP configuration with
phpinfo()
- Ensure all PHP packages are from the same repository
- Restart your web server after installation