by Sally Young on January 17, 2012 // Short URL

Installing PHP PEAR and PECL extensions on MAMP for Mac OS X 10.7 (Lion)

MAMP is an easy to install set of tools for that provides a great local development environment for Mac users (Mac Apache MySQL PHP). OS X does ship with its own built in versions of these tools, but MAMP adds lots of goodies. The newest version of MAMP includes a graphical configuration tool, a choice of PHP versions, web-based tools for administering your databases, and a variety of useful PHP extensions.

Unfortunately, MAMP doesn't include everything. If you're developing a site that needs additional PHP extensions like the Memcached library or exotic database drivers, you'll need to use the PEAR and PECL tools to build and install them. PECL (PHP Extension Community Library) and PEAR (PHP Extension and Application Repository) both provide access to a large pool of useful PHP add-ons, but using them with MAMP requires you to do a bit of extra configuration. In this article, you'll learn how to get them running on the latest version of MAMP (2.x).

Installing Components

The usual method for installing PEAR and PECL components is via the command line. Here is an example for installing PHPUnit.

$ pear channel-discover pear.phpunit.de
$ pear install phpunit/PHPUnit

Easy! Unfortunately, if we try this after we've installed MAMP we'll get an error as the "pear" command can't be found and running "php" will point us to OS X's default version.

$ pear
-bash: pear: command not found

$ which php
/usr/bin/php

Let's Fix It

To rectify this we need to add PHP's binaries to our path. The path is an environment variable that denotes which directories to look for commands in. The path can be modified by editing the ".profile" file under your home directory. I've used version 5.3.6 of PHP, but you can choose from whichever versions are available to you.

$ echo "export PATH=/Applications/MAMP/bin/php/php5.3.6/bin:$PATH" >> ~/.profile

Now you can either restart your terminal session or run the following from your home directory to read in the new path variable.

$ . ./.profile

You should now have the correct binaries on your path and can check this by running the "which" command again.

$ which php
/Applications/MAMP/bin/php/php5.3.6/bin/php

$ which pear
/Applications/MAMP/bin/php/php5.3.6/bin/pear

$ which pecl
/Applications/MAMP/bin/php/php5.3.6/bin/pecl

If this doesn't work make sure that your PHP binaries have execute permissions, an easy way to tell is by running

$ /Applications/MAMP/bin/php/php5.3.6/bin/php -v
-bash: /Applications/MAMP/bin/php/php5.3.6/bin/php: Permission denied

$ chmod +x /Applications/MAMP/bin/php/php5.3.6/bin/*

Try It Out

$ pear channel-discover pear.drush.org
Adding Channel "pear.drush.org" succeeded
Discovery of channel "pear.drush.org" succeeded

$ pear install drush/drush
downloading drush-4.5.0.tgz ...
Starting to download drush-4.5.0.tgz (281,392 bytes)
.........................................................done: 281,392 bytes
install ok: channel://pear.drush.org/drush-4.5.0

Hurray! Good times.

PECL

For PECL to work we need to prepare the build environment for extensions by making the PHP source available. Download the MAMP Server components and libraries zip file and make yourself a cup of tea whilst you wait. Perhaps have a biscuit as well, I recommend the classic custard cream. You'll also need to have Xcode installed to get Autotools.

Once it's downloaded, find the zip file corresponding to your version of PHP, extract it into a new directory in your PHP installation and run the configure script.

$ mkdir /Applications/MAMP/bin/php/php5.3.6/include
$ mv ~/Downloads/MAMP_components_2.0.2/php-5.3.6 /Applications/MAMP/bin/php/php5.3.6/include/php
$ cd /Applications/MAMP/bin/php/php5.3.6/include/php
$ ./configure

All being well, you should now be able to install PECL extensions.

$ pecl install uploadprogress
.
.
.
Build process completed successfully
Installing '/Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/no-debug-non-zts-20090626/uploadprogress.so'
install ok: channel://pecl.php.net/uploadprogress-1.0.3.1
configuration option "php_ini" is not set to php.ini location
You should add "extension=uploadprogress.so" to php.ini

You can now have some upload progress goodness by editing your php.ini file (in this case /Applications/MAMP/bin/php/php5.3.6/conf/php.ini) and restarting the MAMP servers.

Sally Young

Senior Developer

Want Sally Young to speak at your event? Contact us with the details and we’ll be in touch soon.

Comments

tommyent

Disregard last comment

Appears the download was corrupted

Also I had to edit the pear.conf
php_dir";s:44
to
php_dir";s:43

Reply

Terry

And why should I each time

And why should I each time after reloading terminal manually, reload profile file with ". ./.profile"?

Reply

justafish

You don't!

You don't, your .profile file will get read automatically each time you start a new terminal session. This was just to load the new settings into the current terminal session.

Reply

Terry

After all steps described,

After all steps described, now when I just now launch terminal and enter for example iMac-Molfar:~ molfar$ pecl I got -bash: pecl: command not found. Then I each time run . ./.profile, and only after that pecl, drush etc works fine. The file .profile is in my home dir< but it isnt loaded automaticly.

Reply

justafish

.bash_profile or .bash_login

hmm maybe you've already created a .bash_profile or .bash_login file? These will override .profile being run, so you could try sticking the path export into .base_profile instead.

Reply

Alexander

Unserialize() error

If, like me, you ran into this error...

Notice: unserialize(): Error at offset 276 of 1133 bytes in Config.php on line 1050
ERROR: The default config file is not a valid config file or is corrupted.

...simply delete the pear.conf file from MAMP's bin/php/php5.3.6 directory.
Hoo-ah.

Reply

Mike

Please help with error ...

Pear works, when i try ./configure with php i get this:

loading cache ./config.cache
checking for Cygwin environment... no
checking for mingw32 environment... no
checking for egrep... grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking host system type... i386-apple-darwin11.3.0
checking target system type... i386-apple-darwin11.3.0
checking for gcc... no
checking for cc... no
configure: error: no acceptable cc found in $PATH

whilst $PATH returns:

-bash: /Applications/MAMP/bin/php/php5.3.6/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin: No such file or directory

Please advise?

Reply

Mike

May help in answering my query

Ok, so I figured that it cannot find the gcc compiler.
I have Xcode installed, gcc is at /developer/usr/bin

... so I changed $PATH to ...

/Applications/MAMP/bin/php/php5.3.6/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/developer/usr/bin:/developer/usr/lib

... using profile.

I don't think the compiler can find its libraries as I get ...

loading cache ./config.cachechecking for Cygwin environment... nochecking for mingw32 environment... no
checking for egrep... grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking host system type... i386-apple-darwin11.3.0
checking target system type... i386-apple-darwin11.3.0
checking for gcc... gcc
checking whether the C compiler (gcc ) works... no
configure: error: installation or configuration problem: C compiler cannot create executables.

Please can you help?

Reply