Saturday, June 30, 2018

Upgrade PHP version from 5.6 to 7.2 on macOS Sierra (10.12)

Upgrade PHP version from 5.6 to 7.2 on macOS Sierra (10.12)

The PHP version 7.0.0 is released on 03 Dec 2015. It has been three years since then, however, the PHP version installed originally on macOS Sierra (10.12) is still 5.6. I would like to upgrade PHP version in my mac, here are the upgrade procedures to the version 7.2.

First, let’s find out the PHP version number.

$ php -v
PHP 5.6.30

Actually , There are two ways to upgrade PHP.

1. Homebrew

Homebrew is a famous package manager for macOS. It is very easy to use and very powerful.

First, let’s find out which PHP version is available in Homebrew’s package list.

$ brew search php7
==> Searching local taps...
php@7.0   php@7.1    php@7.2

Let’s install the latest version ‘7.2’. Just excute the following commands to install.

  • a. Update the brew package first.
$ brew update && brew upgrade
  • b. Error messages occurs with following commands. Actually, these commands are no longer needed. Therefore, they can be ignored.
$ brew tap homebrew/dupes
$ brew tap homebrew/versions
$ brew tap homebrew/homebrew-php

p.s. Error message: some of the taps are no longer needed.

$ brew tap homebrew/dupes
Error: homebrew/dupes was deprecated. This tap is now empty as all its formulae were migrated.
 $ brew tap homebrew/versions
Error: homebrew/versions was deprecated. This tap is now empty as all its formulae were migrated.
$ brew tap homebrew/homebrew-php
Error: homebrew/php was deprecated. This tap is now empty as all its formulae were migrated.
  • c. Unlink the old version of PHP if it was installed with Homebrew.
$ brew unlink php56 
  • d. Install the new version of PHP.
$ brew install php72

After installation, it shows:
To enable PHP in Apache add the following to httpd.conf (/etc/apache2) and restart Apache:

  • e. Using vim edit httpd.conf of apache2.
$ sudo vim /etc/apache2/httpd.conf

If you don’t like to edit using vim, go to Finder and type:

$ open /etc/apache2/

Search “php5_module” then commend out the statements as follows:

#Comment out the PHP5 module
#LoadModule php5_module libexec/apache2/libphp5.so

Add the commands as follows:

#Enable PHP 7 module
LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>
  • f. Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
/usr/local/etc/php/7.2/

To have launchd start php now and restart at login:

$ brew services start php

Or, if you don’t want/need a background service you can just run:

$ php-fpm
==> Summary
 /usr/local/Cellar/php/7.2.7: 515 files, 78.9MB
  • g. Restart Apache service
$ sudo apachectl restart
  • h. check the php version:
$ php -v
PHP 7.2.7 (cli) (built: Jun 22 2018 06:29:00) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

Done!


Other brew commands for reference

  • brew tap: to list and update the repositories exist in local computer.
$ brew tap
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
No changes to formulae.
  • brew tap (user)/(repo): to download the 3rd party formula repositories (user)/(repo). If you want to install a package listed in a certain formula repository, the formula repository must be tapped first.

  • brew untap (user)/(repo): delete the 3rd party formula repositories (user)/(repo).

  • brew cleanup: a little tip, clean up old downloads and the files from the Homebrew download-cache are deleted.

$ brew cleanup
Removing: /Users/xxxx/Library/Caches/Homebrew/node-10.4.1.sierra.bottle.tar.gz... (16MB)
   :
   :
==> This operation has freed approximately 5.4GB of disk space.

5.4GB! It’s quite a lot of space.

  • brew doctor: fix your Homebrew repository.

2. CURL

Another option is to execute a curl command provided by php-osx.liip.ch which downloads the binary package from the website and installs it to my mac. This one-line installation is for PHP 7.2 (Current stable) and macOS 10.10 and later.

curl -s https://php-osx.liip.ch/install.sh | bash -s 7.2
  • One little problem mentioned on the php-osx.liip.ch website is: php -v echoes the old version number after installation. The explanation given by php-osx is:

php-osx doesn’t overwrite the php binaries installed by Apple, but installs everything in /usr/local/php5. The new php binary is therefore in /usr/local/php5/bin/php.

You can also adjust your PATH do include that directory, eg. write into your ~/.profile file the following

The solution is to add the path /usr/local/php5/bin to global variable $PATH.

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

I choose the first method since it is really convenient to manage and install packages utilizing Homebrew.

3 comments:

  1. hi.. thanks for the post. I tried to follow the first method but I am getting some error while it tries to install some dependencies for php 7.2

    DownloadError: Failed to download resource "openldap"
    Download failed: https://www.openldap.org/software/download/OpenLDAP/openldap-release/openldap-2.4.49.tgz

    This is kind of a dead end to me. I tried to find the solution online but no luck. You think you can help me with this?

    Thanks in advance.

    Cheers

    ReplyDelete
  2. Thanks Charles. I tried the second method and it worked like a breeze after modifying the PATH through EXPORT. Thanks for also explaining the reason for do the export. Often when we do installations, we are like blind men groping. An explanation like this is helpful. Thanks.

    ReplyDelete