Switching between PHP versions with Laravel Valet

Laravel Valet can easily switch between PHP versions, but sometimes it can get stuck. Let's fix that!

Since PHP8 was released we've started upgrading all our projects to PHP 8, the things we can do with attributes, union types and other new features made us desire to do this upgrade as quickly as possible.

But sometimes you've got to work on an older project. When you want to switch between PHP versions in your terminal and Laravel Valet it is only one terminal command away:

valet use php@7.4 --force

Want to switch back to PHP 8 then use php@8.0. Or just php since it's the most recent released PHP version at this moment. You can switch to any PHP version installed with Homebrew on your system:

valet use php@8.0 --force
valet use php@7.4 --force
valet use php@7.3 --force
valet use php@7.2 --force
valet use php@7.1 --force
valet use php@7.0 --force
valet use php@5.6 --force

You'll probably need the shivammathur/homebrew-php tap to install some of these older versions with Homebrew.

Fixing a stuck Valet PHP version

Sometimes, the valet use command only switches the PHP's terminal version, but Valet's PHP version keeps stuck. It can be that the valet socket is not configured properly, removing it normally solves the problem:

rm ~/.config/valet/valet.sock
valet restart