Profile picture of Mark Hesketh Mark Hesketh

Switching between multiple PHP versions on macOS

I recently needed to switch between PHP versions on my macOS environment to work on a legacy project.

As usual, I’d forgotten 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.

Using several versions of PHP on macOS via homebrew

1. Prerequisites

You’ll need both Xcode Command Line Tools and Homebrew installed.

1.1 Install XCode Command Line Tools

xcode-select --install

1.2 Install Homebrew

Homebrew is a package manager for macOS. It’s like apt on Ubuntu.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Check that brew has installed:

$ brew --version
Homebrew 2.2.5

You can also run brew doctor to check everything is good to go.

2. Install Multiple PHP Versions

As of writing, only PHP 7.2, 7.3 and 7.4 are maintained and supported by Homebrew.

To install PHP 5.6, 7.0 & 7.1 we’ll need to ‘tap’ a repository for deprecated packages:

brew tap exolnet/homebrew-deprecated

Now, we can install all the available PHP versions:

brew install [email protected]
brew install [email protected]
brew install [email protected]
brew install [email protected]
brew install [email protected]
brew install [email protected]

This may take a little time to install. Go make yourself a brew ☕️.

3. Switching between PHP versions

Once installed, you can switch between PHP versions by ‘linking’ and ‘unlinking’ in brew:

# Switch from 7.4 to 5.6
brew unlink [email protected]
brew link [email protected] --force

Switching from PHP 7.4 to 5.6

You can combine brew unlink and brew link to swap between any installed version.

Open Source Alternatives

There are a few open source projects that aim to automate this for you, if you prefer: