Hacker, Concerned Citizen and Avid Reader. Interested in politics, economy and history. Founder & CEO of Melluli Technologies. Bengaluru, India


Install PHP Using HomeBrew on OSX El Capitan

This post briefly describes certain work arounds for issues, at the time of this writing, while installing php using HomeBrew on OSX El Capitan.

Apparently homebrew recipe taps for php have moved to a new location at the time of this writing. More details here - github-homebrew-php. The site clearly states that they have not tested on El Capitan despite being available for more then a year now. Well open source issues! You need a contributor for everything and of-course a need for someone to contribute!

Why upgrade php?

I have encountered a memory leak in regular expression api in php 5.5.31 (strange!) and i am attempting to upgrade to 5.6.20. Your reasons could be different - just to be up-to-date or experiment with a newer version. The good thing about using homebrew is that you can have multiple installations of php and switch between them by changing symlinks and apache php module path in apache configuration.

What is the installed php version?

Current php version checking…

$ php -v
PHP 5.5.31 (cli) (built: Feb 20 2016 20:33:10) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans

Ok! So thats 5.5.31

Configure taps

Ok lets get going. Run the following to configure brew taps for php. Some of the commands might not give any output (I’m not a homebrew expert). Before running any of these, a brew update is a good thing to do

$ brew update
$ brew tap homebrew/dupes
$ brew tap homebrew/homebrew-php
$ brew tap homebrew/dupes

If all goes well, that should set the taps for php. However i got a weird error about wrong permissions in some directory and i was also asked to execute a command to change those permissions (neat!). Here is what i was told to do and it worked (Might not be required for everyone and if required would be suggested by brew update.) -

$ sudo chown -R $(whoami):admin /usr/local

Cool. Run the brew update again and rest of the tap addition commands above and it should not complain.

What are the available php versions in homebrew?

Checking available PHP versions in HomeBrew…

$ brew search /php[0-9]+$/
homebrew/php/php53 homebrew/php/php54 homebrew/php/php55 homebrew/php/php56 homebrew/php/php70

Great. The version I need is available. Hurray!

Is current php installed through homebrew? (Good to know but does not matter)

In the previous output, it is clear that the php installed on my machine is not installed through homebrew. If it was then there would be a tick mark (✔) against one of the options. In my case, I know it is the bundled php from OSX. But php can also be installed by other options like PHP installers distributed by third parties. This information is not significant in our current exercise.

A Hack!

When I ran the brew install for php, I encountered an error during compilation of one of the dependencies downloaded along with php by brew. It was related to expecting a apache configuration file (httpd.conf) at a specific location which i think has changed in El Capitan and hence the error. So here is what i did -

$ cp /private/etc/apache2/original/httpd.conf /private/etc/apache2/httpd.conf

And phew! it worked!

Install php (with pear option)

$ brew install php56 --with-pear

If all goes well then we should see -

==> Summary
/usr/local/Cellar/php56/5.6.20: 489 files, 50.6M, built in 8 minutes 27 seconds 
Fix Apache httpd php module with new version

Change the apache httpd.conf file so that php5_module path looks like -

LoadModule php5_module /usr/local/opt/php56/libexec/apache2/libphp5.so 
Add new php version to command path

Add the following line at the end of your bash profile file located at *~/.bash_profile*

export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH"  

Now activate the new path by

source ~/.bash_profile
Test new version on command line!

By running “php -v” on terminal and we should see the new version number.

Brew installation output has lot of information to configure various tools and utilities to use new php and is a must read. The upgrade solved my memory leak problem. Yay!

Hurray! Adios!