Last updated 23 May 2016. Created on 12 January 2015.
Edited by charginghawk, Tschet, colan, HongPong. Log in to edit this page.

Notice: As of Drupal 8.1, Drupal core directly uses Composer to manage dependencies, which can include Drupal modules. Rather than use Composer Manager, the new recommended workflow for a module with Composer dependencies will be to require the module itself through Composer. For more information, see: Using Composer in a Drupal project, Using Composer to install Drupal packages through Drupal.org.

Overview

Composer requires a Drupal site to have a single composer.json file and vendor/ directory.
Drupal 8 ships with a root composer.json file which should be edited to include any module requirements. The composer-merge-plugin is used to merge the requirements from core/composer.json into the root composer.json.

Composer Manager provides the following functionality:

  • Discovers each module's composer.json file and adds it to the root composer.json merge list.
  • Adds the Drupal Packagist to the list of repositories, allowing drupal modules to be required the same way as other packages
  • The /admin/reports/composer-manager status report.

Provided commands

  • composer drupal-rebuild: Rebuilds the root composer.json file (replace, repository, extra/merge-plugin/include keys).
  • composer drupal-update: A shortcut for composer drupal-rebuild && composer update

Installation

  1. Install the Composer Manager module
  2. Run the module's init.php script on the command line in your Drupal root directory.
    (php PATH_TO_COMPOSER_MANAGER_MODULE/scripts/init.php).
    This registers the module's Composer command for Drupal core.
    Example:
    username@locahost:/var/www/SITENAME$ php modules/composer_manager/scripts/init.php
    

    You should see Composer Manager has been successfully initialized. once initialized properly.

  3. Run composer drupal-update from the the root of your Drupal directory.
    Example:
    username@locahost:/var/www/SITENAME$ composer drupal-update
    

Workflow

  1. Download the desired modules (such as Commerce).
  2. Run composer drupal-update from the root of your Drupal directory.
    This rebuilds composer.json and downloads the new module's requirements.
    Example:
    username@locahost:/var/www/SITENAME$composer drupal-update
    
  3. Install the modules.

People who can't run composer drupal-update on their servers can do the process locally, then commit the results.

You can also rebuild the composer.json file by simply running composer drupal-rebuild.

If you want to update the dependencies for a single module (after rebuilding composer.json), you can run composer update drupal/example_module.

Updating Drupal Core

  1. Follow the instructions over at Update procedure in Drupal 8, but do not Run update.php (stop before step 6).
  2. Run composer drupal-update.
  3. Continue with the standard update procedure: Run drush updatedb or run update.php (step 6).

Drush integration

When downloading or installing a module with Drush, composer drupal-update will automatically run afterwards.

Sometimes it is desirable to turn off this behavior, if you want to run composer drupal-update once after all downloads, or need to pass custom arguments to it.
In that case, set the SKIP_COMPOSER_UPDATE environment variable:

export SKIP_COMPOSER_UPDATE=1 && drush dl commerce

You can then rebuild composer.json manually, and call composer update with custom arguments:

composer drupal-rebuild && composer update --prefer-source

Build process workflow

Composer Manager can also run without a Drupal installation, making it usable as a part of a Drush Make site build process.

drush make test.make target
cd target
php modules/composer_manager/scripts/init.php
composer drupal-rebuild
composer update -n --lock --verbose

The site is now ready to be installed.

Adding dependencies to your module

Just include a composer.json file in your module root. The file should at least contain a name and requirements like so (requiring a Mailchimp library as an example):

{
  "name": "drupal/mymodule",
  "require": {
    "mailchimp/mailchimp": "2.0.6"
  }
}

To make sure that the user ran Composer and the library is present, add a hook_requirements implementation to your .install file. Example from Address module:

/**
 * Implements hook_requirements().
 */
function address_requirements($phase) {
  $requirements = [];
  if ($phase == 'install') {
    if (!class_exists('\CommerceGuys\Addressing\Repository\AddressFormatRepository')) {
      $requirements['addressing_library'] = [
        'description' => t('Address requires the commerceguys/addressing library.'),
        'severity' => REQUIREMENT_ERROR,
      ];
    }
  }

  return $requirements;
}

Note: Your module should not depend on Composer Manager. The module itself is not required to be enabled for commands to work, and other users might use different Composer strategies that don't involve Composer Manager.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

MartinMa’s picture

Uff, it seem i'm solving the problem

installed lates dev instead of beta4

composer is running only with php5.5 ... just was a little bit tricky to find out how to run composer
/usr/local/php5.5/bin/php ../bin/composer.phar drupal-update

bojanz’s picture

Sorry about the beta4 confusion. I've now tagged rc1 and updated the documentation.

The Composer installation docs specify that Composer should be downloaded into /usr/local/bin/composer, making it globally available ("composer drupal-update"). Looks like your hosting provider missed that step.

druglo’s picture

Following the installation section, the command "composer drupal-update" take you on error "bash: composer.phar: command not found".
Here most of us need simply to use composer for drupal updates...
This instrument, which began as a simple and flexible, it becomes difficult, complex and crooked.
Here we install Composer as module, and this go ok, drupal report shows the correct information about the dependencies, but no update command is possible...
Seems to me that this page does not talk about Global Installation of Composer or some other esoteric configurations, if is nedeed some other step, please write it. Thanks

bojanz’s picture

Composer Manager is a helper module for Composer usage.
You are required to install the Composer command line tool yourself, as documented on its website: https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx

druglo’s picture

Thank you, I landed on this page for miss dependencies of commerce module, didn't known Composer, I just needed to properly handle the installation of the modules required by commerce. For this type of use, where would you recommend me to install it?

bojanz’s picture

Follow the instructions I listed above. Composer is a system utility similar to Drush, you'll want to put it in a global place (or your home folder, if you don't have higher permissions than that).

zmove’s picture

Same problem here :

I have to add that I use Acquia Dev Desktop that should already have composer and I could init it by typing the first command, so I don't think this is a problem of composer install.

dazweeja’s picture

This is working perfectly for me but I'm wondering what effect using this has on the preferred Drupal core update workflow. For example, would it be sufficient to:

1. Replace 'core' folder with latest version
2. Run 'composer drupal-update'
3. Run 'drush updb'

bojanz’s picture

Yes.

vinta’s picture

I do this username@locahost:/var/www/SITENAME$ php modules/composer_manager/scripts/init.php and

Warning: json_encode() expects parameter 2 to be long, string given in /my_site/modules/composer_manager/src/JsonFile.php on line 68
Composer Manager has been successfully initialized.

And after this file composer.json has become empty

bojanz’s picture

This is not a page for bug reports. please open an issue at drupal.org/project/issues/composer_manager

poida22’s picture

just an average guy here
If logging on remotely to run this file from a terminal is the only way to get commerce running using composer then say goodbye to so many mums and dads wanting to use Drupal.
Ive spent over a month on and off now trying/giving up including drone IT comments from my service provider that dont help.
Still not working....
Seems strange unless im missing something major here but this isnt what i thought Drupal was about?

bojanz’s picture

You don't need to run Composer on your server, run it locally and upload the result.

Once Commerce 2.0 is ready we will be offering Kickstart-like tarballs that don't require Composer, but it's too early for that while we're still in alpha.

poida22’s picture

Hi Bojanz

finally got the terminal to work on host now i still have problem... oh the pain of it all...

[~]# public_html/modules/composer_manager/scripts/init.php
public_html/modules/composer_manager/scripts/init.php: line 1: ?php: No such file or directory
public_html/modules/composer_manager/scripts/init.php: line 3: syntax error near unexpected token `{'
public_html/modules/composer_manager/scripts/init.php: line 3: `if (PHP_SAPI !== 'cli') {'

bojanz’s picture

init.php is a PHP script, you need to run it with "php". Follow the instructions as they're written, and you won't be in pain :)

poida22’s picture

no more pain, thanks

markusa’s picture

Does anyone have any experience or thoughts about using this module with a multi-site Drupal 8 installation?

Will it work?

Colin Fitz-Gerald’s picture

I want to get the new AMP module up and running on a new Drupal8 installation. I know alot about Drupal but I'm a newbie when it comes to using ssh and the command line. I've got as far as initializing the Composer Manager and applying the patch:

zamplets@a2ls12 [~/public_html]# curl https://www.drupal.org/files/issues/2664274-19-fix-composer.patch | patch -p1
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
110 663 110 663 0 0 1702 0 --:--:-- --:--:-- --:--:-- 2437
patching file core/lib/Drupal/Core/Composer/Composer.php
Hunk #1 succeeded at 162 (offset -3 lines).

My ego was exploding into heretofore unknown realms until I ran into this: zamplets@a2ls12 [~/public_html]# composer drupal-update
-jailshell: composer: command not found

I can go no further and my ego is rapidly deflating. Am I in the wrong directory when I issue the composer command? Should I shuffle over to core/lib/Drupal/Core/Composer/ and then run the composer command?

BTW Drupal8 is awesome.

bojanz’s picture

@Colin Fitz-Gerald
Your server doesn't have Composer installed. See other comments on this page.

Colin Fitz-Gerald’s picture

I installed Composer on my server and it works!

poida22’s picture

im sorry. Do i need to do a php course?
I have initialised, now i type "composer drupal-update" and no good. Tried it in root, every folder i can find to no avail.

bojanz’s picture

What does "no good" mean? If you're asking for help, at least paste the messages that you're getting.

poida22’s picture

Sorry
I am in the directory: public_html and I can see the composer.json and lock files, core and module folders etc. so I take it I'm in the root?
I type
composer drupal-update
Bash: composer: command not found

Thanks for help

qiqiy_ruby’s picture

it seems you need to install composer on your service first.

https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx

poida22’s picture

Fixed

poida22’s picture

Fixed I think
It saying its downloading dependencies

Had to use

php composer.phar drupal-update

saaniaki’s picture

Hey guys,
I'm running SSH on a 1and1 shared server. I installed composer tar.gz file from Drupal 8 itself, then when I run this code:
php modules/composer_manager/scripts/init.php

I will get:

X-Powered-By: PHP/4.4.9
Content-type: text/html

This script must be run from the command line.

After lots of searching I tried php6 modules/composer_manager/scripts/init.php as it worked for bunch of people, but still I'm getting kind of same response:

X-Powered-By: PHP/5.4.45
Content-type: text/html

This script must be run from the command line.

I just stuck at this step and I've no idea what should I do!

BTW, I'm running the command from here:
(uiserver):[username]:~/subfolder > where [username] is my username and subfolder is my Drupal 8 root folder.

bojanz’s picture

That's a question for your hosting provider.

saaniaki’s picture

yeah, I think so... after puting php -v

PHP 5.4.45 (cgi-fcgi) (built: Mar 31 2016 16:37:21)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies

which is showing I'm not using CLI which can answer why I'm getting that error. So now the question is can I enable CLI myself from SSH or I have to contact the provider?! If I can do it myself plz help me to do so.

To let you know, I ran php6 -cli modules/composer_manager/scripts/init.php but -cli it's not working like that.

saaniaki’s picture

/usr/bin/php5.5-cli modules/composer_manager/scripts/init.php
/usr/bin/php5.5-cli composer.phar drupal-update

charginghawk’s picture

I recommend against doing a full composer drupal-update. It updates a vast amount of packages used by core, and it's not unusual for a new version of one of these to break functionality. For simplicity's sake, you can do module-specific updates by running composer update drupal/my_module.

This is a much easier workflow when all you're using composer_manager for is to install a new module, since it only installs just the dependencies for the new module. Updating every package Drupal depends on every time you want to install a module could very well drive you crazy.

bojanz’s picture

The best approach is to not use Composer Manager at all, but to require the entire Drupal module instead.

Here are the current Drupal Commerce install instructions:

Run these commands in the root of your website:

1) Add the Drupal Packagist repository
- composer config repositories.drupal composer https://packagist.drupal-composer.org
This allows Composer to find Commerce and the other Drupal modules.

2) Download Commerce
- composer require "drupal/commerce 8.2.x-dev"
This will also download the required libraries and modules (Address, Entity, State Machine, Inline Entity Form, Profile).

This didn't work correctly until Drupal 8.0.6 and Drupal 8.1.0-RC1. Now that it does, we'll be able to effectively deprecate Composer Manager.

waibiaf2’s picture

Hi, guys, it seems everyone explains linux or mac..... Is there a specific way of doing it on windows 10????

Maplinx’s picture

For a good while I ran into difficulties getting the init.php file to run after downloading composer manager on a drupal 8 installation. Like other newbies I felt quite frustrated with the drupal requirement for composer to deploy dependencies for some of its third party modules. My goal was of course to install address module and its dependencies.
The best and probably easiest solution for less experienced site developers is to install PuTTy (putty.org) and run the command line php script via the drupal root via a SSH assuming you have a VPS or dedicated server. As this was new to me I ran into similar errors noted on this page earlier.

After successful login as 'user@sitename.com' you should have a prompt which may differ depending on your server. In my case it was '#'. You would need to be familiar with cmd line language to navigate into your server root (drupal) folder (~public_html).
'pwd' shows you your working directory and you may 'ls' [list] all files/folders in any directory.
It is then a case of entering this line in the 'drupal' root to activate the init.php

"user@sitename.com[~/public_html]# php modules/composer_manager/scripts/init.php
Composer Manager has been successfully initialized".

After initializing the composer manager, you would, by clicking on the 'help' link (extend/module details), be able to see all the dependencies and their current state -whether active or not. At this stage my installed dependent modules could not be discovered by drupal and I had problems getting the address module activated.

Unfortunately reading through the forum can be confusing when the 'experts' are oblivious of communication gaps as they attempt to explain these issues with undue presumption. Fortunately reading through this link https://www.drupal.org/node/2730825 and Ashokshrestha comment #10 was quite helpful.

Firstly, make sure you have installed the commerceguys and wikimedia (and other third party) dependencies correctly in the vendor folder (public_html/vendor).

Next confirm from your cpanel/file manager/drupal root that 'composer.json' file (which comes with drupal installation) is updated to indicate where these 'required' files are located. You may need to enter this yourself if not already updated by composer rebuild script (again triggered via cmd line php code on root folder).
"require": {
"composer/installers": "^1.0.21",
"wikimedia/composer-merge-plugin": "~1.3",
"commerceguys/addressing": "~1.0 ",
"commerceguys/intl": "~0.7 ",
"commerceguys/zone": "~1.0 "
},
etc
Now also observe where the 'autoload.php' is run from as this could either be within the vendor folder or in the document root.

"extra": {
"_readme": [
"By default Drupal loads the autoloader from ./vendor/autoload.php.",
"To change the autoloader you can edit ./autoload.php."
],
etc..
This is important as it determines whether your "require" files should be "vendor/commerceguys/addressing... or just "commerceguys/addressing...". The pre-determined or default position is that autuload.php is in the vendor folder.

Following this I was able to activate address module and all other commerce modules easily.

bojanz’s picture

Where did you find the suggestion to use Composer Manager?
Composer Manager for D8 was abandoned a year ago, and the Address / Commerce install instructions were then rewritten to use the simpler method of getting whole modules via Composer.

Maplinx’s picture

Thanks. Yes composer manager is now being replaced by using composer.json directly to manage dependencies if I understand correctly. I am aware also drupal 8 is moving towards making composer native to core drupal. Unfortunately in trying to install Address module and its dependencies, I did get prompted to install composer manager some where. However as noted in my comment (which was really tough luck!) I still had to initialize the composer manager (which probably may not have been needed) but allowed me to see what dependencies were active and if not what to do about them. Most of my activity were via cmd line and working with the docroot composer.json file by the way.
I sure did learn a lot in the process by the way. Again clarity will help less advanced drupal users like me.

bojanz’s picture

I'm the maintainer of Address module. It contains no mention of composer_manager in the code, readme, or the project page.
Composer Manager was abandoned precisely because it imposed additional confusing steps compared to the pure-Composer approach.
I try to update documentation based on user feedback, but it's hard for that to matter when people run into a year-old outdated approach :/

Maplinx’s picture

I am aware you are. And I am sure you could do better but your responses from time have always followed the same tone. Well done for maintaining the module however.

dscoop’s picture

Hi,

If you are hosting your website on a shared host, where you cant use terminal console commands through SSH, how can we then install libraries with this module?

// Cooper Webdesign

vinta’s picture

Tell me, from which folder you would run the command Commerce updates composer update drupal/commerce --with-dependencies ?

heykarthikwithu’s picture

In general, composer update should be done from the project's root folder.

karthik@mac:/var/www/html/project$ composer update drupal/commerce --with-dependencies

vinta’s picture

Thank you very much