Site icon WebDevStudios

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:

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 php@7.4
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 php@7.4

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

echo 'export PATH="/opt/homebrew/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@7.4/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 php@8.0

Update the path:

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

PHP 7.4

arch -arm64 brew install php@7.4

Update the path:

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

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

arch -arm64 brew install php@7.3

Will throw an error:

Error: php@7.3 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 php@7.4

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.

Exit mobile version