How to install the MongoDB extension in MAMP for Mac

9th Aug '172 of your Earth minutes

How to install the MongoDB extension in MAMP for Mac

Yes, I use MAMP. Sue me (please don’t sue me). It’s actually a pretty good stack for standard web dev and I’ve found that it works fine for most of my needs, even with complex Laravel applications.

But recently I needed the MongoDB extenstion and the PHP builds shipped with MAMP don’t include this by default. Boo!

So I Google’d.

And it turns out there are not a lot of people doing this, surprise-surprise. But I did find one article which seemed to do exactly what I needed. However, despite only being about 1 year old (not even, as of this writing) this article was already well out of date.

Not sure if this is thanks to the rapid pace of change in the PHP community or because the writer was intentionally working against some older setup (El Capitan, PHP 5.6).

Regardless, the steps in that article didn’t quite do what I needed as quickly and painlessly as I would’ve liked. But they did lead me in the right direction… so I’m documenting here for reference.

As I have Homebrew installed, I just followed the instructions found on PHP.net. In my MAMP setup, I’m running PHP 7.1.1, so I ran:

1brew install php71-mongodb

That took a while, but it built a version of the extension for PHP 7.1. (It’s worth noting that, from what I can tell, this is built against a Homebrew version of PHP, but it seemed to work ok.)

Next, I copied the mongodb.so file from /usr/local/Cellar/php71-mongodb/{latest-version}/ (the {latest-version} in my case being 1.2.9) to my MAMP extensions directory, which is defined in the php.ini loaded for the version of PHP I selected to use in MAMP. In my case this was: /Applications/MAMP/bin/php/php7.1.1/lib/php/extensions/no-debug-non-zts-20160303/.

1cp ```
2/usr/local/Cellar/php71-mongodb/1.2.9/mongodb.so /Applications/MAMP/bin/php/php7.1.1/lib/php/extensions/no-debug-non-zts-20160303/
1
2The final step is to load this extension by adding it to the `php.ini` (`/Applications/MAMP/bin/php/php7.1.1/conf/php.ini`) like so:

; Extensions

extension=imap.so
extension=mcrypt.so
extension=gettext.so
extension=pgsql.so
extension=pdo_pgsql.so
extension=mongodb.so

1
2And of course, reboot the servers in MAMP to reload PHP under Apache with the new module. And as I’m using the same PHP version on the command line, a simple `php -m` now shows the MongoDB extension is loaded.
3
4CAVEAT: I’m not sure how things will fare when I upgrade my MAMP installation, I’m hoping it keeps the extension in place. At worst, I may have to repeat the last step again.
5
6Hope this helps someone.

#notadesigner • #sometimesitworks

All content licensed CC BY-SA 4.0  •  Code highlighting by Torchlight