Taming MAMP on the command line (and upgrading PEAR)

MAMP is a great tool to run Apache, PHP and MySQL on your Mac but it doesn't always behave that well on the command line. This article will show you how to tame MAMP, upgrade PEAR and finally install that PHPUnit you've always wanted to!

MAMP is a great tool to easily run Apache, PHP and MySQL on your Mac. We use it here at Studio 24, along with local development Linux boxes, to develop client websites. MAMP packages everything up into a neat container keeping it separate from your Mac's default PHP binaries. The "pro" version has a great tool to add new hosts which avoids editing your /etc/hosts file by hand (very handy for designers!).

However, it doesn't always play that well on the command line. You need to fiddle to get Terminal to use the "right" PHP and recently I've had real issues using it to install PHPUnit via PEAR. This article will show you how to tame MAMP, upgrade PEAR and finally install that PEAR module you've always wanted to!

The problem

The PEAR that ships with MAMP is out of date and can cause issues when trying to install certain packages, i.e. PHPUnit. It's also a pain to upgrade.

The problem is caused by the /Applications/MAMP/bin directory. On more recent versions of MAMP you have the choice of PHP5.2 and 5.3 (in the /Applications/MAMP/bin/php5.2 and php5.3 folders). However, PEAR wants to install modules to a /Applications/MAMP/bin/php folder and everything gets a bit confused.

How to fix this

The first step is to ensure you don't already have a /Application/MAMP/bin/php folder. If you do, or if you don't have the /Applications/MAMP/bin/php5.2 or php5.3 folders, upgrade MAMP. This is easily done by downloading the latest version from http://www.mamp.info/ and installing. If you have MAMP Pro uninstall this first (drag to trash) before installing. This should retain your previous host file and config settings.

Next step is to link up terminal so the correct version of PHP is being used. This is easily achieved by adding a symlink. Pop open Terminal and write:

cd /Applications/MAMP/bin
ln -s php5.3 php5

You can swap php5.3 for php5.2 if you so wish though I'd recommend developing with PHP5.3 if you can host it. Next add this location, and the MySQL binary location, to your path like so:

sudo nano ~/.bash_profile

And enter the following content (all on one line):

export PATH=/Applications/MAMP/bin/php5/bin:/Applications/MAMP/Library/bin:$PATH

You need to close and re-open your terminal window for these settings to take effect. Now, you can type php and mysql safe in the knowledge you are using the right binary. Test this by typing:

which php

Upgrading PEAR

Next, you'll want to update PEAR. As of today (24/June/2011) the version of PEAR is 1.9.3.

sudo pear channel-update pear.php.net
sudo pear upgrade pear

# Find out what version of PEAR you are running
pear -V

Installing PHPUnit

Now if you really want to get going you can install PHPUnit with the following commands:

sudo pear channel-discover pear.phpunit.de
sudo pear channel-discover components.ez.no
sudo pear channel-discover pear.symfony-project.com

sudo pear install phpunit/PHPUnit

Switching versions of PHP on the command line

That's it. If you ever want to swap what PHP version is being used on the command line you can remove and re-create the symlink to /Applications/MAMP/bin/php

# Use PHP5.2 on the CLI
cd /Applications/MAMP/bin
rm php5
ln -s php5.2 php5

# Use PHP5.3 on the CLI
cd /Applications/MAMP/bin
rm php5
ln -s php5.3 php5

When re-creating the symlink there's no need to close and re-open terminal since your path environment variable hasn't changed, just the symlink.

Leave a comment