Drupal 8 Composer 101 tutorial

This tutorial is for managing Drupal (>=8.1) with composer.
What is composer?
For getting started with Composer in general, please visit the offical documentation.
Git practices:
put /vendor in .gitignore
put /modules/contrib in .gitignore

Install site:

composer install
Use official Drupal package repository: https://www.drupal.org/node/2718229
composer config repositories.drupal composer https://packages.drupal.org/8
ALWAYS install modules using composer. Forget about Drush or even manual download!!
composer require drupal/{module_name}:{module_version}
Where {module_version} example:

8.x-1.1 => 1.1

8.x-2.2.0 => 2.2.0

We can support updates by saying 1.* which means next time you make composer install or update it will update to newer minor version if exists, but won't update to 2.x.
Don't use https://packagist.drupal-composer.org/ as this one is deprecated.
Don't install module manually or through Drush.
Don't change anything in modules source code. Patch it and add patch through composer.json.
Don't use composer manager module, this is no more needed as of Drupal 8.1
Uninstall a module:
Always uninstall a module in this 2 step way:
drush pm-uninstall {module_name}
composer remove drupal/{module_name}



You can always see latest library and module versions installed in composer.lock file.
Deploying modules to other devs or production:
Now you added a module locally through composer and want to push it? Since modules are now ignored in git, only thing that will be pushed is depenency in composer.json. Therefore to deploy the module, each developer will need to rebuild its dependencies using 
"composer install".
Drupal configuration manager will take care if the module is enabled or not. However order must be respected so you should import new configuration only after composer install is run, otherwise you might get a error.
So good practice after git pull is:
composer install
drush cim
drush cr
Drupal 8 composer starterkit:
Q:What if I downloaded a module manually or through Drush?
A: Just re-add it using composer require drupal/{module_name}:{module_version}
Make sure its in modules/contrib folder.

Share this post