Nowadays, it's pretty common for contributed Drupal modules to rely on third-party libraries. Some of these modules can only be installed using Composer. If you've already fetched Drupal core with Composer, or if a module demands Composer, you should use Composer to download all your modules and themes. If you mix and match Composer updates with other methods, you might run into some rather troublesome update problems.
 

Grab contributed modules and themes using Composer

So, do you want to download contributed Drupal modules or themes with Composer? Here's how:

  • Type composer require drupal/module_name
  • For instance: composer require drupal/token
  • Make sure you're at the root of your Drupal installation

Composer will then cleverly update your composer.json, adding the module to the list of other requirements, like so:

{
    "require": {
        "drupal/token": "^1.5"
    }
} 

Composer will download the module and any potential dependencies it might have.

Now, to enable the Drupal module, you have a couple of options:

When requiring modules, you can use either the project name or the specific module name within a project:

  • Composer will download the whole project containing a particular module.
  • For example, if you need the fe_block module from the features_extra project, you can choose either of the following options:
    • composer require drupal/features_extra
    • composer require drupal/fe_block
 

Choosing a version

If you'd like to specify the version of the module or theme you want to download, do it like this:

composer require 'drupal/module_name:version'

(Just replace version with the actual Composer version or version constraint.)

For instance:

composer require 'drupal/dxpr_builder:^1.1' 
composer require 'drupal/dxpr_builder:~1.1' 
composer require 'drupal/dxpr_builder:2.2.2-alpha2' 
composer require 'drupal/dxpr_builder:2.2.x-dev' 

To steer clear of any hiccups on different terminals or shells, enclose the version in quotes, just like in the examples above.


Heads up! On Windows, using (single) quotes might actually mess up the version specification, causing a failure to install with 'Could not parse version constraint .....': Invalid version string "....'".

Without the quotes, for example:

composer require drupal/dxpr_builder:^2.2

Or using double quotes instead of single quotes, like so:

composer require "drupal/dxpr_builder:^2.2"

It should work just fine. For more info, see Comment 'Composer problems: "could not parse version constraint"'.


In the examples above, the versions correspond as follows:

  • ^1.1: maps to the latest stable 10.x-1.x release of the module.
  • ~1.1: maps to the latest stable 10.x-3.x release of the module.
  • 2.2.2-alpha2: maps to version 2.2.2-alpha2
  • 2.2.x-dev: maps to 2.2.x-dev

For more on version constraints with ~ (tilde) and ^ (caret), have a look at Next Significant Release Operators.

It's a good idea to specify the version of the contributed module you want to download.


Using Composer search

Good news! Drupal.org's composer endpoints support the Composer search function. That means you can search for Drupal projects straight from the command line. To use Composer search, type:

composer search views
 

Using Composer browse

Drupal.org's composer endpoints for Drupal 7 through 10 also support the Composer browse function. This allows you to find additional information about Drupal projects directly from the command line. To use Composer browse, type:

composer browse drupal/token