Managing multiple versions of PHP on Mac OS X

I feel a trend starting. A few weeks ago, I wrote a post about managing different versions of Python on Mac OS X, and today I’m going to explain how I use multiple versions of PHP on the same platform.

Until the homebrew-php group figure out a better way of handling several versions of PHP (their instructions haven’t worked for several months), without installing something like phpenv, I concocted my own means using bash aliases similar to my Python post.

(Command line output that I’ve pasted below may be truncated for brevity.)

Step 1: Install your favorite versions of PHP

Homebrew is still awesome at installing PHP, but trying to install multiple versions back to back can give you errors:

$ brew tap homebrew/dupes
$ brew tap homebrew/versions
$ brew tap homebrew/homebrew-php
$ brew install php{5{5,6},7{0,1}}
Error: Cannot install homebrew/php/php56 because conflicting
formulae are installed.
  php55: because different php versions install the same binaries.
Please `brew unlink php55` before continuing.


There’s an easy way around it:

$ for i in 55 56 70 71; do brew install php$i; brew unlink php$i; done

This will install PHP 5.5, 5.6, 7.0 and 7.1, unlinking each version right after installation, and leaving you with PHP 7.1 as your system default.

Step 2: Bash aliases

I put all of my aliases in a file and call that from within my .bashrc file:

# ~/.bashrc
if [ -f ~/ ]; then
  . ~/
# ~/
function unlink_php {
  for i in `ls -1 /usr/local/opt/ | grep php | cut -c4-5 | uniq`; do
    brew unlink php$i > /dev/null
for i in `ls -1 /usr/local/opt/ | grep php | cut -c4-5 | uniq`; do
  alias php$i="unlink_php && brew link php$i"

The unlink_php function basically autodetects which versions of PHP you have installed with homebrew, and unlinks all of them. I’m sure you could be clever and detect via “php –version” which version is currently linked and just unlink that one, but this gives me peace of mind that ALL versions are properly unlinked.

Then, it uses the same autodetection to create aliases for each version which also calls the unlink_php function.

Now you can ‘source’ that aliases script and you should be in business:

source ~/

Step 3: Profit

Verify that your new aliases are made:

$ alias | grep php
alias php55='unlink_php && brew link php55'
alias php56='unlink_php && brew link php56'
alias php71='unlink_php && brew link php71'

And then you can switch your version of PHP using your alias:

$ php55
Linking /usr/local/Cellar/php55/5.5.38_11... 17 symlinks created
$ php --version
PHP 5.5.38
$ php71
Linking /usr/local/Cellar/php71/7.1.0-rc.3_8... 17 symlinks created
$ php --version
PHP 7.1.0RC3

Leave a Reply