Posts Learn Categories Tags About
How to easily switch between PHP versions on macOS? cover

How to easily switch between PHP versions on macOS?

Learn how to easily switch between PHP versions on macOS using Homebrew command line

5 months ago

8 mins read


In this post, we'll learn how to switch between PHP versions in the macOS system. Firstly I assume that PHP is installed using Homebrew and if you have not done so, do follow this tutorial on How to install PHP 8 on macOS Big Sur using Homebrew.

Using Homebrew
By right when using Homebrew, we can install multiple different versions of PHP at the same time but only use it one version at a time. To see which PHP version you have installed do run the command below and if you see it's ticked ✔︎ then it means it's installed.
brew search php

If none of the PHP is ticked then by default the PHP that's on the system is the default one shipped with macOS. Installing different versions is very important as many projects use a different version of PHP. In this case, let's assume that we have version 7.4 and 8.0.

By default PHP version on Homebrew is "[email protected]", "[email protected]" and "php" which means that the PHP that has no version number is the latest / highest version, in this current time of writing is version 8.0.

Changing PHP Version
To switch to the PHP version you can simply do run the command below. Switching from PHP version 7.4 to 7.3.
brew unlink [email protected]
brew install [email protected]
brew link [email protected]
php -v

Otherwise, if it's from PHP version 7.4 to 8.0 all you have to do is specify the version number.
brew unlink [email protected]
brew link php
php -v

Warning when switching
Sometimes there will be a warning while switching mentioning that linking should use "force" and if that's the case do use the flag option.
brew link php --force

That's all, by now you should be able to change to any PHP version that your project requires. One piece of advice is that it's better to have a project to always stay up to date so having maintained aka latest PHP version is always a good practice.