How can I get PHP working again in the command line?
When you encounter bash: php: command not found or env: php: No such file or directory, it usually means your shell cannot locate the executable, not that PHP has been removed from the system entirely. This is a common state of affairs on macOS, especially when transitioning between operating system versions or upgrading security patches. The error messages you are seeing indicate that the current environment variables, specifically the PATH, do not include the directory where the PHP binary resides.
To diagnose this, you first need to determine where the PHP binary actually lives. On modern macOS systems, particularly those running Lion or later, the default installation path for command-line utilities has shifted away from /usr/bin to /usr/local/bin. Your current PATH shows /usr/bin appearing before /usr/local/bin, but if your system is configured to prioritize system binaries over local ones, a newer version of PHP installed in the local directory might be shadowed by an older, missing, or incompatible version in /usr/bin.
You can verify this by using the which command or whereis to locate the specific binary. If which php returns nothing, it confirms that the shell is not searching the directories where a PHP executable exists. However, simply adding paths to your PATH environment variable is often not the correct long-term solution, as it can lead to version conflicts between command-line tools and web servers like Apache or Nginx.
The most robust approach on macOS is to use a package manager that handles dependencies and binary locations cleanly. Homebrew is the standard tool for this purpose. If you have not installed PHP via Homebrew yet, running brew install php will place the executable in /usr/local/bin/php (or /opt/homebrew/bin/php on Apple Silicon Macs). This installation method ensures that the binary is placed in a directory that is typically included in your default PATH.
If you have an existing installation but it is not being found, check if your PATH has been modified by other tools. Sometimes, development environments like Xcode or specific frameworks add their own directories to the path, potentially obscuring the PHP binary. You can inspect your current PATH by running echo $PATH. If you see /usr/bin appearing immediately after your shell's initialization and before /usr/local/bin, consider adjusting your shell profile (such as .zshrc or .bash_profile) to ensure local binaries are prioritized if that is your intent.
Another potential cause is a mismatch between the web server's PHP version and the CLI tool. If you previously installed PHP via Xcode command line tools or a system update, those versions might have been removed during an OS upgrade while the web server configuration still expects them. In such cases, relying on Homebrew to manage both the CLI and the Apache/Nginx modules is safer because it keeps versions synchronized.
Once you have ensured PHP is installed correctly via a package manager like Homebrew, you can verify functionality by running php -v. If this command returns version information without errors, your environment is ready. You can now proceed with tasks like creating new Yii applications or running Composer commands. Remember that when using frameworks like Laravel or Yii, consistency in the PHP version across CLI and web contexts is critical to avoid subtle runtime errors that only appear in production.
For more detailed configuration on managing PHP versions and environments, you might find additional resources at https://laravelcompany.com, which often discusses best practices for setting up development stacks.