(Re)installing PHP 5.3 on Mac OS X

So you develop PHP 5.3 on a Mac? Mebbe you, like me, are a fan of Symfony2? Then you might have noticed a few deficiencies in the pre-loaded version of PHP 5.3 on your computer. Here’s how to …

Fix PHP 5.3 on OS X

First you’ll need Homebrew1. If you’re not already using it, what are you waiting for?

tl;dr (just give me something to copy and paste)

If you’re impatient, copy and paste this gist into a Terminal.

Otherwise, you’ll want to use one of the following options to install my Homebrew formula for PHP 5.3

PHP needs autoconf, and Apple no longer ships it with Xcode…

Check whether you need to install it:

which autoconf

Unless that told you about a magical autoconf instance somewhere, you’ll need to install it:

brew install autoconf

Most of you will want MySQL and Apache support

brew tap josegonzalez/homebrew-php
brew install php --with-mysql

Symfony2 developers will need to build PHP 5.3 with i18n support

brew tap josegonzalez/homebrew-php
brew install php --with-mysql --with-intl

By default this formula comes with readline

Because honestly, who wants to run a php -a interactive shell without real interaction? If you’re masochistic, you can disable readline by adding the --without-readline flag.

But wait, there’s more!

This Homebrew formula can do PHP FPM, MSSQL and Postgres as well. Here are all the options available:

--with-mysql        Include MySQL support
--with-mariadb      Include MariaDB support
--with-pgsql        Include PostgreSQL support
--with-mssql        Include MSSQL-DB support
--with-cgi          Enable building of the CGI executable (implies --without-apache)
--with-fpm          Enable building of the fpm SAPI executable (implies --without-apache)
--without-apache    Build without shared Apache 2.0 Handler module
--with-intl         Include internationalization support
--with-imap         Include IMAP extension
--without-readline  Build without readline support
--with-gmp          Include GMP support
--with-suhosin      Include Suhosin patch

Using your fresh copy of PHP 5.3

You might have missed it, but Homebrew spit out a couple of caveats at the end of the install:

For 10.5 and Apache:
    Apache needs to run in 32-bit mode. You can either force Apache to start
    in 32-bit mode or you can thin the Apache executable.

To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php5_module    /usr/local/Cellar/php/5.3.10/libexec/apache2/libphp5.so

The php.ini file can be found in:
    /usr/local/etc/php.ini

If you haven’t already, follow that advice

If you have any custom settings in your system-wide php.ini, copy them to the new one in /usr/local/etc.

Fix the PEAR config and upgrade

pear config-set auto_discover 1
pear update-channels
pear upgrade

You’ll need to reinstall PECL packages now (and whenever you update PHP)

Like all good homebrew setups, you won’t need sudo for PEAR anymore. Your needs will vary, but since this is my article I’ll just leave these here so I can copypaste them later:

pecl install apc-beta mongo memcache

Because PEAR is installed in the Homebrew prefix, you’ll need to reinstall all of your PEAR libraries as well

Here’s what I use:

pear install pear.phpunit.de/PHPUnit
pear install PHP_CodeSniffer-beta

After installing PEAR libraries with executables (like phpunit) you’ll need to get them into your path. Either add the PHP bin directory to your path (e.g. in ~/.bashrc):

export PATH="$(brew --prefix php)/bin:$PATH"

… or symlink PHP binaries after running pear install ...:

brew link php

Of course, there might be some gotchas

Offhand, I can think of one — In order to work with things like the Symfony2 console and PHPUnit, your new PHP should come before the pre-installed PHP in your $PATH. Check it like this:

which php

In my case, I see this:

/usr/local/bin/php

If my $PATH were incorrect, I’d see this instead:

/usr/bin/php

To fix it, you can add something like this to your .bashrc:

export PATH="$(brew --prefix)/bin:$PATH"

Now go build something cool!

  1. If you already have Homebrew installed, run brew update to make sure you have the latest hotness.

  2. Remnants of older PEAR will keep the PHP installer from installing a new copy of PEAR, which, it turns out, is a Bad Thing.