Installing extensions on Mamp Pro for Mac

Placing objects within this box opens a hidden trapdoor...

arduino,physical computing

I spent way too many hours last night (this morning? What time is it?) trying to install MongoDB for a site I was building which requires it, and most of the documentation I read (which was super helpful) didn't quite fix my issues. I'm no systems administrator, so it took a lot of time to stumble through this issue. The solution I finally came to was a conglomeration of a few tutorials I read online, so I'm posting it here in hopes it will help someone else.

If you go into terminal and run
which php
, chances are you'll get something like
usr/bin/php/

This means that php is set to run from Apple's pre-installed instance of PHP, not MAMP's, so any modules you install will not take effect within MAMP, unless you add MAMP's php binaries to your path variable:

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

You'll need to figure out what version of PHP you're running in MAMP and change the path above accordingly. Within MAMP Pro, if you're using virtual hosts, just go to the Hosts tab and find your site, and note the version of PHP listed there. And just for safe measure, you might want to just go into finder, within your MAMP/bin/php directory, and find that folder. If you drag the folder straight in to terminal, you'll get the absolute path that you need to copy to the echo statement above.

Once you run this command, you should read in the new path variable with this command:

. ./.profile
Now, if you run
which php
, rather than
usr/bin/php
you should see the folder you just specified in the path variable:
/Applications/MAMP/bin/php/php5.6.2/bin/php
. If you get something about the command which not being found, just restart terminal and try again. I had some weirdness happen around that.
You might also just want to see what's in your /.profile file, by running
sudo vim /.profile
- I've configured a lot of sites on my machine based on instructions from backend devs that I barely understood, so mine had some duplicates and unused path settings which I cleaned up.

Now that you're pointing to the right version of PHP within MAMP, you need to install the tools that will allow you to build your extension (extensions can't just be downloaded, they have to be compiled within your environment to ensure they have the proper headers). I saw a lot of tutorials referencing a certain set of downloadable tools available from MAMP's download page, but these were written around the time of MAMP 2. As far as I can tell, those are no longer available as of MAMP 3. I was actually able to download them straight from php, here..

Once they are downloaded, go to that folder within MAMP where your version of PHP is running (
/Applications/MAMP/bin/php/php5.6.2/bin/php
) and make a directory called
include
. Extract the file you just downloaded - it should leave you with a folder called php-5.6.2 (or whatever version you downloaded). Copy that into the include folder you just made, and rename it to
php
.
Now you can install extensions from this folder. Is your path still pointing to the right place? Run
which php
to find out. For some reason, I found that if I screwed up in the install process, this would revert back to
usr/bin/php
so if you have issues, always check that along the way. You'll now need the mongodb extension itself. You should be able to run:
sudo pecl install mongo

ONLY IF YOU HAVE ISSUES

If for some reason it doesn't work for you, read below. Otherwise, skip ahead to the next. If it didn't work, you can download Mongo directly from here and, unzip it somewhere on your computer (the desktop is fine). Open up another terminal window and cd into that directory, then run the following commands:


phpize
./configure
make
sudo make install

This will create a
mongo.so
file within the
modules
folder of the mongo folder you just downloaded. Take that mongo.so file, and copy it into the appropriate MAMP folder. For me, that folder would be
/Applications/MAMP/bin/php/php5.6.2/lib/php/extensions/no-debug-non-zts-20131226
. If you open that up in finder, you should see a bunch of other .so files. Copy mongo.so into there. You can verify the proper path by opening up the php.ini file (
/Applications/MAMP/bin/php/php5.6.2/conf
), and searching for the extensions directory listed there.

IF YOU STILL HAVE ISSUES

Now, assuming you've got your mongo.so built properly and copied into the right folder, here's what tripped me up. You need to add the extension to the list of installed extensions within php.ini. However, MAMP Pro does not put these in the folder. To find out where it's putting them, you could put a php file with the contents:
into a visible location in your application, and read out the path listed in the "Loaded Configuration File" section. Or you can just go into the MAMP Pro application and choose
File > Edit Template > PHP > PHP 5.6.2 php.ini
, which will open up that location directly for editing. Find the list of extensions there, and add:
extension=mongo.so

Now restart MAMP, and mongo should be installed.

If you are having troubles, here were the posts I found most helpful:

Good luck!