You switched PHP versions with Homebrew, but php -v still shows the old one
php -v still prints the old version after brew linkShort answer
Homebrew can have several PHP versions installed, but only one is linked into the prefix, and your shell uses the first php on PATH. Run which -a php and brew list --versions first. Then unlink the old formula, link the new one, put its bin folder first in PATH, and restart php-fpm.
Fix it fast
1Find out which cause you have
List every php the shell can see, in the order it tries them, and ask Homebrew which PHP formulae are installed. The first line of the first command is the one that runs.
which -a php brew list --versions | grep php2Match what you see, then jump to its fix
- 1
brew link php@8.3 fails or does nothing, and php -v is unchangedThe old formula is still linked in the Homebrew prefixFix - 2
php -v is still old, although which -a php lists the new version further downAnother php comes first on PATHFix - 3
php -v shows the new version but the website still reports the old one in phpinfo()php-fpm is still running the old versionFix
- 1
What does the error look like?
php -v still prints the old version after brew linkbrew link php@8.3 prints an error or does nothingthe website still runs the old PHP version after switching in the terminal
Cause 1 of 3
The old formula is still linked in the Homebrew prefix
1Check
Versioned PHP formulae such as php@8.3 are alternate versions of the php formula, so only one of them owns the links in bin/. brew link refuses to replace files it finds there.
ls -l "$(brew --prefix)/bin/php"2Fix
Unlink the version that is linked now, then link the one you want. A keg-only formula needs --force (the php@8.3 formula page says it is keg-only), and --overwrite deletes files that block the link. Preview first with --dry-run.
brew unlink php
brew link --overwrite --dry-run php@8.3
brew link --overwrite --force php@8.33Verify
Ask the shell which php it finds and what version answers.
php -vConfirmed by docs.brew.sh
Cause 2 of 3
Another php comes first on PATH
1Check
which -a lists several paths. The shell runs the first one, so an older entry in ~/.zshrc or ~/.zprofile that puts another PHP first wins over whatever brew link did.
which -a php
grep -n "php" ~/.zshrc ~/.zprofile 2>/dev/null2Fix
Put the bin folder of the version you want first on PATH. brew --prefix with a formula name prints where that version is installed. Then open a new terminal.
echo 'export PATH="$(brew --prefix php@8.3)/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc3Verify
The first path printed should now sit inside the folder of the new version.
which php
php -vConfirmed by docs.brew.sh
Cause 3 of 3
php-fpm is still running the old version
1Check
The command line and the web server use different processes. A php-fpm service started from the old formula keeps running until you stop it.
brew services list2Fix
Stop the old service, start the one for the new version, and point the web server at it.
brew services stop php
brew services start php@8.33Verify
Reload a page that prints phpinfo() or PHP_VERSION and check the version it reports.
brew services listConfirmed by docs.brew.sh
How do I stop it coming back?
- Keep one active version and write down the unlink and link pair for the other, instead of linking without unlinking.
- Keep the PATH line for PHP in one file only, so two lines never fight over order.
- After every switch, restart php-fpm and check the version from the browser, not only from the terminal.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix