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:
If you want to install deprecated versions of PHP (< PHP7.2), then you’ll need to add this tap to homebrew:
brew tap shivammathur/php
#versions="php@7.2 php@7.3 php@7.4 php@8.0 php@8.1" # in bash
versions=(php@8.1 php@8.2 php@8.3) # in ZSH
for version in $versions; do
echo "installing ${version}"
brew install shivammathur/php/${version}
done
# install xdebug for php >= 7.2
pecl uninstall -r xdebug
pecl install xdebug
If you receive the error configure: error: Cannot find libz
you need to install required libraries via XCode:
xcode-select --install
brew upgrade
To easily switch PHP versions, install the following script.
curl -L https://raw.githubusercontent.com/rhukster/sphp.sh/main/sphp.sh > /usr/local/bin/sphp
$ chmod +x /usr/local/bin/sphp
As I don’t add Apache on my host machine, change apache_change=1
to 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 php@7.4
Switching your shell
Unlinking /usr/local/Cellar/php@5.6/5.6.40... 0 symlinks removed
Unlinking /usr/local/Cellar/php@7.0/7.0.33... 0 symlinks removed
Unlinking /usr/local/Cellar/php@7.1/7.1.33... 0 symlinks removed
Unlinking /usr/local/Cellar/php@7.2/7.2.31_1... 0 symlinks removed
Unlinking /usr/local/Cellar/php@7.3/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
That’s it!
Again; take a look at the great, kept up-to-date, article of Andy Miller over at his website: https://getgrav.org/blog/macos-monterey-apache-multiple-php-versions