pecl fails installing mongodb driver on Apple Silicon (M1)

Solution 1:

I have finally solved my problem. I followed these steps from https://github.com/mongodb/mongo-php-driver/issues/1159

After installing a newer PHP version where previously I used 7.3.24 and updated to 7.4.16.

Still had the same issue but diffrent file:

fatal error: 'pcre2.h' file not found #include "pcre2.h"

From this error I tried to check the files in the MacOs Big Sur File system and in the end I found where the pcre2.h is located, and oddly it was being called in the pcre folder, so I manually copied from the pcre2 folder to pcre

The solution that I used:

$cp /opt/homebrew/Cellar/pcre2/10.36/include/pcre2.h /opt/homebrew/Cellar/php\@7.4/7.4.16/include/php/ext/pcre/pcre2.h

Then, I installed mongodb using

$brew install mongodb

After that, I once again tried

$sudo pecl install mongodb

Finally, it worked. However, I'm not sure if this is a good way to solve the problem by manually adding a header file to the directory. (Sorry I'm still new to the MacOs Environment, just bought a mac mini m1 last week for developing my programming skills)

If you were to install php multiple versions from homebrew make sure you are using the current version as you wanted to. You could check the version by using which php.

To switch from 7.4 to 5.6

$brew unlink [email protected]
$brew link [email protected] --force

credits: https://www.markhesketh.com/switching-multiple-php-versions-on-macos/

New error New error

Success Installation: Success Installation

Solution 2:

I had the same issue with PHP 7.3, I solved it just by running the following commands:

cp /opt/homebrew/Cellar/pcre2/10.38/include/pcre2.h /opt/homebrew/Cellar/[email protected]/7.3.31/include/php/ext/pcre/pcre2.h

Then installed it successfully:

sudo pecl install mongodb