DEV Community

Mark Hesketh
Mark Hesketh

Posted on • Originally published at markhesketh.com on

Switching between multiple PHP versions on macOS

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.

Using multiple 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 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.

2. Install Multiple Versions of PHP

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 php@5.6
brew install php@7.0
brew install php@7.1
brew install php@7.2
brew install php@7.3
brew install php@7.4

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 php@7.4
brew link php@5.6 --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:

Source

Top comments (3)

Collapse
 
leslieeeee profile image
Leslie

Have you tried using ServBay?
It is a good tool, especially for the beginners. It handles all PHP, MariaDB, PostgreSQL versions, plus Redis and Memcached. Run multiple PHP instances simultaneously and switch easily. This tool has made my PHP dev simpler. Worth a shot!

Collapse
 
manuelojeda profile image
Manuel Ojeda

Have you tried using Laravel Valet?

After installing an specific PHP version you can sweet between using:
valet use <version>

Example:
valet use 7.2
valet use 7.4

Nice post 🤘

Collapse
 
markhesketh profile image
Mark Hesketh

Thanks Manuel!

Yeah I've used valet in the past but forgotten the use command swapped the CLI PHP version too. Thats really cool. I might have to reinstall it now.