Programming

Simple PHP Development Environment on Mac

4 Jun 2020 Amit Bhalerao
Simple PHP Development Environment on Mac

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!

Share

Written by

Amit Bhalerao
Amit Bhalerao
Project Manager

A Software Engineer by profession, Amit has been a passionate technology enthusiast since his college days. He has been with Ranium since its early days and brings with him over 12 years of experience in the field. His core expertise lies in PHP, and he is a Zend Certified PHP 5 Engineer. He aspires to build impactful open-source software that makes technology more accessible and inspires others to contribute to the community.

In his role as a Project Manager, Amit enjoys taking on new and challenging projects alongside his team. He is known for his calm and friendly nature, making it easy for teammates to approach him and ask for help.

Amit believes in celebrating the small joys of life and strives to lead a fulfilling life with his family. He enjoys traveling and never misses a chance to go on long bike rides. As a proud father, he now cherishes creating meaningful memories with his son while balancing his professional and personal passions.

Related Posts

PHP And Laravel Package For Fixer.Io
Programming

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.

13 Feb 2019 Abbas Ali

Want to talk about AI for your business?

Get in Touch Get in Touch