Valet 3.0: Multiple/Parallel PHP Version Support

For my local PHP Development needs I use Laravel Valet. It’s easy to set up, provides HTTPS, and just works. The only downside of using it, is the fact that the selected PHP version is system-wide: switching PHP versions — using valet use php@7.4 for example — affects all sites. With the 3.0 release of Valet this is no longer the case, as it offers per-site PHP version isolation.

To isolate a site, use new isolate command:

cd path/to/app
 
# Isolate the current project
valet isolate php@7.4

# Isolate a site by name
valet isolate php@8.0 --site=laravel9x

If a certain PHP version is missing, Valet will install it automagically (using Homebrew)

⬆️ If you’re already running Valet, you can’t update to 3.x by invoking good ’ole composer global update. Instead, you must re-require Valet again, with the required version set to ^3.0

composer global require "laravel/valet:^3.0"

~

Note that when isolating sites using valet isolate, it only affects the PHP version that’s used by Nginx. On the CLI your php will always use the one you’re globally using. For dependencies, you could lock down your PHP version via composer.json, but that won’t prevent you from possibly incompatible PHP syntax.

To cater for both issue, version 3.1 (which got released just few days after 3.0) comes with three new commands that allow you to run PHP and Composer commands using the isolated site’s PHP version on the CLI.

  1. valet php ... will proxy PHP Commands with isolated PHP version
  2. valet composer ... will proxy Composer Commands with isolated PHP version
  3. valet which-php outputs the PHP executable path for a site. For isolated site it would output the isolated PHP executable path. But non-isolated site will just output the linked default PHP path. The other two commands are dependent on this one to find the PHP executable.

To not have to type valet php every time, this tip by Jacob Delcroix is pure gold: make php an alias for valet php

~

If you’re rocking the aforementioned PHP Monitor, you’ll be glad to read that version 5.2 has isolate support built-in.

To update PHPMon, do so using Homebrew:

brew upgrade phpmon

~

🔥 Like what you see? Want to stay in the loop? Here's how:

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.