How to install and remove Drupal 8 modules at the command line with Composer

I find Drupal 8 module management to be confusing, but one thing I’ve learned is that you can install and remove Drupal 8 modules with Composer at the command line.

Adding a Drupal 8 module with Composer

The short story is that to add a new module — such as the reCAPTCHA anti-spam module — you type this command at the command line (in the root directory of your Drupal 8 website) to install it:

composer require drupal/recaptcha

In addition to installing the module, this command adds a line like this to the require section of composer.json file:

"require": {
    ...
    "drupal/recaptcha": "^2.3"
},

Removing a Drupal 8 module with Composer

Later, if you want to remove a Drupal 8 module, you use this Composer command:

composer remove drupal/recaptcha

This command removes that module, checks all the dependencies to see if any of them can be deleted, and also updates the composer.json file. In Drupal 8 the admin/modules URI really only works as a “view,” where you can see which modules are installed, and after issuing that command that URI shows that the reCAPTCHA module is removed, so that’s cool.

More information

For more information on adding and removing Drupal 8 modules with Composer, see these URLs: