Howto: Install PHP 5.5 and PHPUnit on OS X via Homebrew

Here is how to install the latest PHP and PHPUnit versions on OS X using , which became my first choice after testing other package managers.

To avoid conflicts, it is generally not recommended to use Homebrew alongside other external OS X package managers such as MacPorts or Fink. You can remove MacPorts with these Terminal commands:

sudo port -f uninstall installed
sudo rm -rf /opt/local
sudo rm -rf /Applications/DarwinPorts

If you prefer to stay with MacPorts: There is a blog post with a detailed howto for PHP 5.5. The respective uninstall commands for Fink are (untested):

fink remove --recursive daemonic xinitrc
sudo rm -rf /sw

In case you don’t have Homebrew installed yet, it’s as easy as that:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
brew doctor # checks your installation and gives advice how to fix it

Make sure to have Xcode and (!) the Command Line Tools of Xcode installed (see screenshot) and then execute the following commands to download & install PHP 5.5 (takes a little while):

rm -rf ~/.pearrc # remove existing PHP/PEAR config files
brew update
brew upgrade
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
brew install freetype jpeg libpng gd zlib openssl unixodbc
brew install php55

Edit the PATH environment variable in your .bash_profile (might not exist yet) and restart your terminal session/tab for the changes to become effective:

nano ~/.bash_profile
export PATH="$(brew --prefix josegonzalez/php/php55)/bin:/usr/local/bin:$PATH"

Afterwards, you can easily install the latest PHPUnit release via PEAR (the PHP package manager):

sudo pear config-set auto_discover 1
sudo pear install pear.phpunit.de/PHPUnit

Additional changes are needed to enable PHP 5.5 in Apache – OS X currently ships with 5.3 which will not be uninstalled/replaced by the previous steps. You can check the versions of the installed packages like this:

[MacBook-Air]# /usr/bin/php -v # OS X PHP
PHP 5.3.15 with Suhosin-Patch (cli) (built: Aug 24 2012 17:45:44)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

[MacBook-Air]# php -v # Homebrew PHP
PHP 5.5.1 (cli) (built: Aug 21 2013 16:26:25)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies

[MacBook-Air]# phpunit --version
PHPUnit 3.7.24 by Sebastian Bergmann.

Note that Suhosin is currently not available for PHP 5.5.