TL;DR: If you’re working on a variety of projects each of which requires different versions of PHP, Composer, and/or NPM you may need to change the version of all or any permutation of any of these utilities.

This article outlines what steps need to be taken to downgrade Composer, PHP, or NPM when working on any given project.

Downgrade Composer, PHP, and NPM

Downgrading Composer 2 to Composer 1

For the last couple of months, I’ve been using Composer 2 with no problem; however, I’ve just had the need to downgrade Composer for a current project.

To do this, issue the following command in the terminal:

$ sudo composer self-update --1

Usually, this will work. If it’s been a little while since you’ve done anything like this then you may need to update your keys.

Verify Keys

The terminal commands for updating this are easy enough in terms of guiding you how to do it but having the following URL will be handy.

First, grab this URL: https://composer.github.io/pubkeys.html.

Then issue the following command:

$ composer self-update --update-keys

From there, the terminal will prompt you for which key you need to enter. Once done, you should be able to complete the downgrade process.

Downgrade PHP

I use Homebrew to manage the various installations of PHP that I have. I’ve been working on a project that was using PHP8 but have needed to downgrade it.

Assuming you already have the existing package for the version of PHP you want to use, you can use one of the previous installed versions.

For example, I have PHP 7.3.26 installed in /usr/local/Cellar/php@7.3 on my local machine. To see which versions you have installed, I do something like this:

$ ls /usr/local/Cellar/php*

And the outline will return the directories of the versions of PHP I have installed. At the time of this writing, I have:

  • /usr/local/Cellar/php which is 8.0.3
  • /usr/local/Cellar/php@7.3 which is 7.3.27

To use the older version, I first need to unlink the current version of PHP:

$ brew unlink php

And then create a link to the previous version:

$ brew link php@7.3 --force --overwrite

And it’s important to use the directory name (such as php@7.3 that terminal returns above).

To verify that this works, you should be able to run $ php -v and see the version that’s currently being run.

Downgrade NPM

Similar to the previous two items above, I’ve needed to downgrade a package which came as information when running npm i in the terminal.

Assuming the warning, error, or notice shows the version that you need then use that as a guide for what version to downgrade.

First, identify the current version by running:

$ npm -v

Then, using the information from the message in the terminal, run the following command:

$ npm install -g npm@6.13.4

One done, double-check the version once more using the same command as above and it should match with what you just installed.

Not All At Once

Obviously, you may not need to change all of these at the same time but one at a time. And that’s not unusual. This post is the result of me working on something from front-end to back-end that required I update the whole environment.

Ultimately, upgrade or downgrade only what you need. One doesn’t necessitate the need to update the others.