Using Composer to Manage Project 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/etc. with Composer
Now, we can start installing Drupal projects, like modules and themes.
The drupal.org composer endpoint allows us to install modules and themes directly from drupal.org using Composer. You can read the official documentation for all of the details, but I'll go over the basics with you now.
To install a module, you'll type
composer require drupal/<module>. For example, to install Pathauto, you'd type
composer require drupal/pathauto in the command line. You can also specify a version, and there are a number of version patterns that you can use. We'll talk about all of those as we progress through this series.
Now, even though you can technically install a package without specifying a version, you really should so that you get the minimum stable version desired. I'll go ahead and install Pathauto using the command above
composer require drupal/pathauto
You'll notice a couple of things happened with that command.
- Pathauto was installed, along with it's dependencies "Ctools" and "Token". 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 Pathauto instead of the "recommended" version: 1.0-beta1.
And if we take a look in our composer.json file, we'll see that Pathauto was added like this:
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'll notice the quotes around the package and version. While not required, they do ensure compatibility across all terminals and shells, some of which will have issues when the quotes are not used.
I'll install the latest stable version of Pathauto with the following command:
composer require "drupal/pathauto:^1.0"
When I press enter, we'll see in the command line output that the beta1 version was installed. We can also take a look at the composer.json file, where we'll see that the Pathauto line was updated to match the new requirement.
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 Pathauto, we'd simply type:
composer remove drupal/pathauto
Then, when I press enter, we can take a look at the composer.json file, where we'll see that the pathauto line was removed for us. And, you'll notice in the terminal that the Pathauto package was deleted, as well as the dependencies since they are no longer required by any package in the composer.json file.