Posted by ant
May 13th, 2008
Filed in Sysadmin
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
How 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.
# 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!
39 Responses to “Recompiling PHP5 With Bundled Support for GD on Ubuntu”
Leave a Reply ☆
Blog uses Mephisto
Design from OSWD
by dreamLogic

May 13th, 2008 at 03:13 PM
couldnt your just do [code] sudo apt-get install php5-gd [/code]
May 20th, 2008 at 06:00 PM
Donald,
The apt-get method will NOT work if you want to have a BUNDLED version of GD. The bundled version comes with thingies like “imagerotate” which is missing from the apt-got library. I am currently compiling the library, fingers crossed
Oh, and thanks, ant!
May 27th, 2008 at 07:42 PM
Awesome! I’ve been developing an application that relies heavily on imageconvolution and couldn’t get the thing to run on ubuntu in development. Your instructions worked great. One tip though, I had to enable the source code option in the synaptic package manager repository settings before I could download the php source code to compile it. Not a big hurdle, but it’s unchecked by default.
June 8th, 2008 at 02:12 PM
Thank you very, very much. I’ll see if it works as soon as my compiler runs trough.
June 13th, 2008 at 03:53 PM
Thanks for the howto! but after compiling and installing the new version it’s not working anymore (php5-gd), which is a pity really.
Solved the problem using the binary packages from dotdeb.org, works flawlessly.
June 18th, 2008 at 09:50 PM
Thank you very much fpr sharing this! As soon as the compiler is ready I can test the result :) and can hopefully use the rotate function of my phoca gallery for joomla again…
July 23rd, 2008 at 12:19 PM
Great thanks for this tips.
But, I have only one question. When I use apt-get update again, ubuntu will tell me the php-gd need update.
How can I update others pakage but without this one?
Regards, Mark
September 3rd, 2008 at 10:07 AM
Thanks for the tips!
2Mark: Just try ‘aptitude’ instead of ‘apt-get’. It’s much more smarter than traditional tools:
ws1:/usr/src# aptitude hold php5-gd Reading package lists… Done Building dependency tree… Done Reading extended state information Initializing package states… Done Reading task descriptions… Done Building tag database… Done The following packages have been kept back: linux-image-2.6.18-6-686 php5-gd 0 packages upgraded, 0 newly installed, 0 to remove and 2 not upgraded. Need to get 0B of archives. After unpacking 0B will be used. Writing extended state information… Done
As you can see, server is running custom built kernel image and libGD for PHP. They will not be upgraded with newer versions until you unhold them.
September 3rd, 2008 at 10:38 AM
Dude, thank you so much for this!!
Just a few things I have learned that other people might like to know…
1. In a production environment with php already enabled… if you can manage taking the server offline for about 40 minutes then you can make this happen. Simply run “apt-get purge php5” first, then run what he has listed here. I know because I just did this.
2. You must run “dpkg-buildpackage
rfakeroot” IN THE php5-5.2.* DIRECTORY -otherwise it will error on you.3. In the end, if you want to install more than just the gd you CAN dpkg -i the “php5-gd_5.2.-etc…” file first AND THEN if you want to later you can dpkg -i the “php5_5.2._all…” file.
Hope that helps & I also hope that last one makes sense.
Take care & happy bashing! —Ace
September 3rd, 2008 at 10:42 AM
Wow, so that parsed the code very weird. Please, disregard the fact that things are striked out or bolded. —Ace
PS: Anthony, feel free to change my former comment so it does not parse so oddly & you can delete this comment too if you fix the other one. :-)
September 4th, 2008 at 10:12 AM
This is the best guide I have seen to solve this problem, and the only one I could make it work. So, thank you! :-)
September 15th, 2008 at 07:31 AM
Thank you for this excellent guide. I just followed your instructions to the letter & it worked perfectly. I need the imageantialias function for a specific Joomla! component & this function is apparently not part of the GD library that works with PHP & Apache in any Ubuntu or Debian distribution.
September 15th, 2008 at 05:48 PM
thank you for this tutorial, it works perfect
September 28th, 2008 at 10:36 PM
Hello, and thank you, i have the same question as Mark (#7). “When I use apt-get update again, ubuntu will tell me the php5-gd need update. How can I update others pakage but without this one?”
Can you help ? Thank you
October 1st, 2008 at 04:03 PM
when i typed this dpkg -i php5-gd_5.2.3-1ubuntu6.3_i386.deb it showed php5-gd_5.2.3-1ubuntu6.3_i386.deb no file or dir where will it stored
October 16th, 2008 at 01:38 PM
Another simple way to do it using compilation but maybe a bit dirty…
http://info.momeunier.fr/node/20
But I must say I like your way to do it :)
November 13th, 2008 at 09:46 PM
Another satisified customer! Worked perefctly.
Thanks.
November 14th, 2008 at 11:31 PM
still receiving the following error message:
Fatal error: Call to undefined function imageantialias() in /var/www/admin/library/functions.php on line 231
Any help would be greatly appreciated
November 14th, 2008 at 11:36 PM
still receiving the following error message:
Fatal error: Call to undefined function imageantialias() in /var/www/admin/library/functions.php on line 231
Any help would be greatly appreciated
January 6th, 2009 at 10:07 PM
Hi, i am a happy guy. I am french, i have spent about 6 * 8 hours = 48 hours to search before to find ur “how to”. You can’t imagine how much pleasure u r giving me. So many ppl could use their GD in captchas, without problem, nobody understanding why i couldn’t… It is long story i have related at http://www.siteduzero.com/forum-83-356477-p1-ubuntu-probleme-avec-les-fichiers-png.html (in french sorry !) and… well, ur how to the only one i found explaining what u explain. Thanks again …LOL
January 29th, 2009 at 01:42 PM
Nice Solution, especially when you like to do it the Debian/Ubuntu way!
This Trick also works with Debian 4.0 (Etch)
September 26th, 2009 at 10:34 AM
after “apt-get build-dep php5”, it auto generated below files in /usr/src
php5-5.2.6.dfsg.1 php5_5.2.6.dfsg.1-3ubuntu4.2.diff.gz php5_5.2.6.dfsg.1-3ubuntu4.2.dsc php5_5.2.6.dfsg.1.orig.tar.gz
then
cd php5-5.2.6.dfsg.1 dpkg-buildpackage -rfakeroot cd .. dpkg -i php5-gd_5.2.3-1ubuntu6.3_i386.deb
No found any .deb files.
any thoughts on this?
thanks.
September 29th, 2009 at 12:58 PM
You can solve problem without debian version of php and gdlib. Simply download and compile gd2 using a—prefix=/usr/local/gd2/ , and later download and compile compile php using—with-gd=/usr/local/gd2. Be carefull: do not use standard prefix otherwise php configure cannot find gd.h or gdlib.so ..
I’ve just installed php5.2.3 + gdlib 2.0.35 and work perfectly.
October 5th, 2009 at 11:52 AM
Hello there, nice Tut so far but a Noob like me still got some question: - I already installed PHP. Do I need to remove (purge) PHP before compiling on my own. (Think it’s a YES) - If so is there any list or something similar containig usefull informations/packages I should backup fist in order to reinstall them?
October 23rd, 2009 at 04:29 PM
merci infini pour votre solution.
October 24th, 2009 at 08:43 PM
Thanks for great guide! Followed it and everything worked like a charm! :) The only question is – can I remove the original php5-gd package via aptitude (apt-get)? Does it change anything or can be freely removed? Thanks once more!
October 28th, 2009 at 09:05 PM
Thanks, it helped me a lot!
November 11th, 2009 at 04:34 PM
I ran through this with no luck.
gd installs just fine—but it is the same gd with no filtering or rotation.
I’m running Karmic— I have a feeling that the package that was downloaded when I ran through the instructions wasn’t different than what I got out of synaptic…
In my sources.list “backports” and “partner” were commented out—
But I’m not sure which needs uncommented or what…
November 11th, 2009 at 04:37 PM
The package that gets built is named php5-gd_5.2.10.dfsg.1-2ubuntu6.1_i386.deb which is slightly different from the instructions… I’m not sure where I went wrong.
November 13th, 2009 at 11:43 PM
Much kudos, Ant. Works like a charm !
@Matt: No worries, when Ant wrote this posting, php was at version 5.2.3. But it still works for the current version (5.2.10)
My package is even called differently than yours: php5_5.2.10.dfsg.1-2ubuntu6.1_all.deb
I just did a
and then a
and it works like a charm ! (at least imageantialias() I was depending on is working fine).
November 25th, 2009 at 09:50 PM
after dpkg-buildpackage -rfakeroot nothing comes, it says: Build complete. Don’t forget to run ‘make test’. but it does not end and fall back to command line. last line says: mkdir -p temp_session_store env NO_INTERACTION=1 TEST_PHP_CGI_EXECUTABLE=./cgi-build/sapi/cgi/cgi-bin.php5 TEST_PHP_EXECUTABLE=./apache2-build/sapi/cli/php ./apache2-build/sapi/cli/php run-tests.php > test-results.txt
November 28th, 2009 at 08:35 PM
thanks it worked fine until I did aptitude safe-upgrade . Now it turned back to orginal GD.
December 10th, 2009 at 09:53 PM
Thank you very much for this tutorial ! It works really nice !!
December 10th, 2009 at 10:06 PM
@cemkaan: Same problem here.
December 10th, 2009 at 10:50 PM
@cemkann:
Just open another window and “tail” the test-results.txt file.
tail -f test-results.txt
It will show you what kind of test are running.
December 17th, 2009 at 03:20 PM
я считаю: превосходно…
December 25th, 2009 at 12:39 PM
I am getting the following warning message and the build is not completing,
“Warning: PHP Startup: Unable to load dynamic library ’/usr/lib/php5/20060613+lfs/php_gd2.dll’ – /usr/lib/php5/20060613+lfs/php_gd2.dll: cannot open shared object file: No such file or directory in Unknown on line 0”
Can u please help me ..
January 14th, 2010 at 09:45 AM
Пожалуй так оно и есть, на самом деле все очень просто :)
February 6th, 2010 at 03:06 AM
Thank you for this! I have a site that really needs crisp fonts and since I switched to Ubuntu and stopped compiling LAMP from scratch my fonts have looked bad but the customer insisted on using packages. After installing the newly created GD package the fonts look good again, and I’ve learned a lot about Ubuntu.
Thanks Again!