Install or Update PHP on MacOS with Homebrew

Video thumbnail

In this article, we are going to learn how to install and update PHP on macOS using Homebrew, the simplest and most recommended way today. Although macOS includes Apache and a version of PHP by default, it is usually outdated, which prevents working with modern frameworks like Laravel that require recent versions of PHP.

By default, Mac comes with both Apache and the PHP module installed; however, the version it provides is generally quite old, and therefore we need to update it to use technologies like Laravel, which demand more and more over time.

Check the installed PHP version on macOS

Before installing or updating PHP, the first step is to verify which version you currently have:

$ php -v

This command will display the PHP version that your operating system is using at that moment.

Why use Homebrew to install PHP on Mac?

There are several ways to install PHP on macOS (official packages, manual compilation, etc.), but Homebrew offers great advantages:

  • Fast and automated installation
  • Possibility of having multiple PHP versions
  • Simple switching between versions
  • Easy updates

That is why Homebrew is the most used option by professional developers.

Install PHP on Mac using Homebrew

If you already have Homebrew installed, you can install PHP with the following command:

$ brew install php

This command will install the latest stable version of PHP available in Homebrew.

Install a specific version of PHP

If you need a specific version (for example, PHP 8.5), you can do it like this:

$ brew install php@8.5

Currently, Homebrew usually offers newer versions such as PHP 8.1, 8.5, or higher.

Link PHP to the system (brew link)

After installing PHP, you need to link it so that macOS uses it as the primary version:

$ brew link php

Or if you installed a specific version:

$ brew link php@8.5

If another version is already linked, you can force the link with:

$ brew link --overwrite --force php@8.0

Update your PHP version

Now, to update PHP on our systems, we have several variants ranging from manual installations, but we are going to use Homebrew since it keeps everything automated and simple to use: we are going to install the tap or formula.

As you can see in the official documentation, you can install several versions of PHP; I prefer the latest one, so I simply enter:

brew install php

Or at the time this video was recorded, the latest one is:

brew install php@8.5

Both commands are equivalent: Since the previous command brew link php will install the latest version for you.

And with this, we have PHP installed; now we have to link it to the system:

Switching between different versions of PHP on macOS

For that, we use the link option:

$ brew link php@8.0 
$ brew link php

One of the biggest advantages of Homebrew is being able to easily switch between PHP versions.

Example:

$ brew unlink php 
$ brew link php@8.1

This is especially useful if you work with multiple projects that require different versions.

See which versions of PHP you have installed

You can install several versions of PHP on your systems and switch from one to another easily using the link wildcard; however, to do that, you need to see which versions you have; for this, we have the search option where you type part of the name of the package you want to find:

$ brew search php

You can also see which versions you have installed with:

$ brew list | grep php

And that's it, as this allows you to work perfectly with PHP on macOS.

We can NO longer return from the brew option to the previous version or the operating system's version.

Important considerations

  • Once you install PHP with Homebrew, you cannot easily go back to the PHP version included in macOS.
  • Homebrew completely manages your PHP installation from that point forward.
  • It is the recommended way for modern local development.
  • Remember that if at any point while running a brew command you see an error like command not found: brew, you need to configure the brew command in the Operating System PATH.

Common error: command not found: brew

If you see the following error when running a Homebrew command:

command not found: brew

It means that Homebrew is not configured in your system's PATH.

Quick solution (for macOS with Apple Silicon):

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc 
eval "$(/opt/homebrew/bin/brew shellenv)"

On Macs with Intel:

echo 'eval "$(/usr/local/bin/brew shellenv)"' >> ~/.zshrc 
eval “$(/usr/local/bin/brew shellenv)”

Conclusion

Now you know how to install, update, and switch PHP versions on Mac using Homebrew. With this setup, you will be able to work smoothly with Laravel, Symfony, WordPress, or any other modern framework.

If you want to learn Laravel professionally, don't forget to check out my full course.

We're going to learn how to update our PHP on our Mac OS operating system; by default, Mac comes with both Apache and the PHP module installed.

I agree to receive announcements of interest about this Blog.

Andrés Cruz

ES En español