I often need to switch between multiple PHP versions on my macOS environment when working on legacy projects.
As usual, I had forgot how to do this, so I’ve decided to publish the steps for my own and other’s reference.
The instructions below are for use with macOS 10.15 Catalina, and allow installation of PHP 5.6, 7.0, 7.1, 7.2, 7.3 & 7.4.
You’ll need both Xcode Command Line Tools and Homebrew installed.
1.1 Install XCode Command Line Tools
1.2 Install Homebrew
Homebrew is a package manager for macOS. It is similar to
apt on Ubuntu.
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Check that brew has been installed correctly:
$ brew --version Homebrew 2.2.5
You can also run
brew doctor to check everything is good to go.
As of writing, only PHP 7.2, 7.3 and 7.4 are actively maintained and officially supported by Homebrew.
To install PHP 5.6, 7.0 & 7.1 we’ll need to ‘tap’ an additional repository:
brew tap exolnet/homebrew-deprecated
Now, we can install all the available PHP versions:
brew install email@example.com brew install firstname.lastname@example.org brew install email@example.com brew install firstname.lastname@example.org brew install email@example.com brew install firstname.lastname@example.org
This may take a little time to install. Go make yourself a brew!
Once installed, you can switch between PHP versions by ‘linking’ and ‘unlinking’ in brew:
# Switch from 7.4 to 5.6 brew unlink email@example.com brew link firstname.lastname@example.org --force
You can combine
brew unlink and
brew link to swap between any installed version.
There are a few open source projects that aim to automate this for you, if you prefer: