10

I have php 7.4 installed on my macbook pro m1

% php -v
PHP 7.4.15 (cli) (built: Feb 26 2021 09:28:23) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.15, Copyright (c), by Zend Technologies

I'm trying to install the mongodb driver running:

sudo pecl install mongodb

But fails after a while:

mp/pear/temp/mongodb/src/contrib/ -DHAVE_CONFIG_H -g -O2 -c /private/tmp/pear/temp/mongodb/php_phongo.c  -fno-common -DPIC -o .libs/php_phongo.o
In file included from /private/tmp/pear/temp/mongodb/php_phongo.c:29:
In file included from /opt/homebrew/Cellar/php@7.4/7.4.15_1/include/php/ext/spl/spl_iterators.h:24:
/opt/homebrew/Cellar/php@7.4/7.4.15_1/include/php/ext/pcre/php_pcre.h:25:10: fatal error: 'pcre2.h' file not found
#include "pcre2.h"
         ^~~~~~~~~
1 error generated.
make: *** [php_phongo.lo] Error 1
ERROR: `make' failed

I tried with

arch -x86_64 sudo pecl install mongodb

but results in the same error.

Somebody knows how can I solve this please? Or if I can install the mongodb driver without using pecl. Thanks in advance.

Cesar Jr Rodriguez
  • 1,691
  • 4
  • 22
  • 35

6 Answers6

38

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 php@7.4
$brew link php@5.6 --force

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

New error New error

Success Installation: Success Installation

MaxiGui
  • 6,190
  • 4
  • 16
  • 33
Kelwin Tantono
  • 531
  • 7
  • 8
  • 1
    The copy command worked for me on my M1 MBA using php 7.4. Thanks so much dude, pcov was breaking because it was missing that same pcre2.h file – Mick Davies Apr 14 '21 at 02:07
  • Copy command worked for me as well on M1 using php 7.4 for installing imagick. Thanks! – Panda4Man Sep 29 '21 at 13:33
  • Ran into this issue because imagick was missing - did the trick as well (though I had some slightly different versions, due to the date difference of your question and today) +1 – Ben Fransen Dec 13 '21 at 20:06
  • I feel it's helpful, just in case someone misunderstands _why_ one might switch from PHP 7.4 down to PHP 5.6, to add a note that running PHP <7.4 in production is risky as prior versions are no longer receiving security updates. This is doubly true for the 5.x series, which stopped receiving support a long time ago. – M1ke May 11 '22 at 09:36
7

There is a simpler way to fix the installation issue than symlinking the header file into PHP sources. When running pecl install, you can provide additional compile arguments via the CFLAGS environment variable. So, on the Apple Silicon platform, you can use the following command to get the extension to compile:

CFLAGS=-I/opt/homebrew/include pecl install mongodb

Note that this is only necessary on Apple Silicon, not on x64. It also works for other extensions that fail because of similar errors (e.g. apcu)

alcaeus
  • 164
  • 1
  • 3
  • Recommending this solution against the alternatives as it is not version-dependent, and comes from MongoDB on https://jira.mongodb.org/browse/PHPC-2129 – FGM Aug 27 '22 at 13:09
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/php@7.3/7.3.31/include/php/ext/pcre/pcre2.h

Then installed it successfully:

sudo pecl install mongodb
seb_dom
  • 113
  • 7
2

I have finally solved my problem. I used PHP 8.1.7 - Mac Pro 2022 - Apple Silicon (M1)

My errors: fatal error: 'pcre2.h' file not found

The solution that I used:

1. brew install pcre2

2. ln -s /opt/homebrew/Cellar/pcre2/10.40/include/pcre2.h /opt/homebrew/Cellar/php/8.1.7/include/php/ext/pcre/pcre2.h 

Please check your path again when use `ln -s` 

3. sudo pecl install mongodb

See more: Install pecl pear on MacOs - Link

Success Installation:

enter image description here

Hope it help you :) Thanks

Alex
  • 3,646
  • 1
  • 28
  • 25
  • That solution worked with 8.1.9 on my M1 under Monterey 12.5.1 and PHP 8.1.9 (adjusting the paths). The `sudo` is not needed on the `pecl install mongodb` and may even be counter-productive. – FGM Aug 21 '22 at 19:58
  • A few days later, I now recommend the newer solution by Alcaeus, as it is less prone to bit rot with version changes, and is recommended by MongoDB Inc. on the associated Jira ticket https://jira.mongodb.org/browse/PHPC-2129 – FGM Aug 27 '22 at 13:09
0
cp /Applications/MAMP/Library/include/pcre2.h /Applications/MAMP/bin/php/php8.0.8/include/php/ext/pcre/pcre2.h

then run

sudo pecl install mongodb
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 16 '22 at 06:15
0

If you use MAMP,

Based on @Kelwin Tantono's answer follow instructions to install mongodb then,

Firstly, check for the location of pcre2.h file by

cd /opt/homebrew/Cellar/pcre2/

In this directory find the folder with ls command, in my case it was 10.40 .

Change 10.40 value in the below code with the result of ls command.

Then, check your php version with moving to php directory at MAMP by

cd /Applications/MAMP/bin/php/

Change php8.0.8 value in the below code with your desired php version.

cp /opt/homebrew/Cellar/pcre2/10.40/include/pcre2.h /Applications/MAMP/bin/php/php8.0.8/include/php/ext/pcre

Now you can install mongodb extension with below command after changing your working directory to

cd /Applications/MAMP/bin/php/php8.0.8/bin

sudo pecl install mongodb

Now, you should add extension=mongodb.so to php.ini file with

echo "extension=mongodb.so" > /Applications/MAMP/bin/php/php8.0.8/conf/php.ini

Do not forget to change php version.

galiperkin
  • 13
  • 3