Installing PHP with Homebrew
Homebrew installs the latest version of PHP on your Mac with a single, short command.
But what if the project you work on requires a version of PHP that Homebrew no longer provides?
Scenario
You want to use PHP 8.5 for a new project, but you also need PHP 7.4, for example for a legacy project that you have not upgraded yet or for a package that still supports it. Most of the packages I maintain in the ergebnis organization support PHP 7.4 and onwards.
Installing the latest version of PHP
When you have not installed Homebrew yet, follow the instructions on its website.
Running
brew install php
installs the latest version of PHP. Running
php -v
yields
PHP 8.5.12 (cli) (built: Oct 19 2026 17:02:46) (NTS)
Copyright (c) The PHP Group
Built by Homebrew
Zend Engine v4.5.12, Copyright (c) Zend Technologies
with Zend OPcache v8.5.12, Copyright (c), by Zend Technologies
Installing an older version of PHP
At the time of writing, Homebrew provides the formulae php for PHP 8.5, as well as php@8.4, php@8.3, php@8.2, and php@8.1. php@8.1 is deprecated because PHP 8.1 is no longer supported, and Homebrew will disable it on December 31, 2026. There is no formula for PHP 8.0 or older.
For older versions of PHP, you can use shivammathur/php, a Homebrew tap maintained by Shivam Mathur. A tap is a third-party repository of formulae. This tap provides formulae for PHP 5.6 up to the development versions of PHP 8.6 and PHP 8.7, and I use it on all of my Macs. It supports macOS on Apple silicon, but not on Intel.
Running
brew tap shivammathur/php && brew trust shivammathur/php
adds the tap and allows Homebrew to load formulae from it. Running
brew install shivammathur/php/php@7.4
installs PHP 7.4. The formula php@8.4 exists in both Homebrew and the tap, so use the full name shivammathur/php/php@8.4 when you want the formula from the tap.
Keep the formula php from Homebrew. In the tap, php@8.5 is an alias for its own php, which would replace the php from Homebrew that the formula composer depends on.
Using an older version of PHP
Running
php -v
after installing PHP 7.4 still shows PHP 8.5, because Homebrew does not link versioned formulae into /opt/homebrew/bin.
You can run PHP 7.4 without linking it. Running
/opt/homebrew/opt/php@7.4/bin/php -v
yields
PHP 7.4.33 (cli) (built: Oct 19 2026 20:59:20) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies
When you want php to refer to PHP 7.4, unlink the current version and link PHP 7.4.
brew unlink php && brew link --force --overwrite php@7.4
When you do this more than once a day, take a look at Switching PHP versions when using Homebrew.
Installing extensions
shivammathur/extensions is a second tap with formulae for PHP extensions. Each formula is specific to a version of PHP.
Running
brew tap shivammathur/extensions \
&& brew trust shivammathur/extensions \
&& brew install shivammathur/extensions/xdebug@7.4
installs Xdebug for PHP 7.4 and adds its configuration to /opt/homebrew/etc/php/7.4/conf.d.
When you want to switch Xdebug on and off, take a look at Quickly switching Xdebug on and off when using Homebrew.
Updating PHP
Running
brew upgrade php shivammathur/php/php@7.4
updates both versions of PHP to their latest patch release.
Keep in mind that a patch release of PHP 7.4 is no longer on its way: PHP 7.4 has reached the end of its lifecycle, and it does not receive security fixes. The tap allows you to run it locally while you upgrade a legacy project or test a package, but it does not make PHP 7.4 safe to run in production.
Recording what you have installed
When you set up your next Mac, you need to remember every tap and formula you have installed. A Brewfile lists them instead.
tap "shivammathur/extensions", trusted: true
tap "shivammathur/php", trusted: true
brew "php", link: false
brew "shivammathur/extensions/xdebug@7.4"
brew "shivammathur/extensions/xdebug@8.5"
brew "shivammathur/php/php@7.4", link: true
trusted: true replaces running brew trust, and link states which version of PHP Homebrew links.
Running
brew bundle install --file=Brewfile
installs everything that the Brewfile lists.
brew bundle matches a formula by its name alone. When you have already installed php@8.4 from Homebrew, brew bundle considers shivammathur/php/php@8.4 as installed, so uninstall php@8.4 first.
I keep my Brewfile in a dotfiles repository, next to .zshrc and the other configuration files of my shell. A script in that repository links the configuration files and runs brew bundle, so all of my Macs have the same versions of PHP.
Have you found a better option for installing PHP on your Mac?