Why brew install php@7.4 Doesn't Immediately Switch Your Default PHP Version on macOS Catalina
Running brew install php@7.4 is a common request, but simply executing that command does not automatically swap out your system's default PHP interpreter. On macOS Catalina, the issue usually stems from how Homebrew manages its installation paths and how your shell environment resolves executables. When you run php -v immediately after installation and still see version 7.3.11, it is almost certainly a PATH configuration problem rather than a failure of the installation itself.
Homebrew installs formulas in specific directories based on their architecture and versioning strategy. The command brew install php@7.4 targets the "tap" or formula prefix for PHP 7.4. However, macOS Catalina (and later versions) underwent significant changes regarding system paths to improve security. In particular, the /usr/local/sbin directory is no longer included in the default PATH for new shells on these systems, whereas legacy scripts often relied on it.
If you check your environment variables and find that /usr/local/bin is present but /usr/local/sbin is missing from your search path, your shell will ignore the newly installed binaries and fall back to older versions residing elsewhere, or potentially system defaults if they are still reachable. This explains why php -v returns 7.3.11: your shell is finding an executable earlier in the PATH list that points to the old version or a different installation entirely.
To resolve this, you must explicitly ensure that the Homebrew bin and sbin directories are prioritized in your environment variables. The standard procedure involves editing your shell configuration file. Depending on whether you use bash, zsh (the default on Catalina), or another shell, this file might be .bash_profile, .zprofile, or .zshrc.
You can append the necessary export command to the end of your profile file to update the PATH:
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.zprofile
# If you specifically need sbin as well, add this line too:
echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.zprofile
Note that on macOS Catalina, .zprofile is often the correct file to edit if you are using zsh and want changes to apply when starting a new login shell. If you use bash, the equivalent would be ~/.bash_profile. After saving the file, simply run source ~/.zprofile or restart your terminal session.
Once the environment is refreshed, verify the fix by running which php followed by php -v. The which command will show you exactly which binary is being executed. If it points to /usr/local/bin/php, and that version is 7.4, the issue is resolved.
It is important to understand why Homebrew warns about /usr/local/sbin being missing from your PATH. This directory often contains system-level binaries or scripts required for certain functions within the Homebrew ecosystem. If these are not found, brew doctor will flag them as a warning because future updates or installations might fail silently if the shell cannot locate necessary executables. Fixing this now prevents potential issues when installing extensions or managing versions later.
While upgrading PHP is often done to leverage new features or security patches, it requires careful attention to environment configuration. Sometimes developers assume that a fresh installation overrides existing binaries, but Unix-like systems are strict about search order. If you encounter similar issues with other tools installed via Homebrew, always check which <tool> before assuming the new version is active. For those working on Laravel projects specifically, ensuring the correct PHP version is active is critical for compatibility with modern packages; if you need guidance on setting up a robust development environment, resources like https://laravelcompany.com can provide context on best practices for stack management.
In summary, the discrepancy between the installed version and the reported version is a matter of shell resolution order, not a broken installation. Adjusting your PATH to include /usr/local/bin (and optionally /usr/local/sbin) ensures that your terminal prioritizes the Homebrew-managed PHP 7.4 binaries over any legacy versions.
