One of the challenges with building solutions on WordPress is working with the various environments each host provides.

That is, some are still running very old versions of PHP; others are running newer versions, but even then there are variations among them.

Case in point: I’ve been running PHP 7.2 on my local machine for development for some time; however, I’m currently on a project where a few of the dependencies on the continuous integration tool offers support only for PHP 7.1.

And because I like my development environment to mirror the staging and production environments, I think it’s important to make sure there’s parity.

If you’re using a package manager, it’s pretty easy to get going with multiple of versions of PHP with Homebrew, but you may run into a couple of snags as it relates to running it alongside Valet.

Multiple Versions of PHP with Homebrew

First, you want to make sure that you have PHP 7.1 installed. To do this, you simply run the following command in your terminal. This will install the package but nothing more. You’ll still be running whatever version of PHP you’re currently running.

$ brew install php@7.1

After that, you’ll need to tell Homebrew to remove the symbolic link that it’s created to the current version of PHP you’re running and then link it to the package that you’ve just downloaded.

To do that, enter the following command:

$ brew unlink php && brew link --force php@7.1

Remember all that talk about shells? You’ll need to make sure that your shell – in this case, Bash – is aware of the version of PHP you want to use.

export PATH="/usr/local/opt/php@7.1/bin:$PATH"

export PATH="/usr/local/opt/php@7.1/sbin:$PATH"

Next, list out all of the services that brew is running. You can do this by entering the following command:

$ brew services list

Once you do this, you’re going to see a list of the services that are running as well as the account under which some of them are running.

Multiple Versions of PHP with Homebrew: brew services list


If some are running as root then you’re going to need to start and stop them using sudo; otherwise, you can stop and start them using your standard commands.

$ sudo brew services stop php

$ sudo brew services stop php@7.1

$ sudo brew services start php@7.1
After that, you’re likely going to want to restart Valet as this will make sure that PHP, Nginx, and Dnsmasq are all restarted.
$ valet restart
This should complete everything you need to do to swap versions of PHP.

Verify Your Work

To verify that your web server is, in fact, using the latest version of Nginx, I recommend creating a single directory (I call mine beta), linking it using Valet, and then dropping an index.php file into the directory that includes the phpinfo(); function call.
This way, you can get all sorts of information about your environment.
Multiple Versions of PHP with Homebrew
From here, you should see some variation of PHP 7.1 (or whatever it is that you installed). And if you do, then you’re good to go; otherwise, you may need to see about uninstalling and reinstalling Valet again.
But that’s a bit of an extreme step, and everything you see above should take care of it.