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
[email protected]
[email protected]
[email protected]
[email protected]

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

brew install [email protected]

(Replace 8.1 with the version you need.)


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

brew unlink php

or if you know a specific one:

brew unlink [email protected]

Now link the desired PHP version:

brew link [email protected] --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 [email protected] --force --overwrite"
alias php82="brew unlink php && brew link [email protected] --force --overwrite"
alias php74="brew unlink php && brew link [email protected] --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 [email protected] --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 *