Updating and Downgrading Projects with Composer

Now, let's talk about how to update and downgrade package (module) versions. We actually just did this in the last video, but I didn't want to gloss over it, and if you're trying to figure out how to manually update a package, you probably aren't using the dev version. So, this video will show how to update packages to a specific version.

In an ideal workflow, you'll really only do this when you need to downgrade a package because of a breaking change in the latest release, for example. I'll explain more about why that is in another video, but let's get to it.

I'll install Admin Toolbar since it has multiple releases that we can move through.

composer require drupal/admin_toolbar:^1.0

You'll notice that even though I used the version "1.0", Composer installed version 1.17. This is because the "^" (caret) means, basically, "greater than or equal to", and since 1.17 is the latest release that matches that requirement, that's the one we get. (We'll talk more about how the caret functions in a later video but that is how I recommend to install packages, so we'll use it for now.)

Let's pretend 1.16 introduced a change that broke something on our site and it's still present in 1.17. Because 1.17 is out, 1.15 (the last one that worked before the breaking change) no longer matches the version constraints in our composer.json file, so if we want it, we'll have to specify the version manually.

Updating and Downgrading Projects via the Command Line

To update or downgrade via the command line, you actually just type the exact same thing you would if you were installing a package for the first time, but specify the version exactly.

In this case, to install version 1.15 I would type:

composer require "drupal/admin_toolbar:1.15"

(Notice there's no "^" (caret))

Now, we can check the composer.json file, where we'll see the version has been specified as "1.15".

Now, as I mentioned before, it's actually not best practice to include a specific version number. The most common version pattern I use is the one that begins with the "^" (caret) symbol unless I need to downgrade because of a breaking change with the latest release. That's really the only time you should need to specify a version number. I'll explain that in more detail in the following videos.