Boosting Productivity in Drupal with Composer 2

19 Apr 2024

Composer 2 is an effective solution for increasing productivity when working with Drupal. Everyone knows the situation when simple operations take a lot of time. Using Composer in Drupal makes developers' lives easier.
To speed up workflows, web developers often use the same code many times in many projects. This code can be written not only by other people; that's not the point. Regarding programming, rewriting code is a concern for many developers. Composer allows you to get rid of this problem. You can save parts of PHP code and use them for other projects.

Composer 2: What is it?

You (and other developers) can form the necessary parts of the code into libraries and use these libraries in certain projects. Some libraries are linked to other libraries, and they have a logical connection. So, one project can use different code libraries attached. Sometimes, libraries are also called packages, but the essence remains.
For the convenience of developers, a unique website was created - Therefore, when you need a particular functional element and ask Composer to load it, it automatically searches for this library at the site, not only at this site (you can specify a list of repositories). One library is often linked to others, and you need to download all the libraries to regularly operate a site element. The Composer does this automatically. Composer 2 loads all logically interconnected libraries needed for the stable operation of the component, protocol, etc. 
However, you can always configure Composer to search for repositories in other cloud storage sites - GitHub, etc. The main thing is that the Composer will independently search for the necessary libraries, load them into the system, and check the system requirements. In addition, internal conflicts between previously installed and new libraries are also checked. Composer has some simple tools that make a developer's life easier. You need to perform several steps to use the required library in a project, which we will discuss later in the article. 
Composer 2 is ideal for managing large amounts of code. By automating many processes, developers spend much less time. You can install Drupal core, themes, and modules using Composer.

Why Composer 2, not Composer?

There are several reasons why you should opt for Composer 2, but the major one is its enhanced functionality. Composer 2 has been greatly improved over the first version. This allowed us to get rid of many problems, conflicts, and improve productivity and speed. Let's take a closer look at the main improvements:
Offline mode
Composer 2 can work offline. This is very convenient if you need help with the Internet and need to get work done. Every developer knows what a "deadline" is.
Optimizations Drupal by Composer 2
The new version of Composer has significantly improved interaction with the processor and memory, which improves performance. Many minor bugs have also been fixed, improving performance and saving a lot of developer time.
Parallel loading
Also, the parallel download feature is available from the very beginning, and no need to install add-ons. This also improved performance.
Smart installation
The "vendor" folder is modified only after all required libraries have been installed.
There are many more large and small changes; we have only described the main ones. To talk about all the innovations and updates in detail, a separate article is needed.

Getting Started with Composer 2

First, we need to understand - Composer works with libraries using just 2 files: composer.json and composer.lock. 

  • The first of them is “composer.json”, which contains information about installed libraries. So, you install libraries for use in a specific project, and they need to be specified in the “composer.json” file. This file should be in the topmost directory of your project, this is important.  In Composer 2, you can make any repository canonical, which is a big plus.
  • “Composer.lock” keeps track of library versions - this is very important. This file stores information about the current versions. So, “composer.json” contains a list of dependencies, and “composer.lock” contains a list of installed versions. If a new employee joins the development team, the same versions that other developers already have will be automatically downloaded to his computer. Without this, Composer might just download the newest versions but not the ones it needs.

This is a very simplified description, but it is completely correct and allows you to understand how Composer interacts with libraries and repositories. And, remember - “composer.lock” files are interoperable between 1 & 2 Composer versions.

Drupal with Composer 2 - first steps & commands

Composer looks for dependencies on its own. You must register repositories using the repositories key.
Next, you can download the Drupal core and libraries (packages). Drupal and Composer work great together.
To install Drupal with Composer, you need to enter the next command:

$ composer create-project drupal/recommended-project [project-name-dir]

It can be Drupal 8, 9 or 10 core.

To install the dependencies you need, use the command:

$​ php composer.phar install

The Composer will install the dependencies listed in the composer.json file. We have already said above - the versions must be listed in the composer.lock file.

To find libraries, enter the command:

$ composer require [vendor]/[packagename]

where "packagename" is the package you need.

If you want to download a module, the command will look similar:

$ composer require drupal/modulename

where "modulename" is the module you need.

To get the latest dependency updates, use the update command:

php composer.phar update - to update all dependencies.

php composer.phar update monolog/monolog - to update one dependency (specified as an argument).

composer update drupal/modulename - -with-all-dependencies

If you want to preview the update and see - what would happen without any real changes, you need to:

- -dry-run

Sometimes you may need to update a separate module manually:

composer update “drupal/*” - -with-all-dependencies
drush updatedb
drush cache:rebuild

Also, it will clear the cache.

8 tricks to work efficiently with Composer 2 in Drupal

A few tips with Composer Drupal will help you save time. Here they are:

1. ALWAYS (ALWAYS!) use dry-run before major changes. However, many novice developers use dry runs for any changes.
2. Speed and optimization - Composer 2 is no longer an issue, and the optimization plugins you previously installed are no longer needed. For example, “zaporylie” or “prestissimo” plugins. Some people update these plugins to versions that are compatible with Composer 2. 

It is actually easier to remove them using the command: 

composer remove zaporylie/composer-drupal-optimizations

3. Some packages have been abandoned and are no longer supported (doctrine/reflection; webmozart/path-until; swiftmailer; etc.), so you will see messages saying that these plugins are no longer supported. Just ignore. There's nothing you can do about it, you just have to wait.
4. Backward compatible with Composer 1 - yes, it works. But it doesn't work that well, so it's better to have the entire development team update Composer to version 2. This will help avoid conflicts and errors.
5. Sometimes, we find useful modules that do not work with Drupal 9. However, patches have already been released that correct the incompatibility and may make these modules work. What do we do? This is where the Lenient Composer endpoint comes in handy. The Lenient Composer endpoint is added before the standard endpoint. This can be done like this:

"repositories": {
     "lenient": {
         "type": "composer",
         "url": ""
     "0": {
         "type": "composer",
         "url": ""

After downloading the modules, you need to add changes to the composer.json file. THIS CAN ONLY BE DONE BY YOURSELF! And remember - this method does not work in Composer 1.

6. Error when executing the “composer require” and “composer update” commands. If you see that error when entering these commands, you must immediately update the “cweagans/composer-patches” package. Composer 2 only works with version 1.7.0 or later.

7. “Scaffold” plugin may throw an error after updating to Composer 2. It’s simple - update the plugin to version 8.9 or later. Older versions work only with Composer 1.

8. There are some packages that have security problems. During installation, you may not always be aware of these issues. You can include a security feature in your project to prevent such packages from being installed. This can be done by using two commands:

composer require - -dev roave/security-advisories:dev-master
composer require - -dev drupal-composer/drupal-security-advisories:8.x-dev

Wrapping up
Since its inception, Composer has been an efficient package management tool. The second version of Composer eliminated many problems and conflicts that we can see in the past. This made the work process even faster and simpler. Every modern developer should try this effective and vital tool. If you still need to do this, now is the time. Contact us to get help with Composer 2 because we are profi!