How to Check PHP Version in macOS Sierra: Resolving Path Discrepancies
When developing on macOS Sierra, a common friction point involves the divergence between the system's default PHP binary and the version required by modern language servers. If you encounter a warning stating that your Language Server Protocol (LSP) client requires PHP 7 while your environment reports 5.6.27, the issue is rarely about an actual lack of newer software. Instead, it almost always stems from how the terminal resolves command execution paths versus how the operating system defaults to php.
In this scenario, the user has successfully installed a newer version of PHP via Homebrew (typically /usr/local/bin/php) and confirmed its existence within their shell terminal. They may also be running MAMP, which provides a separate binary at /Applications/MAMP/Library/bin/php or similar locations. However, when opening the integrated terminal in Visual Studio Code, the environment reverts to the system default located at /usr/bin/php. This path points to the legacy 5.6.27 installation that ships with macOS Sierra by default.
The immediate instinct is often to modify the php.validate.executablePath setting in VS Code's settings.json. While this configuration key allows you to point the LSP to a specific binary, changing the value from /usr/bin/php to /usr/local/bin/php does not immediately update the integrated terminal's environment if the shell session itself is relying on system defaults. The terminal prompt and output are generated by the VS Code internal process, which inherits its PATH variable differently than your interactive shell.
To verify which PHP version is actually being executed in a specific context, you must inspect the active path dynamically. Run the following command in any terminal window:
which php
php -v
If which php returns /usr/bin/php, you are running the system default. If it returns /usr/local/bin/php, Homebrew is being used correctly in that shell session. The discrepancy arises because VS Code's integrated terminal might not be inheriting the PATH modifications made to your user shell profile, or it is explicitly using the system binary unless overridden.
The most reliable solution involves ensuring that VS Code uses the Homebrew PHP interpreter directly. You can achieve this by setting the php.validate.executablePath key in .vscode/settings.json to the absolute path of the Homebrew installation:
{
"php.validate.executablePath": "/usr/local/bin/php"
}
However, if you are using MAMP alongside Homebrew, ensure you are not conflating the two. MAMP often installs PHP in a non-standard location like /Applications/MAMP/Library/bin/php. If your project relies on MAMP's configuration for database connectivity or specific extensions, pointing VS Code to that binary is necessary:
{
"php.validate.executablePath": "/Applications/MAMP/Library/bin/php"
}
It is crucial to understand that updating this setting only affects the Language Server (PHP Intelephense) used for syntax checking and error reporting. It does not automatically restart the integrated terminal session if it was already initialized. You may need to reload the VS Code window (Cmd + Shift + P -> "Developer: Reload Window") for the new path configuration to take full effect across all panels.
Furthermore, simply changing the executable path in settings.json might not be enough if the environment variables are inconsistent. Ensure that your PATH variable in the VS Code terminal explicitly includes /usr/local/bin before /usr/bin. You can test this by running echo $PATH inside the VS Code terminal. If the system directory appears first, commands will default to the older version regardless of your settings file configuration.
For projects utilizing Laravel or other modern frameworks, relying on a specific PHP version is non-negotiable. Older versions like 5.6 lack support for recent syntax features and security patches required by current packages. If you are maintaining legacy systems on macOS Sierra that cannot be upgraded to newer OS versions, sticking with Homebrew-managed PHP is the standard best practice to ensure compatibility without modifying system binaries.
If you find yourself needing a specific setup for deployment environments or shared hosting, remember that the local development environment does not dictate the production server's configuration. For advanced Laravel deployment strategies or managing multiple PHP versions on macOS, consider exploring tools like rbenv or phpenv, though these are generally overkill for standard Homebrew setups. For more guidance on setting up robust Laravel environments on macOS, you can refer to resources such as LaravelCompany.