Working with composer in Drupal

21 Jun 2019
Composer with Drupal

A composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. In Drupal, we can use it to set up the project. And with the help of some commands, we can add any contrib modules and themes. For any external library, we need to add some code in the composer.json file and after that, with some commands, it will add that library to your code base. Composer is a really a very powerful tool in terms of managing the project. When you are adding any module, it automatically takes cares of all the dependencies of that module. So, that reduces the pain of understanding the requirements of any module or theme.

Installing Composer

Drupal Composer

You need it to be installed on your local machine before executing any commands. To quickly install Composer in the current directory, run the following script in your terminal.

php -r "copy('', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Installer Options


You can install it to a specific directory by using --install-dir option and providing a target directory. Example:

php composer-setup.php --install-dir=bin


You can specify the filename (default: composer.phar) using --filename option. Example:

php composer-setup.php --filename=composer


You can install it to a specific release by using --version option and providing a target release. Example:

php composer-setup.php --version=1.0.0-alpha8

The installer will check a few PHP settings and then download composer.phar in your working directory. This file is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP which can be run on the command line, amongst other things.

Now run php composer.phar in order to run Composer.

You can install it to a specific directory by using --install-dir option and additionally (re)name it as well using --filename option.

php composer-setup.php --install-dir=bin --filename=composer

If you are allowed to install it globally, do so as there is no benefit of having one installation per user/project.

Composer to setup Drupal 8 Project

For settings up Drupal 8, all you need to do is to execute a command which installs Drupal in the current directory.

composer create-project drupal-composer/drupal-project:8.x-dev my_site_name_dir --no-interaction

With this, you will have Drupal 8 set up in your local machine.

Download Contrib Modules and their dependencies

For some Drupal module you will find that it needs some 3rd party library, and composer handles this kind of modules very well without letting you know much about all these requirements of the modules, which means you only need to execute one command to add the module to you codebase and it will take care of all the dependencies of the module.

composer require drupal/<modulename>

For example,

composer require drupal/token

This needs to be executed at the root of your Drupal install but not at the same level as the core directory. Composer will then automatically update your composer.json, adding the module to all the other requirements in the list, like this:

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

Composer will download the module and all the possible dependencies it may have.

Download specific version

If you need to download a specific version of a module, you can download that as well with composer.

composer require 'drupal/<modulename>:<version>'

For example:

composer require 'drupal/token:^1.5'
composer require 'drupal/simple_fb_connect:~3.0'
composer require 'drupal/ctools:3.0.0-alpha26'
composer require 'drupal/token:1.x-dev'

Download 3rd Party library

If you need to download 3rd party library directly, you can do that but to do that you need to make some changes in your composer.json file.

Suppose you need to download Mobile-Detect library which is available on github at:

Now, to download this library to you project, you need to add few lines under "repositories":

  "type": "package",
  "package": {
    "name": "serbanghita/Mobile_Detect",
    "version": "2.8",
    "type": "drupal-library",
    "source": {
      "url": "",
      "type": "git",
      "reference": "origin/master"

Here, "name": "serbanghita/Mobile_Detect" is used to download the library, once this is added in composer json, you can execute:

composer require 'serbanghita/Mobile_Detect'

While working with composer always remember to not do any manual changes in compoer.lock file, it will automatically gets updated. And whenever you do any changes in composer.json file, you need to make sure that composer.lock gets updated accordingly.