Installing Mongo and Mongo-PHP on Mac OSX Mavericks with MAMP
<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.
Thank you for this tutorial. MongoDB installation has been giving me gray hairs and apparently I haven’t been the only one.
However I noticed during the installation that sudo pecl install mongo-php gave me following error:
parsePackageName(): “php” is neither a valid version nor a valid state in “mongo-php”
invalid package name/package file “mongo-php”
install failed
When I typed “sudo pecl install mongo” I got it working. I’m using Mountain Lion, dunno if it matters.
Thanks Jaakko, good tip. I’m on Mavericks so couldn’t test.
I got the same problem : the package name probably changed. You can browse all PECL packages here : http://pecl.php.net/packages.php
Thanks, Sam.
This was really helpful and saved my day !
Cool, glad it helped.
Thanks ! I had to change ‘sudo pecl install mongo-php’ with ‘sudo ./pecl install mongo’. My Mamp version is 3.0.7.2 and my Php is 5.5.18
ok, I try to use only ““sudo pecl install mongo” but now the error the shell said:
Configuring for:
PHP Api Version:
Zend Module Api No:
Zend Extension Api No:
autom4te: need GNU m4 1.4 or later: /usr/bin/m4
ERROR: `phpize’ failed
the error is on phpize….how can I solve it?
@andy – sorry, I haven’t look at this for a while, looks like you need to update the version of m4. I don’t know how to do this off-hand, might be worth looking at Homebrew (http://brew.sh/) or something similar to update Unix utils.
Thanks Sam, I tried a lot of guides but only with this, I have could configure succesfully!!
Oh cool. Glad it worked, bit dated now, relieved it still works.
good tutorial Sam…for Italians advice, is possible, enricoguida.it