Swapping Between Different Composer Versions

I had an issue I was trying to debug today and needed a way to swap between different versions of composer. Composer is one of the few things that I install globally so it was locked at a particular version on the machine I was using.

There has been some significant changes between version 1 and version 2 and it isn't quite safe to use a composer 2 package on a platform that has composer 1 installed.  As some projects I'm working on haven't been upgraded yet I needed a way to swap between versions whilst this work was being done.

To find out the version of composer you currently have installed use the --version flag.

composer --version

This should produce output like this.

$ composer --version
Composer version 1.10.22

To upgrade to composer version 2 I just needed to run the self-update command with the --2 flag passed in (which stipulates the version).

sudo composer self-update --2

Looking at the version now I see that I am now using version 2.

$ composer --version
Composer version 2.0.13

Then, if I wanted to downgrade to version 1 of composer I run the same command but with the --1 flag passed.

sudo composer self-update --1

Another call to the version flag shows that I am now back on version 1.

$ composer --version
Composer version 1.10.22

As it turns out, swapping between composer versions is quite easy, just be careful what directory you are in as composer will read your local composer.json file and if you are in a project with an incompatible file it will produce errors. You will need to upgrade your older composer 1 projects before you can fully adopt composer 2.

Comments

Another solution is not to use composer at all.

Permalink

Thank you for this considered opinion. What do you propose people should use as an alternative?

Name
Philip Norton
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
7 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.