DEV Community

PostSrc
PostSrc

Posted on • Originally published at postsrc.com on

How to easily switch between PHP versions on macOS?

How to easily switch between PHP versions on macOS? Cover

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
Enter fullscreen mode Exit fullscreen mode

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 "php@7.3", "php@7.4" 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 php@7.4
brew install php@7.3
brew link php@7.3
php -v
Enter fullscreen mode Exit fullscreen mode

Otherwise, if it's from PHP version 7.4 to 8.0 all you have to do is specify the version number.

brew unlink php@7.4
brew link php
php -v
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

This post is originally published at PostSrc. If you like this kind of tutorial, I would really appreciate it if you give it a visit.

Top comments (1)

Collapse
 
leslieeeee profile image
Leslie

Have you tried ServBay.dev?
It's a much easier tool for PHP developers, providing a user-friendly experience, especially for beginners. It supports all versions of PHP, MariaDB, PostgreSQL, as well as Redis and Memcached. You can run multiple PHP instances simultaneously and switch between them effortlessly. It also offers easy updates without the need to configure environment variables. This tool has greatly simplified my PHP development and is definitely worth trying!