PHP MongoDB extension fails with no error message or logs
Migrating to Apple Silicon (M1/M2) Macs often exposes subtle architecture mismatches-especially when legacy Homebrew, PHP, and Apache (httpd) installations are still running under Intel x64 emulation (Rosetta 2). Here’s how we resolved the issue of installing the MongoDB PHP extension when everything (Homebrew, httpd, PHP, and MongoDB extension) was running as Intel x64 on an M1 Mac.
The Problem
- Homebrew, PHP, and Apache (httpd) were all installed as Intel x64 binaries, running under Rosetta 2.
- Attempting to install the MongoDB PHP extension (
pecl install mongodb
) either failed or resulted in architecture mismatches. - The extension would not load, or PHP would throw errors about incompatible architectures.
Diagnosis
- Running
php -i | grep Architecture
andfile $(which php)
revealed PHP was running as x86_64 (Intel), not arm64. brew config
showed Homebrew’s prefix as/usr/local
(Intel), not/opt/homebrew
(Apple Silicon).pecl
installed extensions for the architecture of the PHP binary it was called from, so mixing architectures caused incompatibilities.
Resolution Steps
Uninstall Intel Homebrew and PHP
- Uninstall Intel Homebrew (
/usr/local
) and all Intel-based PHP/httpd installations. - Remove any lingering Intel-based binaries to prevent accidental usage.
Install Homebrew for Apple Silicon
- Install Homebrew in
/opt/homebrew
(the default for M1/M2 Macs).
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install PHP and Apache Natively
- Use the new Homebrew to install PHP and httpd:
brew install php httpd
- Confirm both are arm64 with
file $(which php)
andfile $(which httpd)
.
Install the MongoDB PHP Extension
- Install the extension using PECL (bundled with Homebrew PHP):
pecl install mongodb
- If you encounter missing header errors (e.g.,
pcre2.h
orusprep.h
), ensure dependencies are installed:
brew install pcre2 icu4c
- If PECL cannot find headers, you may need to set environment variables so the build process can find Homebrew-installed libraries:
export CPPFLAGS="-I/opt/homebrew/include"
export LDFLAGS="-L/opt/homebrew/lib"
pecl install mongodb
- If a header is missing, copy it as needed (example for
pcre2.h
):
cp /opt/homebrew/Cellar/pcre2/$(brew list --versions pcre2 | awk '{print $2}')/include/pcre2.h /opt/homebrew/Cellar/php/$(brew list --versions php | awk '{print $2}')/include/php/ext/pcre/pcre2.h
pecl install mongodb
Enable the Extension
- Add the extension to your
php.ini
(or create a new.ini
file in the conf.d directory):
extension="/opt/homebrew/Cellar/php/<php-version>/pecl/<api-version>/mongodb.so"
- Restart httpd or php-fpm.
Verify Installation
- Run:
php --ri mongodb
- You should see output confirming the extension is loaded and running under arm64.
Key Lessons Learned
- Consistency is critical: All components (Homebrew, PHP, httpd, extensions) must be built for the same architecture.
- Apple Silicon Homebrew is
/opt/homebrew
, while Intel is/usr/local
. - PECL must run with the correct PHP binary to build extensions for the right architecture.
- Dependency headers (like
pcre2.h
,usprep.h
) may need to be manually linked or copied if not found during compilation