Composer

This page is out of date and needs reviewing.

Composer is used to install and manage Drupal 8+ modules and package dependencies.

A very helpful article on using Composer can be found here: https://www.lullabot.com/articles/drupal-8-composer-best-practices

Installation

Lando

Composer is already installed in Lando's appserver container. To use it you just need to prefix the composer command with lando

lando composer [command]

For example if you want to run composer install you can use:

lando composer install

Linux

You will need PHP installed on your machine.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer

Mac

You will need homebrew and PHP installed on your machine.

Install Composer

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

Installing Drupal modules

In Drupal + we manage all module installations with Composer. In the gitroot directory of a project you will see a composer.json file which contains all the specifications of the project. To install a new module you would issue a command with the following format:

composer require drupal/coffee:~2.0

In the above command composer require tells composer to add a new entry to the require section of composer.json and to download the package specified by drupal/coffee at the version specified by ~2.0. For details of the conventions around version constraints see the composer documentation.

Comprehensive documentation for Composer is available and it is well worth getting familiar with.

Updating Drupal modules

To update a specific Drupal module using composer you would issue a command like:

composer update drupal/coffee

This will update the coffee module as long as there is an available version the fits within the bounds of your version specification. So if you had required coffee at ~2.0 and you were currently on version 2.1, it would only update the module if version 2.2 or higher were available. More details on version constraints can be found in the Composer documentation.

You can also issue a wider composer update without specifying a module and this will update all modules and Drupal core to latest versions. This is generally not recommended and instead modules should be updated individually.

Updating Drupal core

Drupal core is updated using composer with the following command:

composer update drupal/core --with-dependencies

Sometimes dependencies can prevent Drupal core from updating correctly. In particular this can happen when a new minor version of Drupal core (8.4.0) depends upon a new major version of symfony (3.x.x). Resolving this can be difficult so its best to ask for help from others in the team who have resolved this before.

Removing a module

Before removing a Drupal module ensure you have uninstalled it first, this will mean uninstalling it from the production site before removing it from the codebase.

To uninstall a module use:

drush pmu module_name

To remove a module use:

composer remove drupal/module_name

Applying patches

Composer should be used to manage all patches applied to Drupal modules and core. See this file for how to manage them: https://github.com/cweagans/composer-patches/blob/master/README.md

Last updated: