Switching PHP with Valet0:00
In an ideal world, all of our projects would always be running the latest version of php, but unfortunately, it's just not always the case. Thankfully, Valet manages one single instance of php for us and makes it very easy to change what version that's running, and allows us to isolate specific sites to run on a different version of php than your primary instance. Valet exposes two features that allow us to customize our php version, use and isolate. So let's get started with use, which is the simpler of the two. When you first install Valet, it's going to tell Homebrew to install the latest version of php. But if you need to have your entire Valet run on an earlier version of php, you can, with valet use. This also works if a new version of php comes out and you want to upgrade to that. So for example, valet use php@8.1 tells Valet, make sure
How Homebrew Links Versions0:43
version of php, you can, with valet use. This also works if a new version of php comes out and you want to upgrade to that. So for example, valet use php@8.1 tells Valet, make sure Homebrew has this particular version of php installed, and then set it to be the primary linked version of php. While this installs, let's take a quick break to talk about how Homebrew manages versioning dependencies. With Homebrew, you can have more than one version of any dependency installed. At any given moment, only one of those versions will be linked, which means it's the version that's accessible from the main path for that particular dependency. So for example, if php 8.2 is linked, and I run just php in the command line, I'm going to get php 8.2. But I may also have 8.1 installed as well, or 8.0. And I can access those, it's just a lot less convenient than it is to just type php. So we have a
line, I'm going to get php 8.2. But I may also have 8.1 installed as well, or 8.0. And I can access those, it's just a lot less convenient than it is to just type php. So we have a perfect example here, because we just updated to use php 8.1, which might make you think that 8.2 is not still installed, but it still is. You say brew list | grep php, you're going to see multiple versions of php installed here. But if I say which php, we're only going to see the current one, which is 8.1. So how do we find the rest of them, we look in cellar, and that's either going to be /opt/homebrew/cellar or /usr/local/cellar depending on what type of Homebrew you have. And here you can see php, php 8.0, php 8.1, php 8.3. Now php is the one that's most confusing here. Because sometimes when we install without naming a specific version, it just grabs whatever the latest is. So if you ever see something that's
is the one that's most confusing here. Because sometimes when we install without naming a specific version, it just grabs whatever the latest is. So if you ever see something that's just php here, it's almost always going to be whatever is the latest when you ran brew install php. In this case, it was php 8.2. We have all these different versions accessible. The problem is they're not accessible easily. If I were to go php 8.0, we still get the specific version of 8.0 in there. And then under there, we have to go to the bin, and then actually find php from there. So like I said, it's not super easy to find when you're comparing it to just typing php. So the current linked version of your dependency is by far the easiest to interact with, but they are still there. Okay, so back to valet use. When we ran valet use php 8.1, what we really did was a global php version switch, which
What Valet Use Does2:49
the easiest to interact with, but they are still there. Okay, so back to valet use. When we ran valet use php at 8.1, what we really did was a global php version switch, which is effectively saying brew install php at 8.1, and then also brew link php at 8.1. So now if you ever run php, it would be 8.1. And that way, valet can continue just calling php as it is, assuming that whatever version resolves there is the one that you want. And that doesn't make this change just for valet, it makes it for any other usage of php across your entire system. So basically, valet use is the easiest way for you to change your globally installed and linked version of php for homebrew. But let's say you want to have a modern version of php as your daily runner, which I'd recommend by the way, but you have one site or several other sites that are running older versions of php. Basically, let's say
Isolating PHP per Site3:33
a modern version of php as your daily runner, which I'd recommend by the way, but you have one site or several other sites that are running older versions of php. Basically, let's say you're in any situation where you need to run more than one version of php at the same time. Well, you will love valet isolate. This feature allows you to define the version each of your individual sites should use. And then valet will run all requests through the web to those sites through those specific versions. So one thing I did here was I created a whole bunch of new sites, php 8.0, php 8.1, and php 8.2. And if you take a look at them, you know, they're named after specific versions of php. Well, we're already running 8.1 globally, so I don't really need to worry about that one. Although I might want to isolate it just in case. But for now, let's go isolate php 8.2. Because if we were to go to php 8.2.test,
so I don't really need to worry about that one. Although I might want to isolate it just in case. But for now, let's go isolate php 8.2. Because if we were to go to php 8.2.test, and it tells us what version of php we're running, well, it's not actually 8.2. So that's a problem. So what we can do is we can go to php 8.2, we can say valet isolate, and then the version, so php@8.2. And this is basically telling our system every single time you resolve php-8-2.test, run it through the php FPM, or the version of php that's constantly running for php 8.2, not the one for 8.0, not the one for 8.1, not whatever our global one is that's running right now, specifically this one. And let's go back to our site, refresh. And now we're on php 8.2. Now let's look at one of the other ones we haven't isolated yet php 8.1. And look, that's at 8.1. Not because we're isolated it because
our site, refresh. And now we're on php 8.2. Now let's look at one of the other ones we haven't isolated yet php 8.1. And look, that's at 8.1. Not because we're isolated it because that's what's just global right now. What about php 8.0? Still running on 8.1 because that's what's globally installed. Now one thing we could do is go to the php 8.1 site. We can also valet isolate php 8.1. That doesn't matter that much right now. But if we were to later change our global version, that would make sure that this one stayed at php 8.1. We go there. This one's still php 8.1. And php 8.1 is also php 8.1. So one of the things that people almost always ask when they see this feature is what about the command line? Because if I'm this php 8.1 directory that's been isolated to valet php 8.1, and I run php -v, well, that's fine, because that's what we have globally. What about the one
CLI with Valet Proxy5:48
Because if I'm this php 8.1 directory that's been isolated to valet php 8.1, and I run php -v, well, that's fine, because that's what we have globally. What about the one that we've isolated to php 8.2? php -v? Still php 8.1 because this is homebrew and homebrew doesn't know anything about what we've done with valet here. However, if you instead run valet php, that gives you the opportunity to proxy those calls through to a specifically isolated version here. So if you run valet php -v, you're going to get php 8.2. You can do the same thing with composer. If you run valet composer install, it's going to run a version of composer through that 8.2 isolated version of php. And one cool tool that you can use with valet isolate is to go to the directory for one of your projects and add a valet.rc file. In this file, you can write php equals and then the
cool tool that you can use with valet isolate is to go to the directory for one of your projects and add a valet.rc file. In this file, you can write php equals and then the version you want it to be. So we'll say php equals 8.0. Now in that directory, if I just write valet isolate, it's going to automatically read that I'm looking for 8.0. And it's going to isolate that version without me even having to remember what specific version I wanted. And one nice thing about this file is that you can commit it directly to version control. And so all the folks in your team have access to the same thing. As you can probably imagine, it can get a bit complex to keep track of all the sites you've isolated to all the various versions of php. So you can run valet isolated, and it'll give you a list of all the sites you've isolated and the specific versions of php that each of them is isolated to.
Best Practices Recap7:06
versions of php. So you can run valet isolated, and it'll give you a list of all the sites you've isolated and the specific versions of php that each of them is isolated to. I want to name one more interesting aspect of how valet use and isolate work. Quite a few people have told me that they have so many older sites that are running on php 7.4 or php 8, that they have to set that as their primary version of php through valet use. However, valet itself needs a more modern version of php. What I've made possible is that if you're running an older version of php, as long as you have a homebrew installed version of a more modern version, valet will reach out to that more modern version anytime it needs something that your currently installed version of php can't do. So to sum it up, if you want to change the global version of php that you have installed through homebrew,
it needs something that your currently installed version of php can't do. So to sum it up, if you want to change the global version of php that you have installed through homebrew, valet use makes that super easy for you. But my biggest recommendation is to keep a high modern version of php running at any time through valet use, and then to use valet isolate to set just specific sites to run at older versions.
