MacOs change default php version with Homebrew

Viewed 9024

I'm running MacOS Catalina. I installed php@8.0 and now i want to change default from php@7.4 to php@8.0.

I installed php@8.0 with this command:

brew install shivammathur/php/php@8.0

I tried to change the default php with this command:

brew link --overwrite --force php@8.0

It output this:

Warning: Already linked: /usr/local/Cellar/php/8.0.2
To relink:
  brew unlink php && brew link php

Afterwards I restart Apache and Terminal but When I get the version of PHP with this command:

php -v

O get this output:

PHP 7.4.15 (cli) (built: Feb  4 2021 12:11:40) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.15, Copyright (c), by Zend Technologies

Running in output on Terminal:

brew unlink php && brew link php

returns this output:

Unlinking /usr/local/Cellar/php/8.0.2... 24 symlinks removed.
Linking /usr/local/Cellar/php/8.0.2... 24 symlinks created.

After restarting Apache, I get this version of PHP:

PHP 7.4.15 (cli) (built: Feb  4 2021 12:11:40) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.15, Copyright (c), by Zend Technologies
3 Answers

I have multiple versions of php installed on my mac and will switch it in ~/.zshrc depending on the project I am using.

Simply uncomment the one you want to use and comment the one you no longer wish to use. You would need to use brew to install the target version.

Close terminal then re-open for the change to take affect. Or run source ~/.zshrc

# PHP 7.4
export PATH="/usr/local/opt/php@7.4/bin:$PATH"
export PATH="/usr/local/opt/php@7.4/sbin:$PATH"

# PHP 7.3
#export PATH="/usr/local/opt/php@7.3/bin:$PATH"
#export PATH="/usr/local/opt/php@7.3/sbin:$PATH"

Based on Robert Saylor I exec:

# PHP 8.0.2
export PATH="/usr/local/Cellar/php/8.0.2/bin:$PATH"
export PATH="/usr/local/Cellar/php/8.0.2/sbin:$PATH"

Then checking php version:

PHP 8.0.2 (cli) (built: Feb  4 2021 17:58:53) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.2, Copyright (c), by Zend Technologies

after uninstalling php@7.4 my problem solved

brew uninstall php@7.4

checking php version:

PHP 8.0.2 (cli) (built: Feb  4 2021 17:58:53) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.2, Copyright (c), by Zend Technologies
Related