Monthly Archives: June 2013

Using PHP from command line on OSX

It’s no secret, OSX is the best operating system for personal computing. The barrage of haters will tell you it’s for soy decafe mocha latte sipping hipsters; but they’re misinformed luddites. Nevermind, we’ll let them live in their world of fantasy and continue powering on with our proper shell, Unix-style system, our anti-aliasing and Helvetica.

By default, OSX comes with PHP pre-installed. However, this probably isn’t the version you’re developing with.

I’m concentrating on a development environment that uses MAMP as its localhost development server. It’s quite popular, and saves a lot of compiling and messing around. However, if you’re using a custom compiled version of PHP with Apache, but your command line shows a different version being used, you can use similar techniques to those described below.

Step 1 – Establish the version of PHP being used by CLi

Jump into your terminal (my favourite being iTerm 2) and fire off the command

$ php --version

You should receive a response along the lines of

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

Step 2 – Determine the version of PHP being used by MAMP

Pretty straight forward, just open up MAMP and hit the “Preferences” button.

MAMP_and_Edit_Post_‹_Josh_Ellis_—_WordPress
Then you’ll see something along these lines.

MAMP-2
If these two match… you’re winning at life. Crack a beer and have a celebratory nap for the afternoon.

Step 3 – Find the path to your PHP binaries

MAMP, by default, will have installed PHP in

/Applications/MAMP/bin/php/php5.X.X/bin

Open finder and make sure of the available PHP versions (based on the preferences above). Once you’ve successfully determined the name of the folder, pop the following into your terminal.

$ sudo nano ~/.bash_profile

Fear not if this file has content in it already. Just use the down arrow and go to the bottom of the file. If the file doesn’t exist, it will be created when we save it on exit. :-)
Add the following line at the bottom of your file

export PATH="/Applications/MAMP/bin/php/php5.X.X/bin:$PATH"

Please ensure you replace the “X.X” with your relevant version! Then hit Ctrl + X… type “Y”, then hit enter. This line prepends your system path with the directory for the PHP binaries. By PREpending, the system starts at the beginning of these arguments (generally separated by a colon “:”) and goes until it finds a suitable match for whatever command is attempting to be executed.
Confirm your file has been saved by typing

$ cat ~/.bash_profile

If something has gone wrong… repeat the last couple of steps.
Following this, we need to make sure the file is executable. This last command should ensure the permissions are correctly set on the file.

$ chmod u+x ~/.bash_profile

The last thing to do is restart your terminal, then fire off

$ echo $PATH

All going well you’ll see your newly prepended directory to your PHP binaries. You can confirm it’s now running the correct version by typing

$ php --version

Your expected output is

PHP 5.4.4 (cli) (built: Jul  4 2012 17:28:56)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with XCache v2.0.0, Copyright (c) 2005-2012, by mOo

You’re welcome.