Switching PHP Versions with Homebrew

Switching PHP Versions with Homebrew

1. First, see which PHP versions you have installed

Run:

brew list | grep php

Example output:

php
php@7.4
php@8.0
php@8.1
php@8.2

If you don’t have the version you want yet, install it:

brew install php@8.1

(Replace 8.1 with the version you need.)


2. Unlink the current PHP version

Before switching, you have to “unlink” the currently active PHP:

brew unlink php

or if you know a specific one:

brew unlink php@8.2

3. Link the PHP version you want

Now link the desired PHP version:

brew link php@8.1 --force --overwrite
  • --force and --overwrite are important so it properly updates the /usr/local/bin/php symlink.

4. Check the active PHP version

Verify:

php -v

It should show the version you just linked (like PHP 8.1.x).


⚡ Bonus Tip: Quick Switching Script

If you switch a lot, you can make it super easy by adding aliases in your ~/.zshrc or ~/.bash_profile:

alias php81="brew unlink php && brew link php@8.1 --force --overwrite"
alias php82="brew unlink php && brew link php@8.2 --force --overwrite"
alias php74="brew unlink php && brew link php@7.4 --force --overwrite"

After saving, reload your terminal:

source ~/.zshrc

Now just type php81, php82, etc. to instantly switch! 🚀


🛠 If “php” still points to wrong version…

Sometimes you need to fix your system path manually:

  • Make sure /usr/local/bin/ or /opt/homebrew/bin/ is first in your PATH.
  • Check by running: echo $PATH which php

If it points somewhere weird (like system PHP /usr/bin/php), then adjust your .zshrc:

export PATH="/usr/local/bin:/opt/homebrew/bin:$PATH"

and reload terminal.


🎯 Quick Summary:

TaskCommand Example
Unlink PHPbrew unlink php
Link PHP 8.1brew link php@8.1 --force --overwrite
Check versionphp -v

Would you like me to also show you how to configure different PHP versions per project (using something like Valet or Laravel Herd)? 🚀
It’s very cool if you’re working on multiple websites with different PHP requirements! 🎯


Leave a Reply

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