sajad torkamani

Let’s assume you have two PHP versions installed via Homebrew:

  • PHP 8.1.1
  • PHP 8.0.14

Your current PHP version is 8.1.1:

php -v
PHP 8.1.1 (cli) (built: Dec 17 2021 23:49:52) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies

Now, you’re working on a project that’s not compatible with 8.1.1 so you want to use PHP 8.0.14.

Disable unwanted PHP version

Disable the formula that provides PHP 8.1.1.

brew unlink php

Here, we’re unlinking php which at the time of writing is the name of the formula that provides PHP 8.1.1.

Now, if you run php -v, you’ll either get the PHP version that’s installed by default on macOS or a command not found error if your macOS doesn’t provide PHP by default.

WARNING: PHP is not recommended
PHP is included in macOS for compatibility with legacy software.
Future versions of macOS will not include PHP.
PHP 7.3.24-(to be removed in future macOS) (cli) (built: Jun 17 2021 21:41:15) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.24, Copyright (c) 1998-2018 Zend Technologies

Stop PHP-FPM

If using PHP-FPM, stop the PHP service.

brew services stop php

Enable new PHP version

Make sure you’ve installed the formula containing your desired PHP version, then run:

brew link php@8.0

Now, you should have PHP 8.0.14 enabled.

php -v
PHP 8.0.14 (cli) (built: Dec 17 2021 22:29:11) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.14, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.14, Copyright (c), by Zend Technologies

Start PHP-FPM

If using PHP-FPM, enable the PHP-FPM service.

brew services start php@8.0

If you want to switch back, you can repeat the same steps.

Tagged: PHP

Leave a comment

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