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!

  1. Always back up your system before a command-line activity such as this.
  2. 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.
  3. 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.
  4. 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.

Determine Your Architecture

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 enough using Terminal or the GUI. For Terminal, issue this command:

/usr/sbin/system_profiler SPHardwareDataType | grep "Processor Name:"

Or, in the GUI, choose the Apple Menu, select “About This Mac”:

The Core 2 Duo is a 64-bit CPU

Match your CPU to the table below:

Model32-bit64-bit
PowerPC G3X
PowerPC G4X
PowerPC G5X
Intel Core DuoX
Intel Core2 DuoX
Intel XeonX

 

Make a note of whether your CPU is 32-bit or 64-bit, because you will be compiling software using vastly different settings depending on your CPU.

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.

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

NOTE: Check the output of the last command. If you get an error similar to this –“/usr/X11/lib/libpng.3.0.0.dylib: No such file or directory” — you should create a symbolic link with a name matching the file referred to in the error message. For example, the above error indicates that no libpng.3.0.0.dylib file exists. Simply create a link named libpng.3.0.0.dylib pointing to libpng.3.dylib:           

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. You will tell PHP to load it by adding a directive in your /etc/php.ini file.

First, let me give you a couple of pointers about this file. It *probably* exists on your machine. If not, you should just create it as a simple text file. Directives in the file can be commented out by placing a semi-colon in front of the directive.

Open the /etc/php.ini file in a text editor, and search for the section on Dynamic Extensions. Mine looks like this:

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

Simply add the following line, which loads your newly compiled GD shared object:

extension=gd.so

Search your /etc/php.ini file for the  extension_dir= directive, and either comment it out (by inserting a semi-colon in front of it) or ensure it is pointing to the directory where your new GD shared object is stored:

extension_dir=/usr/lib/php/extensions/no-debug-non-zts-20060613

Confirm gd.so is loading

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:

GD info block as it appears via phpinfo();

93 thoughts on “Install GD for PHP on Mac OS X 10.5 Leopard

  1. @Curator: The build date is for the PHP binary, which you are not recompiling, so it won’t change.

    So long as Apache is configured to run PHP (which it is if you’re getting output from phpinfo.php), you shouldn’t need to alter the httpd.conf file. The newly edited instructions explain how to point to the newly compiled gd.so in your /etc/php.conf file using the extension_dir directive. That same directive reveals where the gd.so file is stored.

  2. How do I include enabling bcmath to this compile for a 64-bit, and ensuring all the other built-in libraries are enabled? ldap, openssl, http://ftp... all the other stuff?

  3. This is what I am ending up with.

    PWD => /SourceCache/php-5.2.6/ext/gd
    _SERVER[“PWD”] => /SourceCache/php-5.2.6/ext/gd
    _ENV[“PWD”] => /SourceCache/php-5.2.6/ext/gd

  4. Hi Chris Brewer, I have tried and tried and tried many time over and carefully going back throughly but I keep getting the error posted like no. 1 by Andreu and followed your answer on no. 2 to no avail. I have mac os x 10.5.6 with 64 bit. Please help!
    Thank you

  5. Oh, I got it to work just fine, it was my bad! the instruction is perfect, it was my bad jumping the gun early. I wanted everyone to know, if you’all take a little step back re-read the instruction, it’s exactly just that! It’s working very nice, Thank you Chris for posting this, YOU’re the man!

  6. Hey. Thanks for the tutorial. Worked great. I didn’t see the need for the -p in mkdir, though. Also, your cd instructions after the untarring looked over the fact that you are already inside the SourceCache folder. Those were my only gripes, and quite minor at that. 😀

  7. I’ve followed your instructions 3x, copy/pasting commands where necessary and GD isn’t being loaded into PHP. I am using OS X 10.5.6.

    I changed the php source path to reflect 10.5.6 instead of 10.5.5, but other than that there is no change between the PHP versions.

    When I run “/usr/bin/php -i|grep -i gd” it shows GD is enabled:
    gd
    GD Support => enabled
    GD Version => bundled (2.0.34 compatible)

    However phpinfo() doesn’t show anything for GD after restarting apache

  8. Hi,

    I have the same issue as Francesco #23.

    When I do the command /usr/bin/php -i|grep -i gd it shows:

    GD Support => enabled
    GD Version => bundled (2.0.34 compatible)

    However in a phpinfo(); page in php it doesn’t have the GD Image bit on that PHP info page.

    Can someone help me?

  9. The tutorial worked great. I had an error at first, but I followed the advice to @bob, and went back and recompiled libjpeg (and followed ALL the steps this time) and it worked great! Thanks!

  10. Worked on the first try, no errors, took maybe five minutes. Thanks a million!

    [Idea: turn this guide into a more general guide that would also include extending Mac OS X’s built-in PHP with other extensions that aren’t installed by default (not just GD). Seems to be a good foundation for this.]

  11. I’m having difficulty writing out the modified php.ini file in /etc It is write protected and I cannot use chmod to change the permissions on etc

    Is there more than one /etc directory? Did I overlook something?

    Chris

  12. @Chris: Hi. There can only be one /etc/ directory. The php.ini file is write protected, insofar as you need to have admin permissions to edit it. This is accomplished by issuing the “sudo bash” command as illustrated in the instructions. BE CAREFUL. Once you issue that command, you are able to completely destroy your OS installation. I wouldn’t recommend futzing with ownership and permissions of the /etc/ directory.

  13. Thanks chris, great stuff here.
    I ran into the following problem for the GD extension. At the command tar xjf php-5.2.6.tar.bz2 it wouldn’t untar for some reason. After several tries, I had to go the source file on apple site (http://www.opensource.apple.com/source/apache_mod_php/apache_mod_php-44.1/)and download the 5.2.6.tar.bz2 there, then move it to the SourceCache folder, then proceed.
    Everything then worked as expected.
    Note, I have OSX 10.5.7 and PHP 5.2.8 installed (new IMac), so it might be the reason of my problem. Hope it might help someone out there.

    Anyway, thanks again
    stephane

  14. Thank you very much.
    Excellent post, easy to follow.
    Well done!!

    Regards,
    V

  15. Pingback: Mac OS X: Configuring PHP With GD Support | GrasshopperPebbles.com

  16. you’re da man! – 1 thing that may help others: i got the error in your NOTE only when doing the make install. creating the link as per your NOTE and then redoing the make install solved things for me. thanks a million.

  17. I have run into a few problems with the process in the article. I suspect it is a versioning problem but I’ve not been able to resolve it yet. Here are the problems I’ve found. For reference I am running OS 10.5.7 and php ver 5.2.8.

    1. In the “Download and compile the CD raphics library extension” part following the command: tar xjf php-5.2.6.tar.bz2 I get an error essage “tar: Error ….bzip2: (stdin) in not a bpzip2 file.
    I tried downloading it three times with the same result. I placed the URL in my browser and downloaded the file into a different location, then double clicked the file in the Mac window mode and it decompressed the files for me. Not sure what is happening here(currently corrupted file at the source?(, but my workaround seemed to work ok. (unless item 2 below is the resulting problem).

    2. In the same section following the 64-bit use: with partial coding: MACOSX_DEPLOYMENT_TARGET…
    following the output, I get an error message similar to /usr/x11/lib/libpng.3.0.0.dylib:
    No such file or directory.” In trying to create a symbolic link to the file, as you suggested,
    I noted that the file simply does not exist on my computer. Instead I see the files:
    /usr/xll/lib/libpng.3.24.dylib, (also the files libpng.3.26.dylib and libpng.3.35.dylib). So
    creating a symbolic link probably won’t work because somewhere the program is looking for the 3.0.0 version of the file. Any suggestions as to how I modify the process to accept the newer version? Or am I just stuck in version hell and unable to make it work? (or just not smart enough to figure it out?

  18. @Dick,

    It sounds like there is a corrupt source file. In any event, Mac OS X has been updated several times since this article was written, with changes to the PHP version. You need to download the matching version of PHP source: 5.2.8 per your message. It’s at http://www.opensource.apple.com/source/apache_mod_php/apache_mod_php-44.2/php-5.2.8.tar.bz2. Also, try using curl as I detailed in the instructions instead of downloading it with your browser.

    When I run the following command on my 10.5.7 system, here’s what I see regarding libpng libraries:

    ls -al /usr/x11/lib/libpng*

    lrwxr-xr-x 1 root wheel 14 Jan 28 2008 /usr/x11/lib/libpng.3.0.0.dylib -> libpng.3.dylib
    lrwxr-xr-x 1 root wheel 14 Mar 19 2008 /usr/x11/lib/libpng.3.24.0.dylib -> libpng.3.dylib
    lrwxr-xr-x 1 root wheel 14 Dec 15 2008 /usr/x11/lib/libpng.3.26.0.dylib -> libpng.3.dylib
    lrwxr-xr-x 1 root wheel 14 May 13 10:15 /usr/x11/lib/libpng.3.35.0.dylib -> libpng.3.dylib
    -rwxr-xr-x 1 root wheel 684000 Mar 12 20:38 /usr/x11/lib/libpng.3.dylib
    lrwxr-xr-x 1 root wheel 14 Mar 19 2008 /usr/x11/lib/libpng.dylib -> libpng12.dylib
    -rwxr-xr-x 1 root wheel 834 Feb 27 2008 /usr/x11/lib/libpng.la
    lrwxr-xr-x 1 root wheel 16 Jan 28 2008 /usr/x11/lib/libpng12.0.0.0.dylib -> libpng12.0.dylib
    lrwxr-xr-x 1 root wheel 16 Mar 19 2008 /usr/x11/lib/libpng12.0.24.0.dylib -> libpng12.0.dylib
    lrwxr-xr-x 1 root wheel 16 Dec 15 2008 /usr/x11/lib/libpng12.0.26.0.dylib -> libpng12.0.dylib
    lrwxr-xr-x 1 root wheel 16 May 13 10:15 /usr/x11/lib/libpng12.0.35.0.dylib -> libpng12.0.dylib
    -rwxr-xr-x 1 root wheel 684000 Mar 12 20:38 /usr/x11/lib/libpng12.0.dylib
    lrwxr-xr-x 1 root wheel 16 Jan 28 2008 /usr/x11/lib/libpng12.dylib -> libpng12.0.dylib
    -rwxr-xr-x 1 root wheel 834 Feb 27 2008 /usr/x11/lib/libpng12.la

    What this directory listing shows is that Apple is creating new static links to /usr/x11/lib/libpng.3.dylib each time a new OS patch is released. Thus in my directory listing you see libpng.3.0.0.dylib, libpng.3.24.0.dylib, libpng.3.26.0.dylib, and finally libpng.3.35.0.dylib, all pointing to the same file.

    Why is this important? Because all those versions point to the latest version installed, which is always the libpng.3.dylib file. Thus, my instructions are to create a link to the libpng3.dylib file with a name that matches whatever file the error message indicates. For example, if your error message is complaining that it can’t find libpng.3.0.XX.dylib, the create a static link thus:

    sudo ln -s /usr/X11/lib/libpng.3.dylib /usr/X11/lib/libpng.3.0.XX.dylib

    Hope that helps!

  19. Thanks a bunch for the great help. Your suggestion did the trick!. Great blogs!

    Dick

  20. Great tutorial!

    However, I encountered the same problem as Francesco in #24, but configuring with the option “–with-gd” did not seem to make a difference for me, and the GD block still does not appear with phpinfo, though I do receive the correct response from the command line showing the GD is installed.

    Anyone have any ideas?

  21. The php version today and jpegsrc are both updated. This tutorial will not work anymore, at list for dummies like me…

  22. With 64-bit architecture, to compile the current release of libjpeg (version 7), add the option –disable-dependency-tracking to the configure 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 --enable-shared --disable-dependency-tracking

    This is one way to fix the GCC compilation error : “gcc-4.0: -E, -S, -save-temps and -M options are not allowed with multiple -arch flags

    for GD compilation, nothing change?

  23. Hallo, Thanks a lot for this tutorial. I found it really good.
    One thing during the “Install libjpeg configuration” for 64 bits, I had this problem:
    bash-3.2# make
    make all-am
    CC jaricom.lo
    gcc-4.0: -E, -S, -save-temps and -M options are not allowed with multiple -arch flags
    make[1]: *** [jaricom.lo] Error 1
    make: *** [all] Error 2

    This can be solve by adding to the configuration:
    –disable-dependency-tracking

    so then we have 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 –disable-dependency-tracking

    Thanks a lot.

  24. if you get this error at the fist make:
    make[1]: *** [jaricom.lo] Error 1
    make: *** [all] Error 2

    then you might want to use this for the first 64-bit-only 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 –enable-shared –disable-dependency-tracking

    the only thing added to that command is –disable-dependency-tracking

  25. When I try to run the make command for libjpeg (64 bit architecture) I get the following error

    make all-am
    CC jaricom.lo
    gcc-4.0: -E, -S, -save-temps and -M options are not allowed with multiple -arch flags
    make[1]: *** [jaricom.lo] Error 1
    make: *** [all] Error 2

    bit stumped with this one.

    PS jpeg-6b is gone now – I’m using jpeg-7

    Cheers

  26. If your libjpeg installation isn’t working try this for the build

    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 –disable-dependency-tracking

  27. If you have problems with “gcc-4.0: -E, -S, -save-temps and -M options are not allowed with multiple -arch flags”

    just remove all the -arch XXX that is not your computers architecture, for example 64 bit intels only needs the -arch x86_64 flags

  28. Followed the instructions. Worked perfectly. For 10.5.8 I had to change the CURL file address of the PHP source. I also had to enable Root. Thanks for the great tutorial.

  29. Pingback: Compiling PHP-GD on Mac OSX 10.5 | Gravity Layouts

  30. THANK YOU to Chris and http://wp.me/poMmd-3k. Between the two of you, my install had only 1 minor hiccup. “tar xjf php-5.2.8.tar.bz2” didn’t work so I double-clicked on php-5.2.8.tar.bz2 and the Mac’s UnArchive app did the trick.

    My setup:
    MacMini 1.25GHz PowerPC G4
    MacOS 10.5.8
    MacOS Leopard Server for 10.5.8
    PHP 5.2.8

    Rob

  31. Had issues with the jpeg library and the gd- in each case I needed to get a more current build of the download packages. also had to use ‘sudo’ before ‘make install’ commands – just fyi for novices and noobs like me that could be a hitch.

    Thanks for the tutorial- Once I worked out those elements I was fine. We are lucky this page is here or it woulda been a lotta fishing out there!

  32. Pingback: Configure Debugging with PHPStorm | Margots Kapacs Blog

  33. Do you know how long I have been searching for a simple tutorial on how to install GD… I’ve been trying to get owncloud working on my server hosting http://metabiz.com.au forever and this did the trick – just wanted to say Thanks!

    Simon Maselli

  34. Pingback: Installing Html Tidy | Yetter Answers

  35. Pingback: Compiling PHP-GD on Mac OSX 10.5 | Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.