Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

You switched PHP versions with Homebrew, but php -v still shows the old one

Errorphp -v still prints the old version after brew link

Short 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.

  • Applies to Homebrew 4, php@8.3, php@8.4, macOS 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 php
  2. 2Match what you see, then jump to its fix

    1. 1brew link php@8.3 fails or does nothing, and php -v is unchangedThe old formula is still linked in the Homebrew prefixFix
    2. 2php -v is still old, although which -a php lists the new version further downAnother php comes first on PATHFix
    3. 3php -v shows the new version but the website still reports the old one in phpinfo()php-fpm is still running the old versionFix

What does the error look like?

  • php -v still prints the old version after brew link
  • brew link php@8.3 prints an error or does nothing
  • the 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.3

3Verify

Ask the shell which php it finds and what version answers.

php -v

Confirmed by docs.brew.sh

Did this fix it?

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/null

2Fix

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 ~/.zshrc

3Verify

The first path printed should now sit inside the folder of the new version.

which php
php -v

Confirmed by docs.brew.sh

Did this fix it?

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 list

2Fix

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.3

3Verify

Reload a page that prints phpinfo() or PHP_VERSION and check the version it reports.

brew services list

Confirmed by docs.brew.sh

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.