<tl;dr> A brief and thoroughly geeky blog post. If you’re not tinkering with Mongo on a Mac, best click away now </tl;dr>
Are you wanting to tinker with the fancy schmancy NoSQL database Mongo and you’re running MAMP on your Mac? Should be a doddle to install, right? Erm, not so much.
In case anyone hits the same problem, here’s how I got it working…I’m using the brew package manager to make life easier and install packages. Download and install it from http://brew.sh/
Step 1 – Install Mongo
brew install mongodb
Easy, peasy. Let’s not get ahead of ourselves, this is where it gets tricky. Thanks to Daniel Farrelly for the pointers in his blog post.
Step 2 – Install Autconf & Grab Xcode
brew install autoconf
We’ll need this later on. I’m pretty sure you’ll need Xcode, too. It’s a free download, fire up the App Store, search for Xcode and install it. Bit of a beast, so time to grab a brew (caffeinated or alcoholic, your choice).
Step 3 – Download and Install PHP Source
I’m using PHP 5.5.3. How’d I find out? Easy. Start your MAMP application, click on Preferences and select the PHP tab. You’ll see which version you’re running. It’ll look something like this image.
You’ll need the PHP source in order to compile the mongo-php database driver. The sources used to be available from the MAMP website, but I couldn’t find them anywhere.
Find the correct source from http://www.php.net/releases/, in this case I’m after 5.5.3, so let’s download it:
wget http://museum.php.net/php5/php-5.5.3.tar.gz
Unzip the file and you’ll get a new folder php-5.5.3. Move the contents of this folder – brace yourselves – into the correct folder in the MAMP directory. You’re looking for the correct version number, in this instance it’s:
cd /Applications/MAMP/bin/php/php5.5.3/ mkdir include cd include mkdir php
Now copy the contents of the unzipped file into this new folder /Applications/MAMP/bin/php/php5.5.3/include/php. The install process requires header files, so you need to run:
cd /Applications/MAMP/bin/php/php5.5.3/include/php ./configure
Thanks to Sven Loth for his comment on this post that highlighted this step.
Step 4 – Build & Install mongo-php Driver
Yeah, let’s build this sucker. This needs to be done with the right version of pecl for the version of PHP you’re using with MAMP. Again, I’m using 5.5.3, but change the version number for your version and it should work.
cd /Applications/MAMP/bin/php/php5.5.3/bin sudo pecl install mongo-php
This’ll take a few minutes, but fingers crossed, it should work.
Additional step: if PHP isn’t included in your PATH, you might need to add it. Thanks to Pascal Rieger for pinging this info over:
export PATH=/Applications/MAMP/bin/php/php5.x.x/bin:$PATH
Step 5 – Add mongo.so Extension to php.ini
Another gotcha here. MAMP has a whole bunch of php.ini files, one for each version of php. Some are used, some are overwritten. Thanks to this forum post, we know which one to use: /Applications/MAMP/conf/php5.5.3.
In the php.ini file, look for the extensions, the section will start with ; Extensions – at the end of the section add:
extension=mongo.so
Step 6 – Restart MAMP
Yup, do that to load up the new extension and test it’s working, this’ll do the job:
<?php phpinfo(); ?>
Scroll down the page and you should see a mongo section, something like this:
and that, should be that. Hope it works. Any tips, problems, etc, stick ’em in the comments.
Photo (cc) Jacob Ehnmark on Flickr.