read

Updated 20th August 2016

You can still follow these guidelines below, however Jack Skinner has posted up an easier way to install PHP7 using homebrew here:

https://developerjack.com/blog/2015/12/11/Installing-PHP7-with-homebrew/

Back to the original post

If, like me, you are keen to work with the latest PHP syntax on the latest Mac OS operating system — and especially if you are trying to keep up with the rapidly moving world of Laravel development — you will have noticed that there have been some recent changes that make this more difficult.

Most importantly, there is now a restricted mode on system files — even sudo statements will not be able to change these files, meaning that even if you can get PHP5.6+ installed, the chances of your system recognising it are considerably reduced.

Fortunately there is a way to override it, here's how.

Installing PHP5.6+ on El Capitan step-by-step

Step One: install PHP

You can download a binary of the latest version of PHP from here:

http://php-osx.liip.ch/

There are also more detailed installation instructions if you need them. Here's the TL;DR version. Open up a terminal window and copy & paste the following command in.

curl -s http://php-osx.liip.ch/install.sh | bash -s 5.6

This will download the PHP binary and begin installing it.

The new version of PHP doesn't replace the system one in /usr/bin - it's not allowed to. Instead it installs itself to /usr/local/php5/

Step Two: Tell El Capitan to use the new PHP

It is possible to do this by changing your PATH environment variable, however I didn't find much success with this.

You can permanently change the PATH with this command

export PATH=/usr/local/php5/bin:$PATH

If that doesn't work out for you, you have to go under the hood and change the file reference in /usr/bin

  • Reboot into Recovery Mode (reboot and hold down cmd-R when you hear the chime
  • Launch Terminal from the Utilities menu
  • Enter the following command. This will tell OS X to switch off the restricted permissions. We will do this temporarily. csrutil disable; reboot

The system will now reboot

  • Open up a terminal window and run the following two commands. You will need to supply your normal user password for them to run properly. These commands copy the old php folder to a safe place (~php) and then link the name php to the new php folder.

sudo mv /usr/bin/php /usr/bin/~php

sudo ln -s /usr/local/php5/bin/php /usr/bin/php

  • Now to clear up. Reboot back into Recovery Mode
  • Launch Terminal from the Utilities menu
  • Enter this command to re-enable the restricted permissions. csrutil enable; reboot

The system will reboot again and if you type

php -v

into a terminal window, you will find that the default version of PHP is now the new version you have installed.

Gotchas

System Timezone is not set

To change this you will need to edit your php.ini file. This can be found in /usr/local/php5/lib

Edit the file and search for 'zone.' You should come across an entry that looks like this:

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
; date.timezone = ''

remove the semicolon before date.timezone and put a value in the quotes. In my case it is:

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = 'Europe/London'  

You can get a list of timezones here: http://php.net/manual/en/timezones.php

Xdebug nesting error

My Laravel build was throwing some peculiar errors that could be traced back to the Xdebug configuration. If you get a message like this:

FatalErrorException in MySqlGrammar.php line 139: Maximum function nesting level of '100' reached, aborting!

then you need to edit your xdebug.ini file.

This and a number of other php extension config files can be found here

/usr/local/php5/php.d/

Open up the file 50-extension-xdebug.ini and add the following line to the end

xdebug.max_nesting_level=500

That will resolve it. You may need to logout and log back in for PHP to refresh its configuration.

memcached

The binary install of php5.6 doesn't include memcached by default, however installing it is very straightforward. Here are the instructions, taken from http://php-osx.liip.ch/

sudo /usr/local/packager/packager.py install tools-memcached

This will install memcached and you can use it pretty much right away.

Hopefully this brief guide will help you get your PHP5.6+ install up and running quicker than I did.

Robin

Blog Logo

Robin Layfield

Cotswolds-based web developer digs CodeIgniter, Laravel, PHP, jQuery, Raspberry Pi & MySQL. Makes a living cutting code into tiny bits.


Published

Image

UltraSimplified blog

Thoughts from the other end of the wire.

Back to Overview