Recompiling PHP 5.3 On Snow Leopard With Freetype Support

OK, so this article probably doesn’t have a ton of mass appeal, but since upgrading to Snow Leopard I’ve run into a major issue with the way PHP is compiled in this distribution. Overall, I’m incredibly satisfied with the way everything’s set up by default in Snow Leopard (PHP 5.3 with bundled GD, mysqlnd, etc.), but the big glaring hole in everything was the lack of Freetype support. I’ve been working on a graphing library for my day job, and as such found the need to place text in my images (strange, right?). Anyway, I finally took the plunge and figured out how to get that sucker compiled in there. Here’s what you have to do…

Basically, what we’re going to do is recompile PHP 5.3 exactly the same as it came, but with proper Freetype support. Before we get into anything, here’s a few disclaimers:

  • This worked for me… it may not work for you. If you never make it to the “make install” step of the PHP build, you won’t screw up your system so you can feel relatively safe trying this. However, I can’t support you if something goes wrong. Don’t worry tho, you shouldn’t run into many issues :)
  • The steps in this tutorial are munged together from various other sources on the web. I’d like to credit these people if possible, but I don’t have a list of all my various sources… if your stuff is on here, lemme know and I’ll make sure you’re properly credited and linked to :)
  • You MUST have the xcode developer tools installed for any of this to work. You’ll know right away if you don’t, as “make” will be an unknown command for you. These are on your Snow Leopard install DVD if you don’t have them…

OK, moving on…

Download Source Files

We’re going to need to grab a few things to get started. So, fire up a terminal, and type in the following:

sudo mkdir /src
sudo chown [your username]:staff /src
cd /src
mkdir pcre
mkdir php
cd pcre
curl ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz -O
cd ../php
curl http://www.opensource.apple.com/source/apache_mod_php/apache_mod_php-53/php-5.3.0.tar.bz2 -O

What we’ve got is the PHP 5.3 package from Apple, and the PCRE library… everything else we need is already on our system. Let’s get compiling…

Compile PCRE

We need to compile PCRE into a local directory (not system-wide) so the PHP compiler has access to the headers it will need. This is pretty easy, just run the following:

cd /src/pcre
tar -zxvf pcre-7.7.tar.gz
cd pcre-7.7
./configure --disable-shared --enable-static
make && make install DESTDIR=/src/pcre/pcre-local

That’s it! Now we can start playing around with PHP

Compile PHP

Basically, what we’re going to be doing here is recompiling PHP 5.3 with the same config parameters as it was originally built with, but changing the things we need for GD and PCRE. It’s important that you follow all the steps outlined here, or you’ll get some weird compile errors. Also, if you’re a bit uncomfortable about recompiling, remember that you’re not going to mess anything up until you actually run the “make install” command. So, if the compile fails for any reason, your current PHP install isn’t going to get messed up in any way.

So, let’s get started:

cd /src/php
bunzip2 php-5.3.0.tar.bz2 && tar -xvf php-5.3.0.tar
cd php-5.3.0

Now, we’ll need to edit one file to fix a compile error we’ll run into. Open /src/php/php-5.3.0/ext/iconv/iconv.c in your editor of choice, and head to line 186. We need to change the line from:
“#define iconv libiconv”
to
“#define iconv iconv”

Once you’ve done that, we’re good to go. Assuming you’re still in /src/php/php-5.3.0:

MACOSX_DEPLOYMENT_TARGET=10.6
CFLAGS="-arch x86_64 -g -Os -pipe -no-cpp-precomp"
CCFLAGS="-arch x86_64 -g -Os -pipe"
CXXFLAGS="-arch x86_64 -g -Os -pipe"
LDFLAGS="-arch x86_64 -bind_at_load"
export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET
./configure --prefix=/usr \
--mandir=/usr/share/man \
--infodir=/usr/share/info \
--disable-dependency-tracking \
--sysconfdir=/private/etc \
--with-apxs2=/usr/sbin/apxs \
--enable-cli \
--with-config-file-path=/etc \
--with-libxml-dir=/usr \
--with-openssl=/usr \
--with-kerberos=/usr \
--with-zlib=/usr \
--enable-bcmath \
--with-bz2=/usr \
--enable-calendar \
--with-curl=/usr \
--enable-exif \
--enable-ftp \
--with-gd \
--with-jpeg-dir=/usr/local/lib \
--with-png-dir=/usr/X11R6 \
--with-freetype-dir=/usr/X11R6 \
--with-xpm-dir=/usr/X11R6 \
--with-ldap=/usr \
--with-ldap-sasl=/usr \
--enable-mbstring \
--enable-mbregex \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysql-sock=/tmp/mysql.sock \
--with-iodbc=/usr \
--enable-shmop \
--with-snmp=/usr \
--enable-soap \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--with-xmlrpc \
--with-iconv-dir=/usr \
--with-xsl=/usr \
--with-pcre-regex=/src/pcre/pcre-local/usr/local

If everything completed without error, you’re ready to compile (again, if this fails, nothing is messed up with your current PHP installation):

export EXTRA_CFLAGS="-lresolv"
make

This will take a few moments, so go grab a coffee or something ;) If this step completed properly (i.e you get some message about “don’t forget to run make test”), you’re ready to finish up. One thing I like to do is back up my current php.ini file, but if you haven’t tweaked it (or created it for that matter), you don’t need to do this:

sudo cp /etc/php.ini /etc/php.ini.bak

Now, you can install our fresh PHP build:

sudo make install

That’s it! Now, you can copy your old php.ini file back, and restart apache:

sudo cp /etc/php.ini.bak /etc/php.ini
sudo apachectl restart

If you set up a phpinfo() somewhere in your doc root, you should now see something like the following in the GD section of it:
PHP 5.3 GD with Freetype

Enjoy ;)

Other's Thoughts   (24 so far...)


  • bugi
    Oct 6 '09 at 11:26 pm

    Thanks Ian,
    just what I needed. :)

    I had to install libjpeg before your solution did work

    cd /src
    mkdir libjpeg
    cd libjpeg
    curl http://www.opensource.apple.com/source/apache_mod_php/apache_mod_php-53/libjpeg/jpegsrc.v6b.tar.gz -O
    tar xzf jpegsrc.v6b.tar.gz
    cd jpeg-6b/
    ln -s `which glibtool` ./libtool
    ./configure –enable-shared && make && sudo make install

    (last two lines google’d from http://tomster.org/blog/installing-libjpeg-on-os-x/?searchterm=cd)


  • hafiz
    Oct 7 '09 at 5:42 am

    If anyone get any error like below (libiconv) while compiling

    ====================
    Undefined symbols:
    “_libiconv_open”, referenced from:
    _do_convert in gdkanji.o
    __php_iconv_strlen in iconv.o
    _php_iconv_string in iconv.o
    __php_iconv_strpos in iconv.o
    _zif_iconv_substr in iconv.o
    _zif_iconv_substr in iconv.o
    __php_iconv_mime_decode in iconv.o
    __php_iconv_mime_decode in iconv.o
    _zif_iconv_mime_encode in iconv.o
    _zif_iconv_mime_encode in iconv.o
    _php_iconv_stream_filter_factory_create in iconv.o
    _convert in encodings.o
    “_res_9_dn_expand”, referenced from:
    ==========================

    1. do check the libiconv inside /usr/lib, or /usr/local/lib,
    you may have more libiconv or old libiconv libraries installed.

    2. Check you search path.

    3. Download the latest libiconv from their website.


  • Ian
    Oct 7 '09 at 8:17 am

    @hafiz,

    You can get those errors if you don’t edit the libiconv.c file per my instructions as well… Seems like your solution might be the better route, but the little edit does the trick as well :)


  • Ryan Parman
    Oct 7 '09 at 11:11 am

    Personally, I prefer to just use MacPorts. It makes for a much cleaner install. http://j.mp/11pULu


  • ricbax
    Oct 15 '09 at 5:20 am

    @hafiz
    I took 1,2, and 3 into consideration and I get the following which is slightly different from what you posted:
    ===============================
    Undefined symbols:
    “_iconv”, referenced from:
    __php_iconv_appendl in iconv.o
    __php_iconv_appendl in iconv.o
    _php_iconv_string in iconv.o
    _php_iconv_string in iconv.o
    __php_iconv_strlen in iconv.o
    __php_iconv_substr in iconv.o
    __php_iconv_strpos in iconv.o
    __php_iconv_mime_encode in iconv.o
    __php_iconv_mime_encode in iconv.o
    __php_iconv_mime_encode in iconv.o
    __php_iconv_mime_encode in iconv.o
    __php_iconv_mime_encode in iconv.o
    __php_iconv_mime_encode in iconv.o
    _php_iconv_stream_filter_append_bucket in iconv.o
    _php_iconv_stream_filter_append_bucket in iconv.o
    _php_iconv_stream_filter_append_bucket in iconv.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    make: *** [libs/libphp5.bundle] Error 1
    ===============================


  • Simon Birve
    Oct 16 '09 at 6:48 am

    Thanks Ian for this article. It saved me.
    I had to install libjpeg7 before compiling php despite the change in my inconv.c file.
    This is what i did.

    cd /src
    mkdir libjpeg
    cd libjpeg
    Downloaded jpegsrc.v7.tar.gz from Freashmeat (http://freshmeat.net/projects/libjpeg/) to /libjpeg
    cd jpeg-7
    tar xzf jpegsrc.v6b.tar.gz
    cp /usr/share/libtool/config/config.sub .
    cp /usr/share/libtool/config/config.guess .
    -
    export MACOSX_DEPLOYMENT_TARGET=10.6
    CFLAGS=”-arch x86_64″ \
    CXXFLAGS=”-arch x86_64″ \
    LDFLAGS=”-arch x86_64″ \
    ./configure –enable-shared
    -
    make
    sudo make install

    (the flags from http://diymacserver.com/installing-php/adding-the-gd-module-to-php-on-snow-leopard/)
    I the run command from “Compile PHP” above.

    Worked like a charm and i now have GD as you shown above.
    /s


  • Simon Birve
    Oct 16 '09 at 6:59 am

    Sorry some typo above

    cd libjpeg
    Downloaded jpegsrc.v7.tar.gz from Freashmeat (http://freshmeat.net/projects/libjpeg/) to /libjpeg
    tar xzf jpegsrc.v7.tar.gz
    ….
    /S


  • ivanjovanovic
    Oct 22 '09 at 10:38 am

    I had to build the libjpeg as well before the building of php.
    As I read in the PHP manual, pcre is bundled now and you can just skip the last line of the configuration parameters and building pcre also.


  • Jose Antonio Andujar
    Oct 29 '09 at 12:30 am

    Hello,
    I had some problems. I need to compile the jpeg-6b like bugi, modify some header for type int64 and uint64 and compile with this. (jpeg and php)
    MACOSX_DEPLOYMENT_TARGET=10.6
    CFLAGS=”-arch x86_64 -arch i386 -g -Os -pipe -no-cpp-precomp”
    CCFLAGS=”-arch x86_64 -arch -g -Os -pipe”i386
    CCFLAGS=”-arch x86_64 -arch i386 -g -Os -pipe”
    CXXFLAGS=”-arch x86_64 -arch i386 -g -Os -pipe”
    LDFLAGS=”-arch x86_64 -arch i386 -bind_at_load”
    export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET

    I’m buildin on a mac mini intel core


  • lunix
    Nov 2 '09 at 4:53 am

    Great article, and thanks simon for the tip :)


  • Eric Newman
    Nov 2 '09 at 5:27 am

    Beware of funny quote signs, and missing, or malformed dash characters above. The gist of this is correct, but don’t just copy and paste it.


  • David Nash
    Nov 19 '09 at 3:27 am

    Excellent ‘howto’ – Thanks for the help


  • Jeff Barlow
    Nov 25 '09 at 4:22 pm

    If you get the following errors:

    php5.so
    ld: warning: in /usr/local/lib/libpng.dylib, file is not of required architecture
    Undefined symbols:
    “_png_set_PLTE”, referenced from:
    _php_gd_gdImagePngCtxEx in gd_png.o
    “_png_set_packing”, referenced from:
    _php_gd_gdImagePngCtxEx in gd_png.o
    _php_gd_gdImageCreateFromPngCtx in gd_png.o
    “_png_read_update_info”, referenced from:
    _php_gd_gdImageCreateFromPngCtx in gd_png.o
    “_png_get_tRNS”, referenced from:
    _php_gd_gdImageCreateFromPngCtx in gd_png.o
    _php_gd_gdImageCreateFromPngCtx in gd_png.o
    _php_gd_gdImageCreateFromPngCtx in gd_png.o

    “_png_get_PLTE”, referenced from:
    _php_gd_gdImageCreateFromPngCtx in gd_png.o
    “_png_create_info_struct”, referenced from:
    _php_gd_gdImagePngCtxEx in gd_png.o
    _php_gd_gdImageCreateFromPngCtx in gd_png.o
    “_png_set_IHDR”, referenced from:
    _php_gd_gdImagePngCtxEx in gd_png.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    make: *** [libs/libphp5.bundle] Error 1

    You have to recompile libpng. I did:
    cd ..
    mkdir libpng
    cd libpng
    curl http://www.opensource.apple.com/source/apache_mod_php/apache_mod_php-53/libpng/libpng-1.2.37.tar.bz2 -O
    tar xzf libpng-1.2.37.tar.bz2
    cd libpng-1.2.37
    ln -s `which glibtool` ./libtool
    ./configure -enable-shared && make && sudo make install

    and things worked like a charm! Thanks to all for your help!


  • Kurt Collins
    Jan 10 '10 at 4:19 pm

    Thank you so much for this. :-)


  • Galen Sprague
    Jan 13 '10 at 1:00 pm

    This is great thank you very much! I also had to install libjpeg with the same flags as the php install to get it to compile. Working great now!!! Thanks again, this is awesome!!!!


  • Randy Hobart
    Jan 17 '10 at 6:45 am

    Thank you for your time in writing these instructions.
    I was successful in the recompile and works great.
    Recompiled on the MacBook I just purchase 1 week ago.

    Had to compile the jpegsrc.v7.tar.gz as above.
    —–
    One big change is that the below file is no longer there.
    curl ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz -O
    It is now ——
    curl ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.9.tar.gz -O
    —–
    It took about 4 times to get it right.
    I found out you can copy the configure settings straight from you black box above.
    This saved alot of time.
    ——
    Thanks again, my php is running great now.
    And thanks for the comments given by others, could not have done it without all this help.


  • Mike Kormendy
    Jan 23 '10 at 4:13 am

    For myself as well, I had to install libjpeg7 before compiling PHP.
    In some cases, please be aware of your compiling for 64bit or 32bit as discussed in this post:
    http://diymacserver.com/installing-php/adding-the-gd-module-to-php-on-snow-leopard/

    1. download source files (with corrected pcre ver 7.9 & php)
    2. compile your PCRE
    3. download source files (libjpeg7)
    4. compile libjpeg7
    5. compile php

    Here are Simon Birve’s steps, CORRECTED, in one go (for 64bit):

    cd /src
    mkdir libjpeg
    cd libjpeg
    curl http://www.ijg.org/files/jpegsrc.v7.tar.gz -O
    tar xzf jpegsrc.v7.tar.gz
    cd jpeg-7
    cp /usr/share/libtool/config/config.sub .
    cp /usr/share/libtool/config/config.guess .
    export MACOSX_DEPLOYMENT_TARGET=10.6 \
    CFLAGS=”-arch x86_64″ \
    CXXFLAGS=”-arch x86_64″ \
    LDFLAGS=”-arch x86_64″
    ./configure –enable-shared
    make
    sudo make install


  • Mike Kormendy
    Jan 23 '10 at 4:40 am

    Also wanted to mention something about plugging in mcrypt (useful for latest version phpmyadmin) for php as discussed here:

    http://michaelgracie.com/2009/09/23/plugging-mcrypt-into-php-on-mac-os-x-snow-leopard-10.6.1/

    Recompiling PHP will NOT affect a previous mcrypt plug-in installation. :-)

    I am running a 64bit Xserve with Snow Leopard 10.6.2, and the above process with pcre7.9 and libjpeg7 worked flawlessly for me and maintains mcrypt.

    Thanks to all who contributed.


  • Dae
    Jan 23 '10 at 6:31 am

    The link to PCRE 7.7 didn’t work, so I took the link to PCRE 7.9 from the comment above, but make failed with the following:
    =======
    Dae-MacPro:pcre-7.9 Dae$ make
    make all-am
    /bin/sh ./libtool –tag=CC –mode=compile gcc -DHAVE_CONFIG_H -I. -arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp -MT pcre_compile.lo -MD -MP -MF .deps/pcre_compile.Tpo -c -o pcre_compile.lo pcre_compile.c
    gcc -DHAVE_CONFIG_H -I. -arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp -MT pcre_compile.lo -MD -MP -MF .deps/pcre_compile.Tpo -c pcre_compile.c -o pcre_compile.o
    gcc-4.2: -E, -S, -save-temps and -M options are not allowed with multiple -arch flags
    make[1]: *** [pcre_compile.lo] Error 1
    make: *** [all] Error 2
    =======

    I didn’t fix the problem: instead I took PCRE 7.7 from here:
    http://downloads.sourceforge.net/project/pcre/pcre/7.7/pcre-7.7.tar.gz

    Make worked like a charm with the older version.


  • Ron
    Jan 31 '10 at 2:13 pm

    Does anyone know if this works for OS X Snow Leopard Server as well?


  • RS
    Feb 1 '10 at 10:22 pm

    Anyone try this with PHP 5.3.1? I’m going to give it a shot tomorrow, but thought I’d ask before potentially wasting an afternoon. =P


  • RS
    Feb 3 '10 at 7:59 pm

    So, this does work with PHP 5.3.1 directly downloaded from php.net. I did have to download pcre8.01, as well as jpeg7. But, it’s working with 10.6.2 server.


  • Mike Kormendy
    May 9 '10 at 10:32 pm

    Okay all, I have an update for those with the following system:

    OS X Server 10.6.3
    PHP 5.3.1
    PCRE8.02
    LIBJPEG8a

    I did not have any hiccups with my install from the above steps, however make sure:
    - that you are compiling for the right architecture
    - that you do not copy and paste from this page into your terminal shell

    The double quotes have been converted in the comments section on this site and cause problems with copy and paste.

    @Dae: did you run as sudo su? that might fix the problem
    @Ron: this has worked for my SL Server 10.6.3
    @RS: alternatively you can download from Apple’s repository: http://www.opensource.apple.com/

  • Share Your Thoughts...

    Some HTML is ok. If this is your first comment on my site, it will be reviewed before being posted publicly. Your comment may be edited or marked as spam if it appears intended for SEO purposes.