Recompiling PHP5 With Bundled Support For GD On Ubuntu

I’ve been installing the LAMP stack on an Ubuntu based server for a client. They rely on the version of LibGD that’s bundled with PHP5. This version includes certain functions including imagerotate() and others.

It seems that the people who build Ubuntu (and Debian) distribution do not want to compile PHP5 with the version of GD that comes bundled with PHP5. As far as I can tell the guys at Ubuntu (and Debian) consider this to be a fork of the original GD library and think that this could be a security risk. Their stance on it is that if PHP want to use this version then they should contribute the changes that have been made to the original GD library back to the core project. This seems fair enough, but unfortunately I can’t immediately change the direction of an open source project this big – or probably any project, come to think about it!

After a bit of rummaging around on the Internet I found nothing to do with actually compiling in this support, so I tried to work out how to do this… In theory! I found a link on Ubuntu forums about including the GMP library I roughly followed this through.

To install the required packages and download the source code for PHP5:

Install build tools, debian helpers and fakeroot:

apt-get install build-essential debhelper fakeroot

Source code should reside in /usr/src:

cd /usr/src

Download PHP source:

apt-get source php5

Install all packages required to build PHP5:

apt-get build-dep php5
cd php5-5.2.3

Now a package is compiled is set within the files contained in the debian directory of a package. Rules for configuring the compile process can be found in debian/rules. In this file there is a line that reads --with-gd=shared,/usr --enable-gd-native-ttf \. This links to the Ubuntu distributed version of LibGD as a shared library. It is part of the autoconf script that customises the compilation of PHP. I replaced this line with --with-gd=shared --enable-gd-native-ttf \. This causes the compilation process to use the bundled version of GD and make a shared library.

Once the package has been reconfigured it can be compiled and installed by:

Build the php5-* packages:

dpkg-buildpackage -rfakeroot
cd ..

Install the new php5-gd package

dpkg -i php5-gd_5.2.3-1ubuntu6.3_i386.deb

A quick

/etc/init.d/apache2 restart

and hey presto, you should be using the bundled version of LibGD for PHP. Hurrah!

Share this page:

Suggested articles

17 Comment(s)

Add comment


By: Leszek

In case of dependency problems, apt-get -f install should sort things out.

By: Anonymous


this tutorial works fine. But how can i hold the php5-gd library? Every time when i run apt-get update / apt-get upgrade my system wants to override the new compiled one. Any ideas?

Thank you

By: Gordon

Yes, I am experiencing the same issue. I am using Turnkey Linux, and the default cron jobs run apt and revert to the original php-gd with separate ubuntu compiled library.

 I have been looking through the cron files to see what I can comment out! Any advice or direction much appreciated.



By: Andriy

Thank you from Ukraine!

By: Anonymous

Edit /etc/apt/apt.conf.d/50unattended-upgrades to modify the Unattended-Upgrade::Package-Blacklist section to include the packages you do not want automatically upgraded.


 // List of packages to not update
Unattended-Upgrade::Package-Blacklist {

By: Paul Stenning

I have just completed this on a fully up to date Ubuntu 8.04 LTS server.  Excellent post, works really well! 

Two minor changes were needed because it had a slightly later version of PHP installed.

  • The directory name is now php5-5.2.4
  • The compiled package name is now php5-gd_5.2.4-2ubuntu5.9_i386.deb

By: Vertigo

Worked perfectly, thanks!

 A few notes to anyone else that comes across this. You can use sudo instead of -rfakeroot just fine (it gave me problems), and expect the packages to take a LOOONG time to build - took me upwards of 40 minutes, I could be off there, wasnt timing.

Once done, youll have a bunch of deb files, most of which you wont need, but the install of the new gd package will take seconds, and youre done.

Test it before and after by trying to use imagerotate()

By: Liberty Dandira

You are a star..thanks a lot...those with AMD64 servers, I have the deb which I think you can install that I have compiled. Contact me so that I can send you the deb.

By: Anonymous

You will need to "cd .." before you execute "dpkg-buildpackage -rfakeroot" you will get an error otherwise.

By: Anonymous

Hi, thanks for this great post. I followed the instructions as described however when it came to installing the compiled package, it could not be located. It might be because I am using the latest version of ubuntu and do  not know the correct name for the package. I also went ahead to list the available packages but did not see any under a similar naming style to the one that you have in your post.

 Any pointers in the right direction would be appreciated.


By: Anonymous

 Had the same thing happen. Noticed in the messages it said to use -d.

sudo dpkg-buildpackage -d

This worked for me. It took 1 hour to compile. It made several files including:


Followed the rest of the instructions fine. Now have bundled and imagerotate() and imagettftext()

You can check for this with function_exists('imagerotate') and function_exists('imagettftext') in php. 

By: Chad R. Smith

Hey everyone,

I thought I would make this much simpler for people and provide my .deb that I created using this method. Since it takes 1 1/2 hours to get configured and it’s just easier to grab the .deb and go.

Ubuntu version 10.04 (64-bit)

Please note that it’s the 64-bit version, so if you are on a 32 bit system it wouldn’t work. We also tested in our office with 9.04, 9.10, 10.04, and 10.10 (beta)

Now, you install this and it works perfectly, but your update manager will tell you that there’s an update. DO NOT install the update it will revert the changes. How do you get the updater to hold the package? By following the steps:


1.) Open a terminal
2.) Run the following command: sudo apt-get install wajig
3.) Say Yes to the install
4.) After install do the following: sudo wajig hold php5-gd

There you go now you will have GD bundled 2.0.34, and the package updater will hold the package so it's never updated again.

Take care,
Chad R. Smith

By: Anonymous

I tried a recompile after forgetting to change the class on a 64-bit ubuntu 9.10 for php5.2 on virtualbox with this manual and the process choked on a call trace ending in child_rip+0x0/0x20

I ran make clean from within the source directory to clean up before recompile but it complained that There is no rule to target 'clean' it.

Is it necessary to symlink to the correct libraries for jpeg like described at by Larry Kluger? They seem to be in place already. If so, how do I go about things in light of the above tutorial.

Also @Chad, thanks for the precompile but unfortunately the link is buggered. Do you have a correct address?

I figure that i'm missing some very obvious requirements that are not mentioned  in this tutorial.I welcom suggestions.

By: petemaxi

Hi, I noticed the 64-bit .deb and thank you for that, but what I really needed was a 32-bit .deb, couldn't find one, so I made my own.

Here's the link:-

By: petemaxi

If you've been struggling to follow the link, it's because the URL has a space at the end for some reason. Get rid of the offending space and your page should load. Hopefully someone on this site can edit this mistake in my previous post and get rid of this one.

By: petemaxi

I have updated . It now has 32 and 64 bit versions of the bundled gd library for three different versions of php, the latest php versions for Ubuntu 9.10, 10.04 and 10.10.

Compiling is such a faff, so I've done it for you, enjoy!

By: Gia Phu LIU


 I got an error on Ubuntu/Hardy by using the method above.

 cat /usr/src/php5-5.2.4/test-results.txt 

Warning: putenv(): Safe Mode warning: Cannot set environment variable 'SSH_CLIENT' - it's not in the allowed list in /usr/src/php5-5.2.4/run-tests.php on line 70

Warning: putenv(): Safe Mode warning: Cannot set environment variable 'SSH_AUTH_SOCK' - it's not in the allowed list in /usr/src/php5-5.2.4/run-tests.php on line 71

Warning: putenv(): Safe Mode warning: Cannot set environment variable 'SSH_TTY' - it's not in the allowed list in /usr/src/php5-5.2.4/run-tests.php on line 72

Warning: putenv(): Safe Mode warning: Cannot set environment variable 'SSH_CONNECTION' - it's not in the allowed list in /usr/src/php5-5.2.4/run-tests.php on line 73

Warning: set_time_limit(): Cannot set time limit in safe mode in /usr/src/php5-5.2.4/run-tests.php on line 76


|                       ! WARNING !                         |

| You are running the test-suite with "safe_mode" ENABLED ! |

|                                                           |

| Chances are high that no test will work at all,           |

| depending on how you configured "safe_mode" !             |


ERROR: invalid PHP executable specified by TEST_PHP_EXECUTABLE  = ./apache2-build/sapi/cli/php


So I solved it this way:

I've downloaded the newest PHP5 Version: 5.2.17 (because I got an error on 5.3.6) to /usr/src/

tar -xzvf filename

 cd /usr/src/php-5.2.17

 ./configure --with-apxs2=/usr/bin/apxs2 --with-mysql=/usr --with-mysqli=/usr/bin/mysql_config --with-pgsql=/usr --with-tidy=/usr --with-curl=/usr/bin --with-curlwrappers --with-openssl-dir=/usr --with-zlib-dir=/usr --with-xpm-dir=/usr --with-pdo-pgsql=/usr --with-pdo-mysql=/usr --with-xsl=/usr --with-ldap --with-xmlrpc --with-iconv-dir=/usr --with-snmp=/usr --enable-exif --enable-calendar --with-bz2=/usr --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --with-zlib-dir=/usr --with-freetype-dir=/usr --enable-zip --with-pear

(you can add soap or openssl if you want to or other modules) 


 make -i install

 cd /etc/apache2/mods-available/

make sure that "php5.load" look similar like this:

LoadModule php5_module /usr/lib/apache2/modules/


make sure that "php5.conf" look like this:

cat php5.conf 

<IfModule mod_php5.c>

  AddType application/x-httpd-php .php .phtml .php3

  AddType application/x-httpd-php-source .phps



if not, enable php5 module by using this command:

a2enmod php5

 restart apache:

/etc/init.d/apache2 restart


then check the phpinfo() 

I hope I could help some people out there.