DEV Community

Cover image for Switching between PHP versions with Homebrew
David Carr
David Carr

Posted on • Originally published at daveismyname.blog

Switching between PHP versions with Homebrew

With Homebrew it's possible to have multiple versions of PHP installed at once, to switch which version is active unlink the current version and link the desired version.

Switch from 7.4 to 7.3

brew unlink php@7.4 && brew link --force --overwrite php@7.3
Enter fullscreen mode Exit fullscreen mode

Switch from 7.3 to 7.4

brew unlink php@7.3 && brew link --force --overwrite php@7.4
Enter fullscreen mode Exit fullscreen mode

Since you may want to do this frequently it's more convenient to create a function to do this.

Create a function to your bash/zshrc profile

switchphp() {
    brew unlink php && brew link --force --overwrite php@$1
}
Enter fullscreen mode Exit fullscreen mode

This will unlink PHP and relink to the specified version.

Usage:

switch to PHP 7.4

switchphp 7.4
Enter fullscreen mode Exit fullscreen mode

switch to PHP 7.3

switchphp 7.3
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
mike_hasarms profile image
Mike Healy

Thanks.
Does this effect the CLI PHP only? Meaning, would I still need to reconfigure php-fpm or my local web server config to use the different versions?

Collapse
 
dcblog profile image
David Carr

I have this in my bash profile:

export PATH=$HOME/bin:/usr/local/bin:$PATH

Which takes care of linking to the PHP version I believe so it should work for both. I tend to use PHP's built in web server locally so the command line version works fine.