[English] PHP: gd library > 2.0 (2.0.1 / 2.0.34) en Ubuntu


I noticed that some English speaking people were reading and even making comments on the original (spanish) post. My first guess is: google translator but as good as it may be it is still an automatic translated document and I think I can do better than a blunt headed computer (at least when it comes to translating), also I though it might bring more people into reading the article. So here it is the English version of it:


Ubuntu is a very popular operating system, in the last year its popularity has grown a lot because it is a stable and reliable operating system with regular updates. Its popularity has grown not only in desktops and laptops but also in web servers, however, one must admit that from time to time Canonical has some flaws, sometimes they leave very useful software packages out of their official repositories, sometimes they make mistakes when creating packages, create them in a weird way or leave out the latest updates for some software.

This is exactly the case with the gd library for PHP. The gd library is a graphics library that provides various functionalities like scaling, rotating, trimming, among others. Some plug-ins with advanced graphical effects like ImageFlow use this library to achieve those effects, like the reflections. Some CMS, like Drupal use this plug-in to create picture galleries.

ImageFlow in particular needs gd library version 2.0.1 to work and when it doesn't find it, it doesn't work at all, instead of images we get an error message like this one:

ImageFlow requires GD 2.0.1 or higher (2.0.28 recommended)

The problem is that in the Ubuntu systems, even in the last version (11.04) the gd library version that gets installed when you install the php5-gd package is 2.0 and no matter how many times you run apt-get update you never get an updated version of the library since Canonical (or the community member who maintains the package to be precise) haven't updated the library.

When searching through the network there is a lot of people asking how can they solve this issue, not only in Ubuntu forums but in other sites. There is, among the proposed solutions, one which consists in adding the Debian repositories to the sources.list file and then install php5-gd from there. The problem with this solution is that the dependencies in Debian repositories are not the same that in Ubuntu repositories and some times doing this results in things that stop working like the Plesk control panel, because when installing php5-gd other packages get changed too.

Also when trying to install from the Debian repositories some times another problem arise:

Dependency error:
 php5-gd depends on libjpeg > 15b but 15bubuntu1 will be installed
E: Broken packages

Obviously we have the right library version but the problem is that dpkg fails to compare the versions and so the installation can simply fail, and although we could force the installation from apt-get this could leave us to other problems. We could damage the local repositories or even end with a PHP installation that doesn't work at all.

I have found another solution that worked really well for me and I wanted to share it with the Ubuntu community. I really hope that It will work for you as good as it did for me, so, here we go:

Updating the gd library

Uninstalling the current version of php5-gd

The first thing we have to do is uninstall the current version of php5-gd, if we have one installed, that is, to check if there is a version currently installed we use this command:

dpkg --get-selections | grep php5-gd

We will get something like:

php5-gd      install

If the previous command doesn't show any output it means that there is no version of php5-gd installed so you can just go ahead with the following step "Installing Alien". If the library is installed you must remove it completely first, to do so use this command:

sudo apt-get --purge remove php5-gd

When uninstalling the library the system may tell you that there are some packages that were installed with it and that are no longer needed and will suggest you to use apt-get autoremove to get rid of them. PLEASE DON'T DO IT, leave the packages installed because in any case we are going to install another version of the gd library and so the packages will probably be needed again.

Installing Alien

Alien is an special utility that can be installed in the linux systems that use packages and lets you convert .deb (Debian Packages) to .rpm (RedHat and SuSe Packages) and vice versa. We are going to use this utility in the next step so we install it like so:

sudo apt-get install alien

Once we have Alien installed we are ready to go and search for an updated gd library

Get an updated version of the gd library

The CentOS operating system is a RedHat branch and it has the updated version of the gd library and so we are going to use this operating system's packages to bring an updated version of the library to our Ubuntu system. The fist thing we will need is the exact version of PHP we have installed, to get this information we execute this command:

sergio@fate:~$ php --version
PHP 5.3.2-1ubuntu4.8 with Suhosin-Patch (cli) (built: Apr 27 2011 05:07:22) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

We see here that the installed version of PHP is 5.3.2, now we are going to pay a visit to CentOS' Repositories and we are going to search for the correct gd library for our PHP 5 installation. We use the search to locate the right php5-gd package for our PHP version and architecture (32 or 64 bits) and we are going to download it, in my case that would be:

wget ftp://ftp.icm.edu.pl/vol/rzm1/linux-opensuse/distribution/11.3/repo/oss/suse/i586/php5-gd-5.3.2-1.31.i586.rpm

What we are going to get is a .rpm package. Now we have to use Alien to convert it to a .deb package, so we do:

sudo alien -d php5-gd-5.3.2-1.31.i586.rpm

Alien will process the package and convert it to a .deb package that we can install:

php5-gd_5.3.2-2.31_i386.deb generated

Now we have to install it like we do with any other Debian package:

sudo dpkg -i php5-gd_5.3.2-2.31_i386.deb

The location where Ubuntu stores the PHP 5 extensions is a bit weird and somewhat curious opposed to CentOS so we will have to move some files to make them available for our Ubuntu PHP 5 installation, we do so like this:

sergio@fate:~$ cd /usr/lib/php5
sergio@fate:/usr/lib/php5$ ls
20090626+lfs  extensions  libexec  maxlifetime
sergio@fate:/usr/lib/php5$ cd extensions
sergio@fate:/usr/lib/php5/extensions$ ls
sergio@fate:/usr/lib/php5/extensions$ sudo mv gd.so ../20090626+lfs/gd.so

Basically what we are doing here is moving the library gd.so from the "extensions" directory to the "20090626+lfs" directory which is the directory where Ubuntu puts PHP extension (I told you it was weird) so that PHP can find it.

In your version of Ubuntu the name "20090626+lfs" may be a little different but you'll know which folder it is by the unusual name

Finishing the installation

Well, so we already have a recent version of the gd library, but, we are not done yet, that library is not totally functional, now we have to take care of some details that arise from using other operating system's packages

For the next step we have to make sure that PHP is logging it's error to a file that we can check, to do so we go to the /etc/php5/apache2 directory and open the php.ini file

There we search for a line like this one:

;error_log = php_errors.log

We remove the semicolon (;) at the beginning of the line and replace php_errors.log with the full path to the file we want PHP to log it's errors to, for example I used:

error_log = /var/log/php5/errors.log

We save the file, then we have to make sure that Apache can write on the PHP log file so use these commands to do so:

sergio@fate:/etc/php5/apache2$ cd /var/log
sergio@fate:/var/log$ sudo mkdir php5
sergio@fate:/var/log$ cd php5
sergio@fate:/var/log/php5$ sudo touch errors.log
sergio@fate:/var/log/php5$ sudo chmod o+w errors.log

Now we restart the Apache web server and check the PHP error log file:

sergio@fate:/var/log/php5$ sudo /etc/init.d/apache2 restart
 * Restarting web server apache2
 ... waiting                                                         [ OK ]

sergio@fate:/var/log/php5$ tail errors.log
[30-Apr-2011 22:19:44] PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php5/20090626+lfs/gd.so' -
libpng14.so.14: cannot open shared object file: No such file or directory in Unknown on line 0

There we see that there is an error, PHP tells us that we are missing the libpng14.so.14 library, so we are going to look for it on the CentOS' repositories. There we use the search with the option that says Search for rpms which contain given file name (1) e.g. libhistory.so to locate the package with the library we need.

Once we locate the correct package for our architecture (32 or 64 bits) we are going to download the package like we did before:

wget ftp://mirror.switch.ch/pool/1/mirror/opensuse/distribution/11.4/repo/oss/suse/i586/libpng14-14-1.4.4-2.1.i586.rpm

We get the RPM package and once again we use Alien to conver it to a Debian package:

sudo alien -d libpng14-14-1.4.4-2.1.i586.rpm
libpng14-14_1.4.4-3.1_i386.deb generated

Then we install the library:

sudo dpkg -i libpng14-14_1.4.4-3.1_i386.deb

After installing the library we restart the Apache Web server and check the PHP error log file once again. If we encounter another missing library we will have to repeat the previous procedure to install it from the CentOS' repositories. When you see no more errors in the PHP error log then the installation is done

Verifying the installation

Now we check that the installation is working properly using the phpinfo function and checking that the installed gd library is version 2.0.34:

GD Supportenabled
GD Versionbundled (2.0.34 compatible)
FreeType Supportenabled
FreeType Linkagewith freetype
FreeType Version2.3.12
T1Lib Supportenabled
GIF Read Supportenabled
GIF Create Supportenabled
JPEG Supportenabled
libJPEG Versionunknown
PNG Supportenabled
libPNG Version1.4.3
WBMP Supportenabled
XPM Supportenabled
XBM Supportenabled


Using the Android utility we managed to update the gd library for PHP. Until Canonical updates the library version in the official Ubuntu repositories this is a good way around the problem.

I certainly hope that this post was useful to you. For me it meant an entire day working trying to find a way to update the library while keeping PHP, Plesk and the rest of the software installed on the server working.

1 comentario:

Anónimo dijo...

Brilhante. Obrigado.