We have quite a few ways to install different versions of PHP in a local development environment. As a developer, we need all those versions to serve different projects. Recently I tried setting up Laravel Valet on my Mac. So this is all about setting and using different PHP versions with valet.
Well, all this is already available in the official laravel documentation. I am only adding my installation experience here.
Installing PHP & MySQL
HomeBrew makes it easy to install php and MySQL on Mac. It is as simple as
brew install mysql
brew install php
This of course installs the latest version of php ( current is 7.4.x )
To install earlier versions,
brew install [email protected]
brew install [email protected]
That was pretty simple. Then I tried installing php 7.1 the same way but it failed.
After a few google searches, I found that Homebrew doesn’t support the deprecated versions by default. Yes, php 7.1 was deprecated in December 2019, and php 7.2 will be the next to go in November 2020.
You need to run a brew tap to allow deprecated versions.
brew tap exolnet/homebrew-deprecated
Then I was able to install even php 5.6 - which perhaps was needed for a project :)
Switching PHP versions
PHP versions can be switched using homebrew unlink and link to the required version. This is even easy with Valet.
valet use [email protected]
And php -v shows the switched version immediately. However, in web-browser, the phpinfo didn’t display the switched version. The valet stop and start did not work. I was able to see the updated version in the browser only after restarting the mac.
Well, that was not all. There was another issue while switching PHP versions. The icu4c libraries required by these PHP versions are different causing the error as below
php -v
I found a solution here. Thanks to SO. I followed these instructions and it worked.
-
cd to the Homebrew’s formula directory
cd /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula
-
Find desired commit (version 64 for icu4c) to checkout
git log --follow icu4c.rb
-
Checkout to a new branch
git checkout -b icu4c-64 896d1018c7a4906f2c3fa1386aaf283497db60a2
-
Reinstall the library with the new version
brew reinstall ./icu4c.rb
-
Switch to the reinstalled version
brew switch icu4c 64.1
-
Checkout back to master
git checkout master
Hope this helps in setting up your PHP development environment using Valet. Please refer to the official Valet documentation for more details.
Cheers!