Tutorial

Quick Tip: Change PHP Versions with Homebrew on Apple Silicon Powered Macs

When you change PHP versions with Homebrew, it makes managing packages on macOS easier. However, Apple’s new silicon is based on ARM64 architecture. This means that not every package or app can run natively (yet). Thankfully, Rosetta 2 helps bridge the gap by “translating software” so it can run on this new architecture.

First, let’s talk about the importance of updating PHP.

Keeping the version of PHP updated in your development environment is of paramount importance for several compelling reasons:

  • Security: Outdated PHP versions are more vulnerable to security threats.
  • Performance: Newer PHP versions often come with performance improvements and optimizations.
  • Compatibility: As PHP evolves, new features and functions are introduced. By using the latest version, you can benefit from these improvements.
  • Bug Fixes: PHP updates include bug fixes that address issues present in earlier releases.
  • Long-Term Support: PHP versions are typically supported for several years. Staying up to date ensures access to supported releases.
  • Community Support: A community of developers and experts actively contributes to the PHP ecosystem.
  • Future-Proofing: By adopting the latest PHP versions, you prepare your codebase for future technologies and trends.

Ready to change PHP versions with Homebrew? Keep reading to see how I did it.

Using Homebrew

While setting up my new MacBook Pro, I ran into this error while trying to install PHP 7.4:

brew install [email protected]
Error: Cannot install under Rosetta 2 in ARM default prefix (/opt/homebrew)!
To rerun under ARM use: arch -arm64 brew install ...

Turns out, PHP 7.4 wanted nothing to do with Rosetta, so I needed to specify the architecture first:

arch -arm64 brew install [email protected]

Then I updated the PATH in my .zshconfig , so the terminal can find PHP 7.4:

echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/[email protected]/sbin:$PATH"' >> ~/.zshrc

I restarted my terminal and verified the PHP version:

php -v

Now I see PHP 7.4:

PHP 7.4.27 (cli) (built: Dec 16 2021 18:02:37) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.27, Copyright (c), by Zend Technologies

Switch Between PHP Versions

Switching between versions of PHP is the same process: install (you only have to install once) and then update the PATH. Here are some examples:

PHP 8.1

brew install php

Update the path:

echo 'export PATH="/opt/homebrew/opt/php/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php/sbin:$PATH"' >> ~/.zshrc

PHP 8.0

brew install [email protected]

Update the path:

echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/[email protected]/sbin:$PATH"' >> ~/.zshrc

PHP 7.4

arch -arm64 brew install [email protected]

Update the path:

echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/[email protected]/sbin:$PATH"' >> ~/.zshrc

Because PHP 7.3 and below are no longer supported, Homebrew won’t let you install:

arch -arm64 brew install [email protected]

Will throw an error:

Error: [email protected] has been disabled because it is a versioned formula!

Gotchas

You may need to unlink/link PHP version using Homebrew if you see an error like:

Error: Could not symlink include/php/TSRM/TSRM.h
Target /usr/local/include/php/TSRM/TSRM.h is a symlink belonging to php. You can unlink it: brew unlink php

You could solve for this by trying:

brew unlink php
brew link [email protected]

I hope you found this quick tip helpful, and if you have anything to add, please drop a comment below. WebDevStudios offers a monthly retainer for ongoing website development and support services that include keeping your PHP updated and your website performant. Contact us when you need a team of experts behind your website.

Comments

1 thought on “Quick Tip: Change PHP Versions with Homebrew on Apple Silicon Powered Macs

  1. I’ve found a tool called ServBay.dev can provide much easier way for PHP developers, especially the beginners. It includes all versions of PHP, MariaDB, and PostgreSQL, as well as Redis and Memcached. Users can change multiple versions of PHP immediately.
    Trust me, it’s made my PHP dev life a whole lot smoother. Might be worth a shot for you to check it out!

Have a comment?

Your email address will not be published. Required fields are marked *

accessibilityadminaggregationanchorarrow-rightattach-iconbackupsblogbookmarksbuddypresscachingcalendarcaret-downcartunifiedcouponcrediblecredit-cardcustommigrationdesigndevecomfriendsgallerygoodgroupsgrowthhostingideasinternationalizationiphoneloyaltymailmaphealthmessagingArtboard 1migrationsmultiple-sourcesmultisitenewsnotificationsperformancephonepluginprofilesresearcharrowscalablescrapingsecuresecureseosharearrowarrowsourcestreamsupporttwitchunifiedupdatesvaultwebsitewordpress