One of the great things about Mac OS X for developers?
The sheer amount of built-in, open source technologies available by default: Apache, Bash, Curl, Emacs, Nano, Perl, PHP, Python and Ruby to name but a handful.
In this article we'll guide you through bypassing the version of PHP installed by default on Mavericks and using a different binary instead.
Alternative PHP binaries
We're going to install php-osx, kindly provided for free by Liip, which offers a full feature set for development making it a very attractive alternative to the default version of PHP installed on Mavericks.
Ensure you are connected to the Internet before opening up your Terminal application (located in Applications/Utilities) and then issue the following command:
curl -s http://php-osx.liip.ch/install.sh | bash -s 5.6
Here we are installing the most current stable php-osx package available (version 5.6) from the Liip servers using Curl to the following location on your system:
You will then be prompted for your password by the installer and, within a few seconds after entering your password, the package will be successfully installed on your system.
If you type php -v on the command line you'll notice that the default Mac OS X version of PHP is still being returned and not the 5.6 version we just installed.
Configuring your system
The problem here is that although we installed a more up to date PHP binary Mac OS X doesn't know that it's supposed to use that instead of the default PHP installation.
Rectifying this is relatively simple but involves a bit more command line work.
The php-osx binary installer should have changed the path to the PHP installation that we want the Apache server to use.
To verify this is the case open the apache configuration file with the command line editor of your choice (we prefer nano):
And confirm that the following line exists and is uncommented:
LoadModule php5_module /usr/local/php5/libphp5.so
If so, then Apache knows to use the php-oxs installation instead of the default Mac OS X version of PHP.
If not then change the existing LoadModule php5_module declaration to the above, ensure the line is uncommented and save the file before exiting.
It's all in the profile
Now all that remains is to ensure that the path to the php-osx installation is available in your system shell profile.
To find which shell you are using, navigate to your console and execute the following command:
Which will display the path and name of the shell being used like so:
Now, navigate to your user home directory:
And locate the profile file for your shell (this should ALWAYS be in your user home directory):
You should, if you're using the bash shell, see, in the list that's displayed in the console, a file named .bash_profile.
If you don't, create the file using the following command:
Now you need to edit this file using a unix based text editor of your choice (I.e. vi, emacs, nano, joe etc):
If the file is new simply add the following declaration in at the top - otherwise add at the bottom of the file instead:
Save the file and, if you've made any edits to the Apache httpd.conf file, restart the server with the following command:
Now if you type php -v at the command line you should version 5.6 being displayed.
When it comes to upgrading your PHP installation you could have opted for a different solution such as MAMP or Laravel's Homestead for example. These would have offered a more complete solution including MySQL, Apache and related dependencies/additional software modules.
Whichever approach you choose is, of course, entirely up to you.