Composer Manager for Drupal 6 and Drupal 7

Last updated on
30 April 2025

Maintaining Dependencies

As modules are enabled and disabled, Composer Manager gathers their requirements and generates a consolidated composer.json file in the "Composer File Directory" as configured in Composer Manager's settings page.

There are two ways to install and update the contributed modules' dependencies:

Using drush en and drush dis to enable and disable modules respectively will automatically generate the consolidated composer.json file and run the appropriate Composer commands to install and update the required dependencies.

After doing the above action, don't forget to run the following command by going to sites/default/files/composer
composer install

This technique introduces the least amount of friction with existing workflows and is strongly recommended.

The following Drush commands are also available:

  • drush composer-json-rebuild: Force a rebuild of the consolidated composer.json file
  • drush composer-manager [COMMAND] [OPTIONS]: Pass through commands to Composer, refer to the cli tool's documentation for available commands and options.

Manually With Composer

If you do not wish to use Drush, you must manually use Composer's command line tool to install and update dependencies whenever modules are enabled or disabled. The following steps illustrate the workflow to maintain the dependencies required by a contributed module:

  • Visit admin/modules and enable / disable the modules that have dependencies
  • Change into the "Composer File Directory" as configured in Composer Manager's settings page. This is where the consolidated composer.json file was generated
  • If necessary, download and install the Composer tool
  • Run php composer.phar install --no-dev on the command line, replace install with update when updating dependencies

Refer to Composer's documentation for more details on how Composer works.

Configuring Composer Manager

Visit admin/config/system/composer-manager/settings for Drupal 7 or admin/settings/composer-manager/settings for Drupal 6 as a user with the administer site configuration permission to configure Composer Manager.

It is recommended to maintain a project structure where the composer files and vendor/ directory exist alongside the document root. This can be achieved by modifying the following options in Composer Manager's settings page.

  • Vendor Directory: ../vendor
  • Composer File Directory: ../

You can also set the options in settings.php by adding the following variables:

$conf['composer_manager_vendor_dir'] = '../vendor';
$conf['composer_manager_file_dir'] = '../';

NOTE: The recommended settings are not the defaults because we cannot assume that this structure is viable for all use cases. Furthermore, the "Composer File Directory" is set to a path we know is writable by the web server so the automatic building of composer.json works out of the box.

Multisite

It is recommended that each multisite installation has its own library space since the dependencies are tied to which modules are enabled or disabled and can differ between sites. Add the following snippet to settings.php to group the libraries by site in a directory outside of the document root:

// Capture the site dir, e.g. "default", "example.localhost", etc.
$site_dir = basename(__DIR__);
$conf['composer_manager_vendor_dir'] = '../lib/' . $site_dir . '/vendor';
$conf['composer_manager_file_dir'] = '../lib/' . $site_dir;

NOTE: The sites/*/ directories may seem like an obvious location for the libraries, however Drupal removes write permissions to these directories on every page load which can cause frustration.

Production Environments

Dependencies should be managed in development environments and not in production. Therefore it is recommended to disable the checkboxes that automatically build the composer.json file and run Composer commands when enabling or disabling modules on production environments.

Assuming that you can detect whether the site is in production mode via an environment variable, adding the following snippet to settings.php will disable the options where appropriate:

// Modify the logic according to your environment.
if (getenv('APP_ENV') == 'prod') {
  $conf['composer_manager_autobuild_file'] = 0;
  $conf['composer_manager_autobuild_packages'] = 0;
}

Usage for Drupal module maintainers

Module maintainers can use Composer Manager to maintain their third party dependencies by creating a composer.json file in the module's root directory and adding the appropriate requirements. Refer to Composer’s documentation for details on adding requirements.

It is recommended to use version ranges and tilde operators wherever possible to mitigate dependency conflicts.

hook_composer_json_alter(&$json) can also be implemented to modify the data used to build the consolidated composer.json file before it is written.

Relying on composer manager in .install

Composer manager will automatically handle the autoloader in hook_init(), so modules generally do not trigger the autoloader. However there are occasions where hook_init() is not invoked, e.g., during install and update.php. If you rely on the autoloader in an .install file, you have to make sure the autoloader is triggered by running composer_manager_register_autoloader() at the beginning of your update function or your hook_install() implementation.

Help improve this page

Page status: Not set

You can: