env: php: No such file or directory on macOS Monterey
Upgrading to macOS Monterey (version 12) often triggers a specific friction point for PHP developers: the env: php: No such file or directory error. This message appears when you attempt to run composer, php artisan, or any CLI tool that depends on the PHP interpreter. The error indicates that your shell cannot locate the executable, not that the application is missing from your hard drive.
On older macOS versions like Catalina or Big Sur, the system frequently pre-installed a bundled PHP version in /usr/bin/php. Monterey changed this default behavior; Apple removed the bundled PHP binary from /usr/bin entirely to prevent conflicts with third-party installations. Consequently, any scripts relying on the absolute path /usr/bin/php or those expecting a global default will fail immediately after an upgrade.
Diagnosing the Environment
Before attempting fixes, you need to understand where your shell is looking. When you type php, your terminal searches directories listed in the $PATH environment variable. The error occurs because the standard path no longer contains a valid binary, and your custom installation (likely via Homebrew) is not taking precedence.
Run the following command to see what your system currently associates with php:
which php
On Monterey, if this returns nothing, or if it points to /usr/bin/php which then errors out, you have a path precedence issue. You likely have PHP installed via Homebrew (usually in /opt/homebrew/bin on Apple Silicon Macs or /usr/local/bin on Intel), but that directory is not at the front of your $PATH.
Restoring the Path
The most reliable solution is to update your shell configuration file so that Homebrew's bin directory appears before system directories. Open your preferred shell profile:
- Zsh (default on Monterey):
~/.zshrc - Bash:
~/.bash_profileor~/.bashrc
Add the following lines to ensure Homebrew paths are prioritized. Note that for Apple Silicon Macs, /opt/homebrew is the correct path, whereas Intel Macs use /usr/local.
# Ensure this comes before the system /usr/bin entries
export PATH="/opt/homebrew/bin:/opt/homebrew/sbin:$PATH"
# Or for Intel Macs:
# export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
After saving the file, reload your shell or restart your terminal session. Running php -v again should now display the version installed by Homebrew without triggering the "No such file or directory" error.
Composer and Artisan Specifics
If you are specifically struggling with composer, note that Composer itself is a PHP dependency manager. If the underlying PHP interpreter is missing from the path, Composer cannot execute its bootstrap scripts, resulting in the same environment error. Fixing the PHP path resolves the issue for both tools simultaneously.
However, be aware of a common pitfall: some projects configure their composer.json or php.ini to expect a specific PHP version string that differs from what is installed. While less common on local development setups, ensure your composer.lock file does not mandate a version that conflicts with your Homebrew installation.
Another scenario involves Docker-based workflows. If you are developing locally but pushing to production environments running macOS Monterey, ensure your container definition explicitly installs PHP or maps the correct host path. Relying on the host's /usr/bin/php inside a container will always fail unless explicitly configured via docker-entrypoint.sh scripts.
Alternative Approaches and Trade-offs
If modifying your shell profile feels invasive or you prefer not to alter global environment variables, you can use an explicit path invocation for CLI commands:
php /opt/homebrew/bin/php artisan serve
composer /opt/homebrew/bin/composer install
This approach works immediately but is cumbersome. It breaks the convention of writing portable scripts that assume a standard $PATH. For long-term maintainability, updating your shell configuration remains the industry standard practice.
There is also the option of installing PHP via MacPorts or using Apple's official installer if you prefer system-managed binaries over Homebrew. However, mixing multiple PHP installations (e.g., one from Homebrew and one system-wide) without careful path management often leads to php.ini conflicts where the wrong configuration file is loaded. Sticking to a single installation method, typically Homebrew for developers, reduces this risk significantly.
Finally, if you encounter issues related to specific extensions missing after the upgrade, remember that Monterey may have dropped support for older PHP versions in its system bundle. Ensure your Homebrew PHP version meets the requirements of your frameworks. Laravel 9 and above generally require PHP 8.1 or higher, so verify your Homebrew installation matches these constraints before attempting to run php artisan migrate.