Fullstack Developer & Whitewater Kayaker & Scout
How to easily switch PHP versions on MacOS with a oneliner
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 is a simple script to switch your Apache and CLI configs quickly between major versions of PHP.
brew install brew-php-switcher
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
Andy Miller aka rhukster wrote a handy little PHP switcher script sphp
.
curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw/adc8c149876bff14a33e3ac351588fdbe8172c07/sphp.sh > /usr/local/bin/sphp
chmod +x /usr/local/bin/sphp
sphp 8.0
You can read some additional information on entire PHP stack setup on his blog.
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 :-)
brew-php-switcher
as it is the easiest to usesphp
is my second bet.nvmrc
for defining PHP version for a project or the tools should read it from composer.json