Understand Composer and Drush for Drupal 8

Points To Remember before you start

  • Check the composer requirements for the version of Drupal that you will install. 
  • Install any package with sudo if you are not a root user
  • Don't run composer with sudo
  • This document was created September, 2019, but is updated by Simple Information staff regularly. 
  • If you get stuck, see the IN A JAM section at the bottom

Composer vs. Drush: When to use which one?

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Composer is not a package manager in the same sense as Yum or Apt. Drush is a command line shell and Unix scripting interface for Drupal. Drush core ships with lots of useful commands for interacting with code like modules/themes/profiles. Similarly, it runs update.php, executes SQL queries and DB migrations, and misc utilities like run cron or clear cache.

Let's talk about when to use composer and when to you drush.

  • When you downloading, something composer can help you, whereas when you interact with drupal drush can help you.
  • In latest drupal version everything is changed, now composer used to download module whereas drush used to enable that module
  • Any dependency of a module can be downloaded by composer using composer.json file whereas drush is only limited to drupal

Composer

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Composer is not a package manager in the same sense as Yum or Apt are. Yes, it deals with "packages" or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project. By default it does not install anything globally. Thus, it is a dependency manager. It does however support a "global" project for convenience via the global command. This idea is not new and Composer is strongly inspired by node's npm and ruby's bundler.

Installation

To install composer globally run below command from your home directory

curl -sS -o composer-setup.php https://getcomposer.org/installer && php composer-setup.php --version=1.7.0

then, run

mv composer.phar /usr/local/bin/composer
[Note: you can change composer version as per your requirement]

DRUSH

Drush is a command line shell and Unix scripting interface for Drupal. Drush core ships with lots of useful commands for interacting with code like modules/themes/profiles. Similarly, it runs update.php, executes SQL queries and DB migrations, and misc utilities like run cron or clear cache. Developers love the generate command, which jump starts your coding project by writing ready-to-customize PHP and YML files. Drush can be extended by 3rd party command files.

Drush comes with drupal itself to install drupal follow our below article

https://www.simpleinformation.com/article/setup-guide-lamp-stack-and-drupal-8-ubuntu-server-1804-0

Most common Drush commands

To run drush go to docroot then run drush command.

To check Drush version

vendor/bin/drush status

To clear drupal cache

vendor/bin/drush cr

To build drupal registry

vendor/bin/drush rr

To Open SQL command-line interface

vendor/bin/drush sql cli

To print the database connection details

vendor/bin/drush sql-conf

To print drupal logs

vendor/bin/drush ws

To list of enabled modules

vendor/bin/drush pm-list

To enable site maintenance mode

vendor/bin/drush sset system.maintenance_mode 1

To disable site maintenance mode

vendor/bin/drush sset system.maintenance_mode 0

To set a config name

vendor/bin/drush config-set "config-name" "key"

To import configuration

vendor/bin/drush cim

To export configuration

vendor/bin/drush cex

To update database

vendor/bin/drush updb

To create one time login link

vendor/bin/drush uli

Managing modules with Drush and composer

[note: change "devel" with your module name

To enable a module

vendor/bin/drush en devel

To disable a module

vendor/bin/drush dis devel

To download a module

composer require drupal/devel

To update a module using composer

composer update drupal/devel --with-dependencies

Upgrading Drupal with Composer

First, verify that an update of Drupal core actually is available:

composer outdated "drupal/*"

If there is no line starting with drupal/core, Composer isn't aware of any update. If there is an update, continue with commands below. Assuming you are used to updating Drupal and know all the precautions that you should take, the update is as simple as:

composer update drupal/core --with-dependencies
vendor/bin/drush updb
vendor/bin/drush cr

This will update your drupal to latest version

Commonly used Composer commands

To install module based on composer.json

Composer install

To update a composer.json

composer update

To creates a basic composer.json file in current directory

Composer init

To Run the phpcs script as defined in composer.json. (php code validator script)

composer phpcs modules/devel

For more commands run below below command in your system

composer

What's the difference between composer.lock and installed.json?

composer.lock is generated when installing for the first time or updating. It contains references to the exact versions used. It should be committed into the version tracking repository to allow restoring this exact combination of libraries.

installed.json is an internal file of Composer. It's used when you remove a package manually from composer.json to remove the files from the vendor directory. Otherwise, the old vendor package would be around forever.

Need to install a patch for Drupal?

To create a patch for a module

git diff > devel.patch

To apply a patch to module

git apply -v devel.patch

IN A JAM

To know more details about composer go to

https://getcomposer.org/doc/

To know more details about drush go to

https://www.drush.org/

To know more details about patch

https://www.drupal.org/patch/apply

To know more details about installing drupal

https://www.simpleinformation.com/article/setup-guide-lamp-stack-and-drupal-8-ubuntu-server-1804-0