Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Drupal 8 Composer 101 tutorial

Parent Feed: 

This tutorial is for managing Drupal (>=8.1) with composer.

What is composer?

Git practices:

put /vendor in .gitignore

put /modules/contrib in .gitignore

Install site:

composer install

Modules:

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.

Dont's:

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}

Other:

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:

FAQ:

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.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web