Fullstack Developer & Whitewater Kayaker & Scout
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
curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw/adc8c149876bff14a33e3ac351588fdbe8172c07/sphp.sh > /usr/local/bin/sphp chmod +x /usr/local/bin/sphp
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 email@example.com brew link firstname.lastname@example.org
Some additional ideas on how to switch PHP versions on Mac are on StackOverflow. You can pick your favourite :-)
brew-php-switcheras it is the easiest to use
sphpis my second bet
.nvmrcfor defining PHP version for a project or the tools should read it from