Upgrade the PHP CLI to 7.4 on Mac
December 8, 2019
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.
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
.zshrc. These are typically located in your home (
$ cd ~ $ vi .zshrc
Locate the following (or similar) line...
... and change it to:
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/phpis a symlink pointing to
/usr/local/Cellar/php/7.4.0which is the same location that was linked by Homebrew above.
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 )