4 Jun 2020 / Amit Bhalerao

Simple PHP Development Environment on Mac

Article-banner-new.jpg

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.

  1. cd to the Homebrew’s formula directory

    cd /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula

  2. Find desired commit (version 64 for icu4c) to checkout

    git log --follow icu4c.rb

  3. Checkout to a new branch

    git checkout -b icu4c-64 896d1018c7a4906f2c3fa1386aaf283497db60a2

  4. Reinstall the library with the new version

    brew reinstall ./icu4c.rb

  5. Switch to the reinstalled version

    brew switch icu4c 64.1

  6. 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!

Amit Bhalerao

Amit Bhalerao

Project Manager

A Software Engineer by profession, Amit has always been a software enthusiast since his college days. He has been with Ranium since the early onset of the company and brings with him over 10 years of experience. His expertise lies in PHP and he is a Zend Certified PHP 5 Engineer. He wishes to someday create open-source software to make them more accessible and to inspire others to do the same.

As a project manager, he is always keen on tackling new and challenging projects with his teammates. Amit has a very calm and simple demeanour which always ensures that his teammates feel encouraged to reach out to him with their difficulties.

Amit likes to celebrate the small joys in life and aspires to live a life that makes him and his family feel content. He likes to travel and takes every opportunity to pull out his bike and go on drives with his equally travel enthusiast wife.

Related Posts

PHP And Laravel Package For Fixer.Io
13 Feb 2019 Abbas Ali

PHP And Laravel Package For Fixer.Io

Fixer.io is a simple and lightweight API for current and historical foreign exchange rates. We came across this wonderful service while developing a recent project. I searched but couldn’t find any Fixer.io PHP client fitting our needs for the project so I decided to build a PHP and Laravel package for the same.
read more