Technology BREAKING FEATURED

How to Switch PHP Versions on macOS Using Homebrew (PHP 8.1 to PHP 7.4)

Need to switch between different PHP versions on macOS? This step-by-step guide explains how to safely switch from PHP 8.1 to PHP 7.4 using Homebrew, update your PATH, and verify the active PHP version.

2 min read 9 views

As a developer, you may often need to work on multiple projects that require different PHP versions. On macOS, Homebrew makes it easy to install and switch between PHP versions without breaking your system setup.

This guide walks you through switching from PHP 8.1 to PHP 7.4 using Homebrew.


Step 1: Unlink the Current PHP Version

Before activating another PHP version, unlink the currently active one. This prevents conflicts between binaries.

brew unlink php@8.1


If PHP 8.1 is already unlinked, Homebrew will notify you.


Step 2: Link PHP 7.4

Next, link PHP 7.4 and allow Homebrew to overwrite any existing symlinks.

brew link --overwrite php@7.4


This command tells Homebrew to make PHP 7.4 the active PHP version on your system.


Step 3: Update PATH Environment Variables

To ensure your system uses PHP 7.4 binaries, update your shell’s PATH configuration.

For Zsh users (default shell on modern macOS)

Run the following commands:


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

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


Then reload the configuration:

source ~/.zshrc


For Bash users

Add the same PATH entries to either ~/.bash_profile or ~/.bashrc:

export PATH="/usr/local/opt/php@7.4/bin:$PATH"
export PATH="/usr/local/opt/php@7.4/sbin:$PATH"


Step 4: Verify the Active PHP Version

Open a new Terminal window and check the PHP version:

php -v


You should now see PHP 7.4.x as the active version.


💡 If the version does not update immediately, close the terminal completely and open it again.


Conclusion

Switching PHP versions on macOS is simple and reliable with Homebrew. By unlinking the current version, linking the required one, and updating your PATH, you can seamlessly move between PHP versions based on your project requirements.

This approach is especially useful when maintaining legacy PHP applications while also working on newer projects




Share this article: