Update Your Drupal Projects to Composer 2

By Kevin, November 9th, 2020

Composer 2.0 was recently released with much improved performance in fetching and updating dependencies. If you are running projects on Drupal 8 or 9, you are going to want to switch as soon as you can.

Before you do, there are some packages common to most Drupal projects that you will need to update first prior to upgrading from Composer 1 to Composer 2. Some packages that rely on the Composer plugin API may still be designated as 1.0 - which means Composer 2 will refuse to work if they exist in your project.

Here are some common packages that will need updating or removal:

  • composer/installers (must be at least version 1.9)
  • cweagans/composer-patches (must be at least version 1.7)
  • zaporylie/composer-drupal-optimizations (must be removed)
  • drupal/console (must be removed)

The composer-drupal-optimizations package is no longer needed with Composer 2. Drupal console isn't so relevant anymore and is not compatible with Composer 2. A majority of people use it just for code generation, which Drush 10 supports, so you can pivot over to Drush which is and has been more supported in the community.

At this point you can upgrade Composer to 2.0 and give it a try. You can do it with the following command:

composer self-update --2

If you run Composer from within a container, check if that container has an updated release or extend it with a Dockerfile and build with that command above. Your build process can use the official Composer image to try out:

build:
  image: composer:2.0.4
  stage: build
  script:
    - composer install -n --optimize-autoloader --ignore-platform-reqs

If you run into Composer errors, you can always go back to version 1 with:

composer self-update --1

I'm running multiple projects with Composer 2 now. It is an order of magnitude more performant. I have reduced build times in our CI services by up to 90 seconds in some cases. That time adds up, especially if your CI service limits the build minutes per month.