Switch of PHP Versions Not Working on MAC

Switch of php versions not working on mac

Here is my installation script:

  • brew install php@7.2
  • brew link --force php@7.2
  • brew services start php@7.2
  • export PATH="/usr/local/opt/php@7.2/bin:$PATH"
  • export PATH="/usr/local/opt/php@7.2/sbin:$PATH"

Now my output would be as:

$ php -v
PHP 7.2.25 (cli) (built: Nov 22 2019 10:27:28) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.25, Copyright (c) 1999-2018, by Zend Technologies

I think the PATH environment setup is something matters. And it does show in instructions as part of the installation process though.

Hope it helps resolving your issue.

How can I easily switch between PHP versions on Mac OSX?

If you have both versions of PHP installed, you can switch between versions using the link and unlink brew commands.

For example, to switch between PHP 7.4 and PHP 7.3

brew unlink php@7.4
brew link php@7.3

PS: both versions of PHP have be installed for these commands to work.

MacOs change default php version with Homebrew

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"

Change mac os x default php version

$ brew upgrade php : ( get the latest homebrew php packages )

$ brew install php@7.4 : ( Install php 7.4 )

$ brew link php@7.4 : ( create an alias to this keg-only version; see comments output during installation )

$ echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.bash_profile : ( Add the alias to your path; see comments output during installation )

$ source ~/.bash_profile : ( Reload .bash_profile to use the new settings immediately )

Reference : How to use the php that brew installed?



Related Topics



Leave a reply



Submit