loading...

Switching between multiple PHP versions on macOS

markhesketh profile image Mark Hesketh Originally published at markhesketh.com on ・2 min read

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

Posted on Feb 10 by:

markhesketh profile

Mark Hesketh

@markhesketh

🚀PHP Developer. Laravel enthusiast. Northerner.

Discussion

markdown guide
 

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 🤘

 

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.