Installing PHP on your Mac

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

I've got a brand new Mac yesterday and noted that it states PHP will be removed from future Mac OS versions by default.
I'm pretty surprised they go this way. By default, it comes with PHP 7.3, and I needed 7.4 for my project so let me guide you through the process of setting up PHP on your Mac!
Mac's warning looks like this:
WARNING: PHP is not recommended
PHP is included in macOS for compatibility with legacy software.
Future versions of macOS will not include PHP.
However, don't be scared. It's quite easy these days to install PHP, and even install multiple versions if you like.
When it comes to installing software on your Mac, there is literally only one package manager we need, and it's Homebrew.
It can install any package or software you want and even install specific versions.
Read more on Homebrew - Package manager for Mac.
Quick guide: Run the following command in your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
To install PHP, we can run the following command:
brew install php
This will install the latest stable version of PHP (At the moment of writing, this is PHP 8.0).
Before running any brew commands, it's a good habit to run the following commands first. These will check if brew is all up to date and running the latest versions.
brew update
brew doctor
In my case, I wanted to install PHP 7.4 since it's the version our server is running.
To install a specific version, we can use the @ notation.
brew install [email protected]
This will run the installer, and it should end with a success notice in your terminal.
However, even though this installed PHP, it didn't change our running instance yet.
So if we run the php -v command, we might still see a different version like PHP 7.3.14 (CLI) or whatever you have installed.
To fix this, we need to link the correct PHP version.
Now that we installed versions, we can easily switch between them using the link command.
First, check which version of PHP is currently running:
php -v
# PHP 8.0.1 (cli) (built: Jan 8 2021 01:27:28) ( NTS )
# Copyright (c) The PHP Group
Then we can unlink that version by using:
brew unlink [email protected]
The next step is to link the version we want:
brew link [email protected]
It will tell you to run a script to add the path:
echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc
This will make sure the right PHP version is loaded, now if you run php -v again it should show:
# PHP 7.4.14 (cli) (built: Jan 8 2021 01:35:35) ( NTS )
# Copyright (c) The PHP Group
And there we go, we switched to the PHP version.
I had the issue when upgrading from 7.4 to 8.0 for my demo that I kept seeing 7.4 when running php -v. To fix this manually, remove the old line in your .zshrc file.
nano ~/.zshrc
Remove the line that points to your old instance of PHP.
export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"
Note: This is an example of my version. It might differ from what you installed before.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter