Upgrade the PHP CLI to 7.4 on Mac

by webmaster 2019-12-08 #mac #php
Upgrade the PHP CLI to 7.4 on Mac

On Mac, PHP can be easily upgraded to 7.4 with Homebrew. However, the command line may continue to show the previous version. Here's how to fix that.

Upgrade to PHP 7.4 with Homebrew on Mac is a very succinct article by @brendt_gd that boils it down to two simple commands: brew update and brew upgrade php.

The problem I ran into was that my PHP CLI in the terminal remained linked to the previous version. Checking the version, before and after running the brew command produced the same result:

$ php -v
PHP 7.2.9 (cli) (built: Aug 21 2018 07:42:00) ( NTS )

Just to make sure 7.4 was actually installed, I ran the upgrade command again, then checked the actual location of PHP 7.4:

$ brew upgrade php
Warning: php 7.4.0 already installed

$ ls /usr/local/etc/php/7.4
OK

To switch the PHP CLI to 7.4, first I ran Homebrew's unlink/link command:

$ brew unlink php && brew link php

This should produce an output similar to this:

Unlinking /usr/local/Cellar/php/7.X... XX symlinks removed
Linking /usr/local/Cellar/php/7.4.0... 24 symlinks created

Finally, you need to export the proper path variable for the PHP executable in either .bashrc or .zshrc. These are typically located in your home (~) folder:

$ cd ~
$ vi .zshrc

Locate the following (or similar) line...

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

... and change it to:

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

Note
If you list the PHP executable...

$ ls -al /usr/local/bin/php
/usr/local/bin/php -> ../Cellar/php/7.4.0/bin/php

... you'll notice that /usr/local/bin/php is a symlink pointing to /usr/local/Cellar/php/7.4.0 which is the same location that was linked by Homebrew above.

Finally, run source .zshrc to get the terminal to update its configuration.

For good measure, close the terminal window and open a fresh one. If you now run php -v you should be rewarded with this:

$ php -v
PHP 7.4.0 (cli) (built: Nov 29 2019 16:18:44) ( NTS )
Liked this article? Share it on your favorite platform.