While Apple’s included PHP configuration included with Apache is getting better and better, it is still far from complete.
First thing that it’s missing is support for GD library, which, if you are looking to manipulate images via PHP, is simply essential.
While there are a bunch of other ways to run PHP (XAMPP, MAMP and others), I like using the Apache and PHP that come with Leo and build on top of that (like a true geek).
Here’s how to compile GD for your PHP…
Requirements
- You must have Apple’s Dev Tools installed first (meaning X-Code). This is available on your OSX Install DVD or from Apple’s Web Site.
- You also need X11 SDK installed. The best way to do it: install X11 client from OSX DVD and then install the Dev Tools - this way SDK will be installed for you.
- Backup your system… We don’t want you screwing things up and having no safe point to return to, right?
- I assume you have not manually updated anything related to GD, libpng, libjpeg and freetype so far (if you have, make sure you adapt these instructions to the changes you made).
- I also assume that you have PHP5 ENABLED on your Leopard Machine. It’s quite easy to do and Tyler can help a bunch on this. In short: move the /etc/php.ini.default to /etc/php.ini, and edit /etc/apache2/httpd.conf and remove # in front of LoadModule php5_module.
Installation process
- Open a terminal window and create a new folder for storing your source files and building future apps. While most people use /SourceCache, I like using a folder called “.sources” in my Home directory. So, let’s first create it and then go into it by typing the following in terminal:cd ~
mkdir .sources
cd .sources - Next step is to download libjpeg (the only one missing) so GD can work properly, type:curl -O http://www.ijg.org/files/jpegsrc.v6b.tar.gzNote: that’s an O as in “oh”, not a 0 as in “zero”

- We also need the PHP source so we can compile the GD from there. 10.5 ships with PHP version 5.2.4 originally, but if you run 10.5.5 like I do, chances are you have 5.2.6! Make sure you know which version you have first - so you can download the correct PHP source. Do so by typing:php -vNote: the top line should tell you something like:
- PHP 5.2.6 (cli) (built: Jul 15 2008 23:16:51) - Now that you know the version you got, get the correct file below:For 5.2.4 type:
curl -O http://www.opensource.apple.com/darwinsource/10.5/apache_mod_php-43/php-5.2.4.tar.bz2And if you got 5.2.6:
curl -O http://www.opensource.apple.com/darwinsource/10.5.5/apache_mod_php-44.1/php-5.2.6.tar.bz2 - Now it’s fun time, let’s take care of libjpeg first:Unpack it by typing:
tar -xzvf jpegsrc.v6b.tar.gzEnter the folder and copy the config files to it:
cd jpeg-6b
cp /usr/share/libtool/config.sub .
cp /usr/share/libtool/config.guess .Now we need to configure it, make it and install it, so copy and paste the following command into the terminal (this will make the 64 bit version - which is what works for default Apache installation):
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
That’s all one line, when complete, type:
makeNow, before installation we have to make sure couple of crucial directories exist, so type this:
sudo mkdir -p /usr/local/include
sudo mkdir -p /usr/local/bin
sudo mkdir -p /usr/local/lib
sudo mkdir -p /usr/local/man/man1And it’s time to install libjpeg by typing:
sudo make installNote: you will have to enter the password when calling commands with sudo since installation has to proceed as root.
- Now that we got that out of the way, we need to get back into the .sources folder, unpack the PHP source, get into the GD folder, and do some fun stuff:cd ..
tar -xvf php-5.2.6.tar.bz2
cd php-5.2.6/ext/gdNote: above I assume you got 5.2.6, if you downloaded the 5.2.4, kindly replace 6 with of 4 in the above commands
.Now run:
phpizeAnd then this one line command:
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 –with-gd
When it all compiles, type:
make
sudo make install - Now the fun part is over. I will assume that you already have PHP5 enabled on your MacOS as mentioned under requirements, so:Edit the php.ini file
sudo pico /etc/php.iniAdd the following after all “extension=blahBlah” directives:
extension=gd.soThen find:
extension_dir = “./”And put a semicolon in front to disable it, so it now looks like:
;extension_dir = “./”Save your php.ini file (Press CTRL + O and then enter).
- That’s it. Restart your Apache Server (System Preferences -> Sharing - and uncheck and check back the Web Sharing item).
- Test that GD is installed by typing this in console:
php -r ‘phpinfo();’ | grep GDIf GD is there, you should see this output:
GD Support => enabled
GD Version => bundled (2.0.34 compatible)If it isn’t - make sure you followed all the instructions correctly: Especialy the part of editing php.ini file and restarting your server. You should also check the Apache Log since it may help telling you if the compiled GD object is corrupt. Do so by typing:
tail /var/log/apache2/error_log
Hope this helps you out
My next tutorial, MySQL and mcrypt, so stay tuned
.









23 Comments
I was follow all of this instructions and WORK
thanks
Glad it did
Didn’t work for me. I got the following message after that one long line…
-bash: ppc: command not found
The whole paragraph is a command you have to copy/paste and execute, not line by line:
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
Hey man - when you post this stuff you need to be careful about the characters that you’re using. I got this to work but only after replacing a number of illegal characters.
Instead of ” use ”
and instead of – use –
I know - WordPress encodes them… Sorry about that. I’ll try to put them in [CODE] tags and see if that helps.
when I run this step:
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 –with-gd
I get the fallowing error:
configure: error: invalid variable name: –with-zlib-dir
Please help…
There may be an issue with the ” characters as noted by previous commenter - make sure when you copy the command - you REPLACE the ” by deleting them and entering them yourself via keyboard.
Also - make sure you have Developer Tools installed since it contains all needed libraries.
Hi,
thanks for this, it worked like a charm.
My only problem is that commenting out the extension_dir setting in php.ini breaks some other code on my webserver.
Can you explain what this does, or what the extension_dir defaults to when unset ? or offer any advice on how to get gd running without commenting out this line ?
ah, so many questions
thanks,
glenn
extension_dir tells in what directory PHP should look for dynamically loadable extensions.
I suppose your “other code” relies on some dynamic extension which is not getting loaded because of this.
What’s failing to run properly? When you find out what it is, there may be a way to load it directly at the path it is found at instead (?)
In the instructions I found - they disabled the extensions_dir so I did it also in my instructions above… All my code works fine
Hi, I get the same error as evan:
configure: error: invalid variable name: –with-zlib-dir
I have XCode installed (is that the Developer tools?) and I also installed X11 SDK. I alredy replaced the “, what am I missing???
Thank you for your help.
Thanks for the instruction. I got sth as
“sh-3.2# php -r ‘phpinfo();’ |grep GD
GD Support => enabled
GD Version => bundled (2.0.34 compatible)”
But when I create a phpinfo.php file under my webserver folder and access it from the web browser, the GD module is not listed in this info page. Could you please help?
To tell you the truth, I really do not know. You might want to follow the sources at the bottom of the post and see if in their comments sections someone had same issues and got it working.
Sorry I can’t be of any more help, I’m puzzled myself
I fixed it :$, it was my mistake, I replaced the quotes (”
but forgot to replace the dash (-), after that, everything went smooth.
Thanks for your help
Glad it work Marco
I wish WordPress did not screw up the characters, but now we know at least what the problem is 
@Marco Soria
I had the same problem with you as to
—–
“sh-3.2# php -r ‘phpinfo();’ |grep GD
GD Support => enabled
GD Version => bundled (2.0.34 compatible)”
—–
i can see in the same in the terminal but not in the phpinfo page. can u please help me coz i can’t read your reply why u fix it because of the smileys. thanks.
@james, did you check your php.ini, it looks to me that you haven’t configured your php.ini properly.
I get to this bit”Now that we got that out of the way, we need to get back into the .sources folder, unpack the PHP source, get into the GD folder, and do some fun stuff:cd ..
tar -xvf php-5.2.6.tar.bz2
cd php-5.2.6/ext/gd
but there’s no such file in the .sources directory. I have only three files:jpeg-6b, jpegsrc.v6b.tar.gz, and php5.2.6.tar.bz2
Oh no what’ve I done?
Mark
The smart quotes and dashes were a bit of a problem, and I did run into one error “configure: error: libpng.(a|so) not found.” So I just specified /Developer/SDKs/MacOSX10.5.sdk/usr/X11 and all worked great. Thanks alot!
Finally, a decent explanation that just works! Many Thanks.
Thanks for all the info here … it has been very helpful. However no success yet! I fixed the quotes and dashes and began to make progress but get the error “can’t find ./libtool:cammand not found when I type “make” I installed Xcode and then installed x11sdk from downloaded xcode dmg from apple. I feel like I am close and could use your insight if you have a moment
At which step does it fail? What are you making? You have to install the libjpeg everything else…. ?
Worked like a charm, thanks for taking the time to write this up..
Write a Comment