Here is the solution to a problem faced by any PHP developer that has upgraded their Mac to Monterey only to then re-attempt to run their PHP-based application afterwards and get back the dreaded response:
php: command not found
That’s not good! Did Apple get rid of PHP in their latest operating system? Surely not!Me – an hour after upgrading my MacBook
Oh, I see, that’s exactly what they did.Me – 5 minutes later after Googling it
Right, so, how do we fix it?
First things first, make sure you have Homebrew installed. Chances are you already do, it being similar to
apt for most Linux distros, but it’s best just to check.
If you get back something that looks like the below, then you have it installed, otherwise you’ll need to go and install it.
Homebrew 3.5.9 Homebrew/homebrew-core (git revision 3eda28188e5; last commit 2022-08-22) Homebrew/homebrew-cask (git revision eacfe8f6c1; last commit 2022-08-22)
Right, so now, just
brew install php right? Unfortunately, this won’t work in quite the way you’re expecting, so it’s best now to install a formula that actually allows for more versions of PHP to become available, than were available on older versions of MacOS anyway:
brew tap shivammatur/php
Now you can install it, the only caveat is that you need to specifically choose the version (unless it’s 8.0).
brew install email@example.com
Other options available are:
- 8.0 – as above, you can remove the
@part for this version
Now you need to link it to the
brew link --overwrite --force firstname.lastname@example.org
Where possible of course, I’d usually recommend using the latest available version.
Now all these changes mean we need to restart our terminal before PHP will work, so once you’ve done that run
php -v and you should get back something like the below:
PHP 8.1.9 (cli) (built: Aug 4 2022 15:11:08) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.9, Copyright (c) Zend Technologies with Zend OPcache v8.1.9, Copyright (c), by Zend Technologies
What if I want to change my PHP version?
You can change the PHP version that is installed and linked, but you will need to restart your terminal again after doing so. Let’s say I want to switch back to 7.4 for some reason:
brew install email@example.com
(You will only need to do the install step once.)
brew unlink php && brew link --overwrite --force firstname.lastname@example.org
Of couse, you need to unlink the command from its current destination before you can link it somewhere else!
But, wait, couldn’t I just use Docker?
In most cases, indeed you could! I’d actually recommend this method wherever possible, as you can spin up an image in moments with all the necessary dependencies, configurations, and of course PHP version for your project. However, my situation is rather unique (although maybe not as unique as I think) in that when accessing private Gitlab repositories for my company, even so far that I have to be connected to the company’s VPN to even access the login page, your SSH keys that are on your machine can’t then be used by a Docker container even if it is running on that same machine.
Docker also givs you the advantage of offering more versions. A lot more versions. A huge number of versions! A cursory glance at the tags available on the Docker Hub should give you an idea.
Is there a roadblock preventing you from using Docker, or a very good reason why you shouldn’t? Perhaps you haven’t learned Docker yet, luckily the learning curve is easy for beginners to get onto so I may well do an article on that as well (maybe even a video, I haven’t decided yet). If there is such a reason or roadblock, then go through the process I’ve described above. Otherwise, Dock away!
Oh, and, even if you do go down the Docker route, I’d still suggest at least completing the Homebrew step at the start of this post. A Mac without Homebrew is a bit like an internal combustion car without a gearbox, it will still work, but not nearly as well as it should!