Using different PHP versions on OSX with Apache and Homebrew


Posted by Andrew McCombe on Wed Feb 4, 2015

By default Apple’s OSX (Mavericks) comes with PHP 5.4 installed but if you want to work on a different version you’re pretty stuck.

My solution to this is to use homebrew to install PHP5.3, 5.4, 5.5 and 5.6 and then simply change the PHP module loaded by apache. Here’s how to do it.

Step 1: Install Homebrew

Go to http://brew.sh/ and follow the instructions.

Step 2: Install the different PHP versions

You may also need to install version specific PHP modules. To find these you should use homebrew’s search feature. For example, to install the Mcrypt module:

Install these for the PHP version you need (or all of them is so required)

Step 3: Switching PHP versions for command line usage

Note: If you only develop in the browser you don’t need to do this, you can simply switch the Apache PHP modules as show in step 4.

To switch between versions you should use the link and unlink features of homebrew. For example, to switch from the current PHP version to php5.5:

Step 4: Using different PHP version in Apache

Edit /etc/apache2/httpd.conf and look for the line that loads the PHP module. On my installation this is around line 120. Duplicate the current line and change the path to the module for the version of PHP yopu want to use. Comment out the other versions (using the hash key ‘#’). Here’s my current Apache config file:

As you can see, it is currently set to use PHP5.4.36.

Once you’ve set up the module you want, you will need to restart the Apache Web Server:

Verify this is correct by creating a PHP file in your web root with the call to phpinfo() and then visiting the page: