Many times I found me in the scenario where I need to use different versions for PHP at the same time for different projects. So here is a tiny guide of how to do this in the easy way.
First install different versions for php using Homebrew (If you haven’t installed Homebrew, just follow the at https://brew.sh/), to install this use:
brew install email@example.com brew install firstname.lastname@example.org brew install email@example.com brew install firstname.lastname@example.org
Now you have to install sphp, you can do this with this script:
curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw > /usr/local/bin/sphp
Once it is downloaded in your computer just make it executable:
chmod +x /usr/local/bin/sphp
Cool, now restart your terminal and try to switch between versions with
Switching to email@example.com Switching your shell Unlinking /usr/local/Cellarfirstname.lastname@example.org/5.6.36… 0 symlinks removed Unlinking /usr/local/Cellaremail@example.com/7.0.30… 0 symlinks removed Unlinking /usr/local/Cellarfirstname.lastname@example.org/7.1.32_1… 0 symlinks removed Unlinking /usr/local/Cellaremail@example.com/7.2.30_1… 0 symlinks removed Unlinking /usr/local/Cellarfirstname.lastname@example.org/7.3.18… 25 symlinks removed Unlinking /usr/local/Cellar/php/7.4.6… 0 symlinks removed Linking /usr/local/Cellar/php/7.4.6… 24 symlinks created You will need sudo power from now on
Test your php version:
php --version PHP 7.4.6 (cli) (built: May 14 2020 10:40:11) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.6, Copyright (c), by Zend Technologies
I will be updating this article if I found some issues and some other troubleshooting tips.
If the php version has not changed check first where does your php executable came from with, to work it should show something like this:
which php /usr/local/bin/php
Check your ~/.bash_profile to be sure there no other php version loading after sphp. If there is some PHP loading just comment it, and restart your Terminal.