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.

Switching between multiple PHP versions on macOS
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 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 between multiple PHP versions on macOS
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:

  • phpbrew/phpbrew
  • philcook/brew-php-switcher

Nguồn : https://www.markhesketh.com

Đánh giá post

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Hotline: 0974.0707.83(Zalo/Viber)