LITERAT

Fullstack Developer & Whitewater Kayaker & Scout

Switching easily PHP versions on Mac

How to easily switch PHP versions on MacOS with a oneliner

Switching easily PHP versions on Mac

From time to time I need to switch the PHP version in the console on my Mac. I do not use PHP CLI very often, especially when there is a Docker and Dockerfile present in a project and the exact version is set there.

But Docker support is not present everywhere and for quick and simple debugging is sometimes better to run composer scripts from the command line.

My main runtime is NodeJS right now so I am used to it. I like the nvm version switcher which I use very often. So for PHP I was looking for a similar tool.

Brew PHP Switcher

Brew PHP switcher is a simple script to switch your Apache and CLI configs quickly between major versions of PHP.

Installation:

brew install brew-php-switcher

Usage:

I am using CLI only, so I want to skip Apache

# skip apache only
brew-php-switcher 7.4 -s

You can find more information on Brew PHP Switcher GitHub

SPHP

Andy Miller aka rhukster wrote a handy little PHP switcher script sphp.

Installation:

curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw/adc8c149876bff14a33e3ac351588fdbe8172c07/sphp.sh > /usr/local/bin/sphp
chmod +x /usr/local/bin/sphp

Usage:

sphp 8.0

You can read some additional information on entire PHP stack setup on his blog.

Brew Link & Unlink

As a last resort you can also use brew to link and unlink PHP versions.

brew unlink php@7.4
brew link php@7.3

Some additional ideas on how to switch PHP versions on Mac are on StackOverflow. You can pick your favourite :-)

Conclusion

  • currently using brew-php-switcher as it is the easiest to use
  • sphp is my second bet
  • missing some config file like .nvmrc for defining PHP version for a project or the tools should read it from composer.json

References

I code on

Literat © 2008 — 2024