Homebrew

How to change the PHP version on macOS with Homebrew

By

in

Homebrew is a simple package manager on macOS. Homebrew installs the stuff you need that Apple (or your Linux system) didn’t.

When upgrading your packages e.g. by using

brew upgrade

all your packages are updated to the latest version. This is also the case for libraries like PHP. In my case, I still use PHP 8.2 for most of my projects. The current PHP version is 8.4.6, so after running the upgrade command, my system uses PHP 8.4.6 by default. You can check the PHP version with php -v

php -v
PHP 8.4.6 (cli) (built: Apr  8 2025 19:55:31) (NTS)
Copyright (c) The PHP Group
Built by Homebrew
Zend Engine v4.4.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.6, Copyright (c), by Zend Technologies

To change the version, you have to install the version you want. In my case it’s PHP 8.2.x:

brew install php@8.2

Then you have to unlink the latest version:

brew unlink php
Unlinking /usr/local/Cellar/php/8.4.6... 24 symlinks removed.

And link to the new version:

brew link php@8.2
Linking /usr/local/Cellar/php@8.2/8.2.28_1... 25 symlinks created.

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/php@8.2/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/usr/local/opt/php@8.2/sbin:$PATH"' >> ~/.zshrc

Now, the default PHP version on your system should be 8.2. Again, you can check the PHP version with php -v:

php -v
PHP 8.2.28 (cli) (built: Mar 11 2025 17:58:12) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.28, Copyright (c) Zend Technologies
    with Xdebug v3.2.2, Copyright (c) 2002-2023, by Derick Rethans
    with Zend OPcache v8.2.28, Copyright (c), by Zend Technologies

That’s it. Those steps might be necessary after each brew upgrade.

Photo by Bohdan Stocek on Unsplash



Comments

Leave a Reply

Your email address will not be published. Required fields are marked *