Install GD for PHP on Mac OS X 10.5 Leopard
So, you need GD for your killer PHP web app, and you’re running Mac OS X 10.5? A quick look shows that GD doesn’t ship with Leopard. No worries. It’s pretty simple to install.
There are a few core requirements you must take care of before getting started. Choose to ignore these, and you’re doomed to failure!
- Always back up your system before a command-line activity such as this.
- Update your system to Mac OS 10.5.5. I could detail how to do this with prior versions, but I don’t have time.
- Install the latest version of Apple’s Developer Tools: XCode 3.0+ for 10.5. XCode is available on your OS X DVD, or from Apple as a free download.
- X11 must be installed (it is by default), as well as X11 SDK (from the Developer Tools in step 3).
DISCLAIMER: The author claims no responsibility for any damage that may occur from the use of any information found here or found on links followed from this document. If you choose to use this information, you do so at your own risk.
Get Started
To begin, open Terminal (Macintosh HD -> Applications -> Utilities ->Terminal) and invoke the superuser do command. You will need to enter your administrator password. Careful - you can now utterly destroy your machine:
sudo bash
You will need to enter your administrator password.
Install libjpeg
The free image compression library, libjpeg, is required by GD.
First, let’s create a directory for storing the source files we’ll be downloading:
mkdir -p /SourceCache
cd /SourceCache
Download the source file and unpack it:
curl -O http://www.ijg.org/files/jpegsrc.v6b.tar.gz
tar xzpf jpegsrc.v6b.tar.gz
cd /SourceCache/jpeg-6b
cp /usr/share/libtool/config.sub .
cp /usr/share/libtool/config.guess .
Mac OS X Leopard comes in two flavors, depending on the capabilities of your CPU — 32-bit or 64-bit. YOU MUST COMPILE FOR THE PROPER ARCHITECTURE.
Which architecture do you have? Easy. Click the Apple Menu, select “About This Mac”. If the screen says Core Duo, you’re 32-bit. Anything else, and you’re running 64-bit. My MacBook Pro is running 64-bit because of the Core 2 Duo:
For 32-bit only, use the following command:
./configure --enable-shared
64-bit architecture uses this command instead:
MACOSX_DEPLOYMENT_TARGET=10.5 CFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp" CCFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe" CXXFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe" LDFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -bind_at_load" ./configure --enable-shared
Continue on for both architectures:
make clean
make
mkdir -p /usr/local/include
mkdir -p /usr/local/bin
mkdir -p /usr/local/lib
mkdir -p /usr/local/man/man1
make install
You now have compiled libjpeg!
Download and compile the GD graphics library extension (gd.so)
We will be using Apple’s Darwin sources for PHP, which interestingly contain the GD source code. Why Apple doesn’t ship with gd.so already compiled is known only to the maker.
mkdir -p /SourceCache
cd /SourceCache
curl -O http://www.opensource.apple.com/darwinsource/10.5.5/apache_mod_php-44.1/php-5.2.6.tar.bz2
tar xjf php-5.2.6.tar.bz2
cd /SourceCache/php-5.2.6/ext/gd
phpize
Again: YOU MUST COMPILE FOR THE PROPER ARCHITECTURE.
For 32-bit use:
./configure --with-zlib-dir=/usr --with-jpeg-dir=/usr/local/lib --with-png-dir=/usr/X11R6 --with-freetype-dir=/usr/X11R6 --with-xpm-dir=/usr/X11R6
For 64-bit use:
MACOSX_DEPLOYMENT_TARGET=10.5 CFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp" CCFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe" CXXFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe" LDFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -bind_at_load" ./configure --with-zlib-dir=/usr --with-jpeg-dir=/usr/local/lib --with-png-dir=/usr/X11R6 --with-freetype-dir=/usr/X11R6 --with-xpm-dir=/usr/X11R6
sudo ln -s /usr/X11/lib/libpng.3.dylib /usr/X11/lib/libpng.3.0.0.dylib
Likewise, if your error refers to libpng12.0.##.#, you should create a symbolic link to libpng12.0.dylib.
Then, recompile GD.
Continue on for both architectures:
make clean
make
make install
Add gd.so to PHP
PHP needs to be configured to load the gd.so shared object extension that you just compiled. If you’ve previously created a /etc/php.ini file, simply edit it and add:
extension=gd.so
If you haven’t created a /etc/php.ini file, simply issue the following command to create the file and add the instruction:
echo -e extension=gd.so > /etc/php.ini
If you have previously created a /etc/php.ini file, make sure the extension_dir= directive is commented out (by putting a semi-colon in front of it) or is pointing to /usr/lib/php/extensions/no-debug-non-zts-20060613.
Restart Apache to force the reloading of the PHP configuration file.
apachectl graceful
Confirm that PHP is loading the gd.so extension by running the following command, and looking for the line “GD Support => enabled” in the resulting output:
/usr/bin/php -i|grep -i gd
Alternatively, you can create a file called phpinfo.php in your web server document directory (/Library/WebServer/Documents/) with the following contents:
<?php
phpinfo();
?>
Point your web browser to http://localhost/phpinfo.php, and you should see a GD block verifying installation, as shown below:


Hi. Thanks for this tutorial, is exactly what I was searching for…
I have an error when inserting the 64-bit large command, after the “phpize”.
The error is this one:
“checking for jpeg_read_header in -ljpeg… no”
“configure: error: Problem with libjpeg.(a|so). Please check config.log for more information.”
Do you know which can be the problem?
Thank you in advance!