Why use Composer to manage Drupal dependencies?

One of the initial negatives when Drupal 8 launched was introducing Composer, PHP's dependency manager, and how it could affect non-technical users.

When I started doing Drupal, I downloaded the .tar.gz or .zip file of Drupal, extracted it, and placed it within my project.

I did the same for any additional modules I needed.

To update them, I needed to delete my files and repeat the process of downloading and replacing them.

Drush

Then, instead of doing it manually, I used Drush, the "Drupal shell", to download the files. This saved some time, but it still has down-sides.

What if you needed to install a module like Pathauto, which has dependencies you also need to download and install?

With Drush or downloading the files manually, you'd need to download the dependencies separately.

Composer

Composer is a dependency manager, which means it can handle these dependencies for us.

It looks at each project's composer.json file to find its dependencies and downloads them.

For example, to install Pathauto, you run composer require drupal/pathauto.

Within its output, you'll see this:

Package operations: 3 installs, 0 updates, 0 removals
  - Downloading drupal/token (1.12.0)
  - Downloading drupal/ctools (4.0.4)
  - Downloading drupal/pathauto (1.12.0)

As well as Pathauto, it's downloading its dependencies - ctools and pathauto.

Instead of downloading three modules, we can do it with one command.

In fact, we don't need to know what its dependencies are - Composer will do that.

Also, updating the modules is just another simple command - composer update.

While it may be intimidating to non-technical users, learning a few simple commands makes installing and updating modules much easier!

- Oliver

P.S. Do you want to learn about automated testing in Drupal? Take my free 10-day email course and get daily lessons straight to your inbox.

Was this interesting?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.