Changing the Default PHP Version on macOS High Sierra
On macOS High Sierra (version 10.13.6), the system often ships with a bundled version of PHP, typically 7.1 or 5.6, which is not always compatible with modern frameworks. In your specific case, php -v reports PHP 7.1.32, but you need to run PHP 7.3. The immediate attempt to use brew unlink php7.1 failed because this command does not actually remove the system's bundled PHP binary from the execution path; it merely unlinks a Homebrew formula if one exists alongside the system binary. macOS prioritizes binaries located in /usr/bin and /System/usr/bin over those managed by package managers like Homebrew unless you explicitly adjust the search order.
To successfully switch to PHP 7.3, you must ensure that the Homebrew-installed version takes precedence in your shell's PATH environment variable. The most robust method involves adding the Homebrew bin directory to the front of your path. Open your shell configuration file (e.g., ~/.zshrc, ~/.bash_profile, or ~/.profile) and add the following lines at the very top:
export PATH="/usr/local/bin:$PATH"
If you are using an Apple Silicon Mac running High Sierra (which is unlikely given the OS version, but worth noting for architecture consistency), ensure you are targeting the correct architecture. On Intel-based Macs with Homebrew installed in /usr/local, this command ensures that any subsequent php command executes the 7.3 binary before falling back to the legacy 7.1 system binary. After saving the file, restart your terminal or run source ~/.zshrc (or the appropriate source command for your shell) to apply the changes immediately.
Verify the change by running which php. This command should now output a path starting with /usr/local/bin, such as /usr/local/bin/php73 or similar, depending on how Homebrew names the symlink. Then run php -v again; you should see PHP 7.3.x listed. If which php still points to /usr/bin/php, your shell configuration file was not sourced correctly, or you are running a system service (like Nginx or Apache) that does not use your interactive shell's environment.
For web servers, the default binary is rarely what matters most. If you are using PHP with a framework like Laravel, the server process must be explicitly configured to use the desired version. In an .htaccess file or nginx.conf, you would specify the full path to the correct binary:
SetHandler "php73"
Or in Nginx configuration:
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PHP_BINARY /usr/local/bin/php73;
If you are using php-fpm, ensure the systemd or launchd service file points to the correct binary path. A common pitfall is assuming that changing the global PATH affects system services running as root without explicit configuration updates in their startup scripts.
Another potential issue arises if you have multiple PHP versions installed via Homebrew (e.g., php71, php73, php80). In this scenario, you can create an alias to simplify switching:
alias php='php73'
alias php71='/usr/bin/php71'
Add these aliases to your shell config file. This allows you to keep the global path clean while retaining quick access to specific versions. Note that this approach requires careful management to avoid confusion when debugging issues related to version mismatches between CLI and web environments.
If you encounter persistent issues where the system binary overrides Homebrew's installation despite PATH adjustments, consider using phpenv for a more granular control over PHP versions. Install it via Homebrew (brew install phpenv) and follow its initialization steps to manage multiple PHP versions seamlessly. This tool isolates each version in separate directories and allows you to switch between them per project directory, which is particularly useful when working with legacy systems that require older PHP versions alongside modern ones.
For developers maintaining legacy codebases on macOS High Sierra, ensuring the correct PHP version is active is critical for security and performance. Modern libraries may not function correctly on PHP 7.1, leading to unexpected runtime errors or deprecated function warnings. By aligning your development environment with PHP 7.3, you gain access to improved error handling features and better memory management. If you are deploying applications, always test the deployment on the target server's exact configuration, as macOS High Sierra is no longer receiving security updates, making the choice of PHP version even more critical for stability.