Daniel Kreuer

Daniel Kreuer

Professional Web Developer, Software Architect and Dev-Op

Running multiple PHP versions under OS X 10.11 El Capitan

osx php elcapitan


OS X 10.11 (El Capitan) comes pre-installed with PHP 5.5.29. You might think that's all one needs but in fact PHP 5.5 reaches end-of-life mid-2016. It only gets security fixes since july 2015. See PHP Supported Versions for more details. So updating the PHP version is crucial.

But there is another reason to have multiple versions of PHP on your development machine: Think about testing your projects and libraries against several PHP versions. Sure you could run a set of Vagrant machines to handle this case (see PuPHPet for more information). But most times this is overkill either because you have limited resources (anyone using a MacBook or MacBook Air?) or you just have to run a bunch of tools for unit or behavior testing.

So here are the instructions to prepare your machine to install and run many different PHP versions beginning from PHP 5.5 to PHP 7.0.

Preparing your system: Install XCode CLI Tools

To compile the several packages required for PHP you will need the XCode command line tools. First you have to install the latest XCode from the Apple App Store (which is free).

Afterwards you can install the command line tools with a simple command and after some minutes of waiting you are good to go:

$ xcode-select --install

Preparing your system: Install Homebrew

First you have to install a bunch of dependencies required to compile PHP. To make this happen we use homebrew the missing package manager for OS X, as they say.

Homebrew is easily installed with a one-line command (security put aside you must trust the maintainer of the install script or analyze it).

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

For more documentation on installing and using homebrew please read the official documentation.

Preparing your system: Install dependencies

After installing homebrew you are prepared to install the dependencies required to build PHP (5.6.x at least).

The dependencies required for PHP are libpng, mcrypt, jpg, libxml2 and openssl098. For the xdebug extension additionally autoconf and automake are required.

You can install all dependencies at once with the following command:

$ brew install libpng mcrypt jpeg libxml2 openssl098 autoconf automake

Some other dependencies for the aformentioned dependencies might be installed as well.

Preparing your system: Install phpenv + php-build

After you have installed all dependencies clone the php-env repository into your home directory.

$ cd
$ git clone https://github.com/madumlao/phpenv.git .phpenv

Phpenv requires php-build as install helper so we are going to install it too.

$ cd ~/.phpenv/plugins
$ git clone https://github.com/php-build/php-build php-build

Now the openssl dependency might not be found in future steps so we modify the file /.phpenv/plugins/php-build/share/php-build/default_configure_options to contain the path to openssl 0.98 which should be /usr/local/Cellar/openssl098/0.9.8zg. The file should now look like the following excerpt:

# ...
--with-kerberos
--with-openssl=/usr/local/Cellar/openssl098/0.9.8zg
--with-mcrypt=/usr
# ...

You are now ready to install PHP!

Installing PHP 5.6.15

And installing PHP is easy. Just run the following command and after a while you will have a ready-to-use PHP in your home directory!

$ phpenv install 5.6.15
$ phpenv rehash

You can now explicitly call the installed PHP with ~/.phpenv/versions/5.6.15/bin/php -v which should result in the following output:

PHP 5.6.15 (cli) (built: Nov 16 2015 08:29:18) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
    with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans

For more info how to switch the php version globally or add the apache module to your webserver please read the documentation of phpenv.

Installing PHP 7.0

PHP 7.0 was released on 2015-Dec-03. If you have installed php-build previously (which I assume you have not because this blog post is newer) you must first update the php-build environment.

$ phpenv update

Afterwards you are well prepared to install PHP 7.0.0, the greatest release of PHP in years!

$ phpenv install 7.0.0
$ phpenv rehash

After a well-prepared cup of coffee (or tea if you prefer) the installation is finished and with ~/.phpenv/versions/7.0.0/bin/php -v you have invoked PHP successfully:

PHP 7.0.0 (cli) (built: Dec  4 2015 20:05:04) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
    with Xdebug v2.4.0RC3-dev, Copyright (c) 2002-2015, by Derick Rethans

Have fun!