Of how I managed to update PHP on High Sierra

High Sierra OS comes with PHP 7.1.1 installed.

If you ever used only local installations with Apache-PHP-MySql to run your websites you probably installed PHP by your own in the previous OS versions (like Yosemite, for instance) so updating to High Sierra could lead to a (false) assertion like the one I did: «But… it’s not true that HighSierra updated PHP to 7.* because I still have PHP 5.6».

There’s the rub!

You have to know that MacOS has his own pre-installed version of Apache and PHP (since 10.13 I guess) whose source codes reside in:

APACHE: /usr/libexec/apache2 (for the modules, that’s why in /etc/apache2/httpd.conf you find modules references to libexec) | /etc/apache2
PHP: /usr/bin/php

If no local PHP version is installed this /usr/bin/php will the path of PHP of the system.

And here’s why that, although I updated the OS, running php -v it was still showing me 5.6 version info.

That’s because when you install via CLI PHP, basically through commands like curl -s http://php-osx.liip.ch/install.sh | bash -s 7.1 (https://tecadmin.net/install-php-macos/), the script will install it in /usr/local.

We should also be aware of what’s happening when these commands are run to better understand:

FIRST) a folder with the php (long) version name is created inside /usr/local/ (something like php5–7.1.13–20180201–134129)

SECOND) as the default reference folder of php in Mac has always been php5, if this is not present in /usr/local it will create a symlink called php5 which links our new downloaded PHP updated version folder. And pay attention to this, it must be a symlink and that’s the correct behavior because when we’ll update next to a new version it will automatically update the symlink as well. If by chance (or because of someone too much curious 😏 that touches things he/she shouldn’t like… me 🤩) is a folder, it won’t be able to create the correct symlink and the new version will never be set.

THIRD) You may have run in past, because you found it in the guide for “Update your PHP version on Mac”, a command like this:

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

OK. Don’t do it anymore. If you have this symlink in usr/local/bin remove it.
This tells the system which php exec it should use, which in this case is /usr/local/php5/bin/php, the correct one by the way, but it’s way much better to set it as a path in your .bash_profile by adding this line:

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

In this way is much faster changing the PHP version path in use by simply editing this.
Furthermore, the symlink put in /usr/local/bin/ precedes the one exported in .bash_profile, so you may struggle for no reason trying to understand why the export is not working (that’s what I did).
I think this way it’s cleaner and easier to manage.

Wrapping up, with no symlink in /usr/local/bin, with the export path set in .bash_profile, here’s how we tell it to use our new installed version of PHP instead of the system one. But if you comment the export in the .bash_profile here’s that now your using the system version back.

No more fear from now on when updating PHP!

I also suggest you to have a look on https://php-osx.liip.ch/, it tells interesting things like:

😏

Curious developer and books addicted.

Curious developer and books addicted.