MongoDB with MAMP (and Laravel!) on MacOS

Here’s how I got MongoDB working with MAMP (an Apache/PHP server) on MacOS. There was a bit of messing about involved, including building the MongoDB PHP extension, so I’ve documented it for my future self – hopefully it helps others too.

Prequisites

  • You’re using a Mac
  • Your using Apache and PHP via MAMP PRO (These steps may or may not work with the free version as it does not allow you to activate it’s built-in version of PHP on the command line)
  • You have Homebrew installed

Getting MongoDB working with MAMP’s built-in PHP

Here are the steps you need to take to get MongoDB working with PHP 8.1.1 in MAMP PRO. This should also work with earlier PHP versions, just alter the selections/file paths shown below.

  • If you need a local MongoDB server, install it by with Homebrew by running brew install mongodb
  • Autoconf will need to be installed to build the PHP extension, so brew install autoconf
  • I usually also create some bash scripts to start and stop the MonboDB service
  • Set your default MAMP PHP version to 8.1.1, and ensure it is activated on the command line
  • Open a fresh terminal window and confirm the correct version of PHP has loaded by running php -v
  • Navigate to MAMP PHP directory for the version that you have selected by running cd /Applications/MAMP/bin/php/php8.1.1/bin
  • Install the mongodb PHP extension by running pecl install mongodb
  • You might get a message error: 'pcre2.h' file not found
    • To resolve this, brew install pcre2
    • Then, run ln -s /opt/homebrew/Cellar/pcre2/10.42/include/pcre2.h /Applications/MAMP/bin/php/php8.1.1/include/php/ext/pcre/pcre2.h
    • You may need to update the version numbers in the paths above depending on what version of pcre2 was installed
  • That’s the tricky bit done, now navigate to MAMP’s PHP configuration again,and click ‘Enable other extensions’
  • Add extension=mongodb.so below the line extension=pdo_pgsql.so to enable the extension in Apache
  • You will also need to enable the extension in the PHP CLI configuration
    • Open the file in the nano text editor nano /Applications/MAMP/bin/php/php8.1.1/conf/php.ini
    • add extension=mongodb.so below the line extension=pdo_pgsql.so
    • Save and exit by pressing CTRL + X
  • Restart MAMP and the terminal app
  • You can check that the extension has successfully installed and loaded by:
    • Running php -m on the command line and checking that mongodb appears in the list
    • Adding phpinfo(); to a PHP file hosted by MAMP and accessing it via your web browser

The end goal of all of this was to get MongoDB working with Laravel – it’s labourious writing database migrations during early development (especially when the database design is being iterated on), so I want to try developing on MongoDB instead (no migrations required as there’s no rigid table structure), then moving to a SQL database later.

I’m using the jenssegers/mongodb Laravel package for this, and the installation checks dependencies, so both the Apache and command line MongoDB PHP extensions must be available.

Leave a Reply