解决在Apple Silicon (M1)安装php MongoDB扩展失败

在MacBook Pro上已经安装了php7.4.24

➜php -v
PHP 7.4.24 (cli) (built: Oct 11 2021 08:58:20) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.24, Copyright (c), by Zend Technologies

尝试通过以下命令来安装MongoDB扩展
/opt/homebrew/opt/php@7.4/bin/pecl install mongodb
但是安装失败了

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.24_1/include/php/ext/spl/spl_iterators.h:24:
/opt/homebrew/Cellar/php@7.4/7.4.24_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

解决办法:

brew install pcre
查找pcre2.h头文件的位置

➜ find /opt/homebrew -name pcre2.h
/opt/homebrew/include/pcre2.h
/opt/homebrew/Cellar/pcre2/10.37_1/include/pcre2.h
/opt/homebrew/Cellar/pcre2/10.38/include/pcre2.h

软连接到使用的位置
ln -s /opt/homebrew/include/pcre2.h /opt/homebrew/Cellar/php@7.4/7.4.24_1/include/php/ext/pcre/pcre2.h
重新执行安装命令

➜ /opt/homebrew/opt/php@7.4/bin/pecl install mongodb
Build process completed successfully
Installing '/opt/homebrew/Cellar/php@7.4/7.4.24_1/pecl/20190902/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.10.0
Extension mongodb enabled in php.ini

安装成功

原文地址:https://www.cnblogs.com/ilizhu/p/15423995.html