Switching between PHP versions

This is going to be heavily technical. If you’re generally more interested in other ramblings I post about, you can skip this post. It’s okay, I won’t be disappointed.

Background

I recently learned how to run multiple versions of PHP on my local machine and switch between them using Homebrew. It’s pretty straightforward, actually.

brew install [email protected]

…tells brew to install the latest PHP 7.4 version. This gets installed into /usr/local/opt/[email protected]/7.4.{latest-release}. Installing PHP at a particular revision does not necessarily mean that you are using that revision. In order to use PHP 7.4, I need to do this:

brew link php7.4

That command creates symlinks between the Homebrew-managed path to that PHP version and /usr/local/bin/php. If I then want to switch to a different version of PHP that I’ve installed previously, I’d first need to

brew unlink php7.4

and then

brew link [email protected]

…or whatever version I wanted to switch to.

This is nice that I can switch between versions — in fact, it’s pretty powerful — but the brew unlink / brew link dance can be cumbersome. There must be a better way!

Informercial gif of a woman dropping spice jars out of her cabinet.

A solution

I’ve solved this dance using some clever bash commands I stole borrowed from a fellow Pantheor, Brian Weaver, and a tool that a former colleage at Human Made forked and improved upon called Taskfile. I’ll let you head over there and read about what it does and how you use it. The first step is installing it so you can run t from the commandline.

Taskfile lets me shortcut bash functions to the t command that are contextual to the folder I might be looking at, but you can also add to and edit the global Taskfile with functions that work everywhere by just typing t edit. In my case, I’ve created a series of functions that let me switch to whatever version of PHP I want, e.g.

t php8

With that single command I can:

  • Identify which version of PHP I have installed
  • Identify the newest version of PHP I have installed
  • Unlink from the current PHP version
  • Link to the new PHP version
  • Only type 6 characters! (Let’s face it, that’s the real win here, amirite?)

The code looks like this:

https://gist.github.com/jazzsequence/7fb9f6c6740317d9b7fef7cd33a3a1c0

When I want to add a new PHP version, once I’ve installed it with brew, I can just run t edit and add a new function:

php82() {
    unlink_php
    echo "Linking PHP 8.2"
    brew link [email protected]
}

It’s important to check against the latest version of PHP because Homebrew maps this to just php, as opposed to other version releases, which are mapped to php@{version-number}, e.g. [email protected], so we need to check what the latest version of PHP is, according to brew. Interestingly enough, this supports development versions, too, so installing (as of this writing) PHP 8.2 will install as [email protected] rather than being mapped to php because it’s the “newest” version.

This has taken the headache out of switching back and forth between multiple PHP versions which is a thing I’ve been doing more of recently as I need to test different host environments. I really like Taskfile a lot and use it for all sorts of things and this is yet another example of how useful it can be. I highly recommend it!


Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

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