Composer is the preferred dependency management solution for PHP. List your project’s dependencies in a composer.json file and, after issuing a few commands in the CLI, Composer will automatically download your project’s dependencies and setup autoloading for you. Composer is analogous to NPM in the node.js world, or Bundler in the Ruby world.
Drupal core uses Composer to manage non-Drupal dependencies like Guzzle, and PHPUnit to name a few. Some (and more all the time) contributed modules also use Composer to integrate 3rd party PHP libraries into Drupal.
We recommend using Composer whenever possible to manage Drupal core and contributed module code for your projects. While it is not, strictly speaking, required to use Composer, doing so will prevent you from running into an issue where some contributed modules will not function properly if they are not installed via Composer.
Additionally, we generally recommend using Composer to download all of a project's dependencies, and then committing them to a single repository versus rebuilding the codebase for each deployment
Example tasks
- Install Drupal core from a Git repository clone
- Start a new Drupal site build
- Download a contributed Drupal module that includes a composer library
- Add a third-party PHP library as a dependency for your custom or contributed module
Confidence
The way that Composer is used to manage Drupal sites, and project dependencies, isn’t likely to change significantly. We anticipate that it will continue to become the standard. And that the documentation available on Drupal.org will stay up-to-date with current best practices.
Drupalize.Me resources
External resources
- Official site for Composer
- For a quick overview check out PHP: The Right Ways section on Composer (phptherightway.com) and then dive into the official Composer Getting Started documentation (getcomposer.org)
- Learn how to install and use Composer to manage dependencies for any PHP project.
-
Using Composer to manage Drupal site dependencies (Drupal.org)
- Composer can be used to download Drupal, Drupal contributed projects (modules, themes, etc.), and all of their respective dependencies. These instructions vary based on your approach to managing your Drupal installation.
-
Composer requirements for Drupal (Drupal.org)
- Drupal 10 will require Composer 2.3.6 or higher, since this will be the requirement for using Automatic Updates. Composer can be updated by running
composer self-update
.
- Drupal 10 will require Composer 2.3.6 or higher, since this will be the requirement for using Automatic Updates. Composer can be updated by running
-
Using Composer (Drupal.org)
- Official Drupal community documentation on Composer and its use with Drupal, including packages.drupal.org. Learn how to manage a sites dependencies with Composer, and how to use Composer libraries in a contribute module
-
drupal-composer/drupal-project (github.com)
- Commonly used template for starting a new Drupal project. Includes additional utilities like Drupal console, and Drush. Use this as a starting point for new Drupal projects in order to scaffold inclusion of common settings and utilities. Read more about using drupal-project in Goodbye Drush Make, Hello Composer (lullabot.com)
-
Tips for Managing Drupal 8 projects with Composer (jeffgeerling.com)
- This post contains a bunch of tips/tricks that go beyond the standard install/uninstall a module instructions you’ll find in most other places. Learn about using version constraints, updating modules you’ve already installed, and more.
-
Composer the right way (youtube.com)
- Presentation by Rafael Dohms explains what Composer is, how it works, and how you can use it in a PHP project. Use this to get an overview of what Composer is and how it fits into the PHP ecosystem.
-
Managing dependencies for a custom project (Drupal.org)
- Learn how to use Composer to manage dependencies for your custom modules.