Skip to main content


Using Composer to Manage Projects and if required their dependencies

In this section, we're going to dive into how to use Composer to manage project dependencies. Specifically, we'll cover the following:

    Installing and Uninstalling packages
    Forcing Composer to install the latest dev
    Updating and downgrading projects with Composer
    Skipping versions and Specifying ranges
    Enabling modules with Drush
    How to decide which version pattern to use when requiring projects

Install, uninstall and update modules / themes with composer

Installing Drupal projects, like modules and themes best practice is using drush.  To install a module, you'll type

composer require drupal/<module>

For example, to install Redirect, you will type the command noted above replacing <module> with composer require drupal/redirect in the command line.  You can also specify a version and there are a number of version patterns that you can use.

Installing Projects

While you can technically install a package without specifying a version (as noted above), best practice is that you get the minimum stable version desired. By installing Redirect using the command above

composer require drupal/redirect

You'll notice a couple of things happened with that command.

Redirect was installed, along with it's dependencies (if the module has any dependancies).  This is one great thing about Composer.  It is a dependency manager, so it installs all of the dependencies of the package you requested.  But, it goes even further and installs the dependencies of dependencies - recursively - until you have everything you need to use the package you originally requested.

It also installed the dev version of Redirect instead of the "recommended" version: 1.0-beta1.

And if we take a look in our composer.json file, we'll see that Redirect was added like this:

"drupal/redirect": "^1.8",

In this case, to get the "recommended" release, we have to specify a version by typing something like this:

composer require "<vendor>/<package>:<version>"

You will notice the quotes around the package and version.  To get an idea what to enter go to the module page on the Drupal site.  For example, Redirect can be found at https://www.drupal.org/project/redirect.  Scroll towards the bottom and click on 8.x-1.8.  The current version.  This action will take you across to the Redirect releases page.  Your work as to what to type is done through the following line:

Install with Composer: $ composer require 'drupal/redirect:^1.8'

As now you will be using Drupal 10, check the module is set to your Drupal version by cross checking the requires line:

Requires Drupal: ^9 || ^10

 

While not required, they do ensure compatibility across all terminals and shells, some of which will have issues when the quotes are not used.  Install the latest stable version of Redirect with the following command:

composer require "drupal/redirect:^1.8"

Action this command by pressing enter / return.  You will note in the command line output that version 1.6 was installed.  More importantly, cross check by taking a look at the composer.json file.  Where you will see that the Redirect line was updated to match the new requirement.

Uninstalling Projects

Uninstalling a package via the command line is almost the same as installing, but we don't need to specify a version.  So to uninstall Redirect, simply type:

composer remove drupal/redirect

Press enter / return.  Again look at the composer.json file and observe that the redirect line was removed.  Not only has the module been installed, but also any dependencies that aren't being used by any other module or package in the composer.json file.

Related articles