Skip to content

When updating homebrew with your new M1 Apple machine as a web dev machine by developing backend applications with PHP with Homebrew along with MongoDB, where there is an issue when you compile mongoDB driver using PHP from homebrew. We always run into this minor problem, which is somewhat irritating to an extend as we update via the brew command. The main issue we had encountered was that whenever we run

./brew update

the mongodb.so lib driver file will get deleted automatically in the PHP modules location. Since, we've decided to install mongoDB manually from source code, we knew there would be some type of problems we would encounter.

There were two issues:
1) missing pcre2.h file upon compilation
2) automatic deletion of mongodb.so inside a php module dir, after issuing a brew update command.

We will first remedy the missing pcre2.h file in the following section, so that we could compile mongoDB as a driver to be used with PHP@7.4. We will then copy over the mongodb module into the PHP module location.

Using PHP.net 's instructions on how to retrieve the source code for MongoDB. There is a process to get this done relatively with ease.

Compilation of PHP MongoDB Driver

$ git clone https://github.com/mongodb/mongo-php-driver.git
$ cd mongo-php-driver
$ git submodule update --init
$ phpize
$ ./configure

At this Point, pcre2.h is not available from the github repo for this php-mongodb driver package, you can now copy from the default location from your M1 to get this header file to be copied over to this mongo-php-driver directory.

This pcre2.h file should be in this area on your M1 machine, depending on your version of pcre2 from your brew update command. If you don't currently have the pcre2.h file, you then must install the pcre dev header package for pcre from homebrew first. This version i have installed is listed below.

/opt/homebrew/Cellar/pcre2/10.38_1/include/pcre2.h
$ cp /opt/homebrew/Cellar/pcre2/10.38_1/include/pcre2.h . 
$ ./configure 
$ make all

This should take care of the missing pcre2.h file after placing the pcre2.h unto the mongo-php-driver directory for compilation. Once it has finished compiling, you can cd to the modules directory and copy over the mongodb.so file to /opt/homebrew/Cellar/php@7.4/7.4.26/lib/php/20190902/ or whichever directory location it needs to be copied to that location.

If you do have PHP@7.4 in your PATH, you can enter in php -v to check

if that missing --mongodb.so-- lib error has gone away.

flo@hobbit: $ php -i | grep -i mongo

mongodb
MongoDB support => enabled
MongoDB extension version => 1.12.0-dev
MongoDB extension stability => devel
libmongoc bundled version => 1.11.1-20211103+gitb93e345178
libmongoc SSL => enabled
libmongoc SSL library => Secure Transport
libmongoc crypto => enabled
libmongoc crypto library => Common Crypto
libmongoc crypto system profile => disabled
libmongoc SASL => enabled
libmongoc ICU => disabled
libmongoc compression => enabled
libmongoc compression snappy => disabled
libmongoc compression zlib => enabled
libmongoc compression zstd => enabled
libmongocrypt bundled version => 1.2.1
libmongocrypt crypto => enabled
libmongocrypt crypto library => Common Crypto
mongodb.debug => no value => no value
mongodb.mock_service_id => Off => Off



The reason for compiling is that one can easily obtain the latest and greatest software.