Mac / OSx - Change PHP version which is call in bash

Mac / OSx - Change PHP version which is call in bash

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Switching Your Default PHP Version on macOS Lion

The discrepancy you are seeing between php -v and /usr/bin/php is a classic symptom of how macOS manages its system binaries versus user-installed packages. On macOS Lion (and subsequent versions), the path /usr/bin/php points to the version bundled with the OS, often an older release like 5.3.x in your specific case. When you install PHP via Homebrew or MacPorts, those versions usually live in directories like /usr/local/bin or /opt/local/bin.

Because Unix-like systems prioritize finding executables in the order they appear in the PATH environment variable, the shell resolves the command php to the first match it encounters. If your system PATH lists /usr/bin before /usr/local/bin, every time you type php, the shell invokes the system-wide PHP 5.3.17 instead of your preferred 5.3.15. To make your desired version the default, you must ensure the directory containing your target executable appears earlier in the PATH.

Modifying the PATH Environment Variable

The most direct solution is to prepend your custom PHP location to the system path. This can be done by editing your shell configuration file, such as ~/.bash_profile, ~/.zshrc, or ~/.profile, depending on which shell you are using on Lion.

Locate your preferred PHP binary. If you installed it via Homebrew, it is typically located at /usr/local/bin/php. Verify this with:

which php
# Output might be /usr/bin/php (the system one) or /usr/local/bin/php (if already fixed)
ls -l /usr/local/bin/php

Once confirmed, open your profile file. For Bash, edit ~/.bash_profile:

nano ~/.bash_profile

Add the following line at the top of the file to ensure it is processed first:

export PATH="/usr/local/bin:$PATH"

This syntax literally says: "When looking for executables, check /usr/local/bin first, then append everything currently in $PATH." Save and exit. Now, reload your shell configuration to apply changes immediately without restarting the terminal:

source ~/.bash_profile

Run php -v again. If it now displays 5.3.15, you have successfully overridden the system default. This approach is non-destructive because it does not alter the system files in /usr/bin; it simply changes the order of lookup for your current session and future sessions.

Understanding System vs. User Binaries

It is crucial to understand that modifying PATH only affects your user account. It does not change what happens when you use a Terminal application launched from the macOS menu bar or when running scripts via cron jobs that might source different profiles. Furthermore, if you are developing for a Laravel project (or any PHP framework), relying solely on the system path can lead to inconsistent behavior across different machines.

If you ever switch computers or deploy code to a server where /usr/local/bin does not exist or points elsewhere, your scripts will break unless they explicitly specify the full path to the PHP interpreter. For instance, using /usr/local/bin/php artisan serve ensures the correct binary runs regardless of the PATH. This explicit invocation is a best practice in production environments and containerized setups like Docker, where environment variables are often managed differently.

The Risks of Overwriting /usr/bin/php

You might be tempted to simply replace the file at /usr/bin/php with your custom version. While technically possible using cp -f, this is strongly discouraged. The system binary in /usr/bin is tightly coupled with macOS's internal tooling, including the Package Manager (Software Update) and system diagnostics. Overwriting it can lead to unpredictable behavior if Apple or your package manager attempts to restore or update that specific file.

Additionally, on macOS Lion, the system PHP might be required by other native applications or command-line utilities that expect a specific version signature. By manipulating the PATH, you maintain the integrity of the system while achieving your goal locally. If you need to change this preference for every user on the machine (not just yourself), you would need administrative privileges to edit /etc/profile or similar global configuration files, which is generally unnecessary for personal development workflows.

Conclusion

By adjusting the PATH variable in your shell profile, you seamlessly prioritize your installed PHP 5.3.15 over the system default. This method preserves system stability while giving you full control over your development environment. Always prefer path manipulation or explicit binary invocation over replacing system files to avoid breaking dependencies that rely on the standard macOS installation.

Our network