Switch between multiple PHP versions on your mac
This article is a slimmed-down, firing-from-the-hips, get right to the action version based on the ones listed below. If you miss some background info or want a more spelled out version, please do visit these articles:
Install multiple PHP
If you want to install deprecated versions of PHP (< PHP7.2), then you’ll need to add this tap to homebrew:
brew tap exolnet/homebrew-deprecated
versions=(email@example.com firstname.lastname@example.org email@example.com firstname.lastname@example.org email@example.com firstname.lastname@example.org) # in ZSH #versions="email@example.com firstname.lastname@example.org email@example.com firstname.lastname@example.org email@example.com firstname.lastname@example.org" # in bash for version in $versions; do brew install $version done
If you receive the error
configure: error: Cannot find libz you need to install required libraries via XCode:
xcode-select --install brew upgrade
Install easy switch-script
To easily switch PHP versions, install the following script.
curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw > /usr/local/bin/sphp $ chmod +x /usr/local/bin/sphp
As I don’t add Apache on my host machine, change
apache_change=0 in the script.
vi `which sphp`
Without arguments, this command returns useful information like which versions are installed and which is the active one right now:
$ sphp 7.4
If you need to enter your administrator password, then you probably need to disable the part where Apache is restarted (see chapter above).
Switching to email@example.com Switching your shell Unlinking /usr/local/Cellarfirstname.lastname@example.org/5.6.40... 0 symlinks removed Unlinking /usr/local/Cellaremail@example.com/7.0.33... 0 symlinks removed Unlinking /usr/local/Cellarfirstname.lastname@example.org/7.1.33... 0 symlinks removed Unlinking /usr/local/Cellaremail@example.com/7.2.31_1... 0 symlinks removed Unlinking /usr/local/Cellarfirstname.lastname@example.org/7.3.19... 25 symlinks removed Unlinking /usr/local/Cellar/php/7.4.7... 0 symlinks removed Linking /usr/local/Cellar/php/7.4.7... 24 symlinks created PHP 7.4.7 (cli) (built: Jun 12 2020 00:04:10) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.7, Copyright (c), by Zend Technologies All done!
NOTE FOR XDEBUG: if you want to use
xdebug you’re switch command needs to be expanded to:
sphp 7.4 && pecl uninstall -r xdebug && pecl install xdebug
Now you’ll see that xdebug is enabled for PHP:
php -v PHP 7.4.7 (cli) (built: Jun 12 2020 00:04:10) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans with Zend OPcache v7.4.7, Copyright (c), by Zend Technologies
Again; take a look at the great, kept up-to-date, article of Andy Miller over at his website: https://getgrav.org/blog/macos-catalina-apache-multiple-php-versions