MacOS: Install MongoDB PHP Driver and mongodb Composer Extension

in #macos7 years ago (edited)

installMongoDBPHPmac.jpg

Sometimes a seemingly simple task of installing a package can oftentimes run into unexpected errors and roadblocks that leaves you head scratching, especially dealing with legacy codebases. Because of macOS’s security software, you may run into an error when installing the MongoDB PHP Driver via PECL.

The simplest and recommended way of installing the driver is via PECL, so this needs to be readily available on your Mac.

Disabling csrutils

To perform our driver installation you may need to disable macOSs system integrity protection “csrutils” process. This process ensures that the /usr folder on your mac is not tampered with, disabling write access and therefore throwing an error when attempting to install new software. One such case of this happening is when installing the MongoDB PHP driver.

To temporarily disable csrutils:

  • Reboot your Mac and hold CMD + R at reboot. This boots into recovery.
  • Select your language before opening the terminal in Utilities, Terminal.
  • submit the following commands:
csrutils disable 
reboot



From here your Mac will reboot with integrity protection disabled.

Install PEAR / PECL on MacOS

curl -O http://pear.php.net/go-pear.phar
sudo php -d detect_unicode=0 go-pear.phar



When prompted, enter 1, /usr/local/pear,4, and /usr/local/bin

Verify pear is installed with pear version and we are good to go.

Install MongoDB PHP Driver

Note: The MongoDB driver requires an openssl installation, however the required software is installed with your Mac provided you run the Xcode development tools. If on Linux however, make sure to install openssl development tools. On Red Hat, run sudo yum install openssl openssl-devel. On Ubuntu, run sudo apt-get install openssl libssl-dev.

Installing the driver now should go without issue. Run the following:

sudo pecl install mongodb

Let the installation process complete. At the end of installation you will be prompted to add the extension=mongodb.so in your php.ini file.

Finding your php.ini file

If you are unsure what .ini file you are using in your local web apps, firstly run php --ini. However, you may get something like this:

Configuration File (php.ini) Path: /etc
Loaded Configuration File:         (none)
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)



If this is the case, create a phpinfo(); page on one of your development sites and take note of the ini file location. Look out for Loaded Configuration File.

Once this is determined, simply add the extension to the file.

sudo vi <path-to-your-php-ini-file>

Enable csrutils

Now we are done with our installation, boot into recovery mode and enable crsutils again:

csrutils enable
reboot


Installing MongoDB for PHP via composer

Provided we have the driver installed, run the following composer command:

sudo php -c <path-to-your-php-ini-file> composer.phar require mongodb

Here we use the -c flag to provide composer with the php.ini file your local websites are using.

Installation completed!

From here you can communicate with MongoDB from within your PHP scripts.

Refer to the PHP library to get familiar with the available operations, starting with the CRUD suite of functions at https://docs.mongodb.com/php-library/current/tutorial/crud/#find-many-documents.

Sort:  

Posting a correction:

The csrutils command should be csrutil (no s).

For Debian and Ubuntu there is a quite powerful repository for PHP and many extensions - including the MongoDB driver: https://deb.sury.org/

For Debian check out the README.txt on: https://packages.sury.org/php/

For Ubuntu there is a Repo on Launchpad:
https://launchpad.net/~ondrej/+archive/ubuntu/php/