How to Manage Drupal 8 Web Servers Using Composer

Introduction: CLI Administration Tools for Drupal 8 Servers

This Tutorial will show how to manage Drupal 8 web servers, including configuration & dependencies for required modules & programming languages, using Composer.

Composer is a PHP package management utility for installing web server extensions or managing libraries. Composer works with Drush to administer Drupal 8 web servers.

Developers who need a CLI administration tool for Drupal 8 web servers can use Composer & Drush together for a powerful solution, simplifying many important tasks.

Step One: Install Composer on the Web Server

In order to install Composer on the web server, it is possible to either run the installer.exe directly or through the input of the following script into the CLI utility:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

The latest version of Composer is v.1.7.3 (2018-11-01). Otherwise, download the file:

Developers who need to install Composer on a localhost WAMP server for programming, sandboxing, & testing can run the .exe file on their workstations.

Step Two: Install Composer Module for Drupal 8

After installing Composer on the web server, it is recommended to install the integration module on the Drupal CMS in order to run Drush & Composer together.

Follow this HostAdvice tutorial to install Drush:

Required Files - Download:

If Drush has already been installed (recommended), just run the commands:

drush dl composer-8

drush en composer-8

This will download & install the Composer module for Drupal 8 automatically.

The Drush/Composer commands enabled by the module are:

List all available Composer commands:

drush composer

Parse composer.json & install all dependencies:

drush composer install

Updates dependencies to the latest version, & update cache:

drush composer update

Wizard to create own composer.json file:

drush composer init

Download Symfony & all dependencies:

drush composer create-projectsymfony/symfony

Use these commands to extend the default Drush & Composer options.

Step Three: Managing Drupal 8 Modules & Themes

Composer can be used with Drush to manage Drupal 8 modules & themes. The advantage of using Composer over Drush is that Composer will update the .json file.

composer require drupal/<modulename>

The other advantage of using Composer over Drush is that Composer will automatically locate, download, & install any required dependency files or libraries for the modules. This command can also be run to require a specific version:

composer require 'drupal/<modulename>:<version>'

For modules that need to be converted to Composer, use the Composerizer app. The screenshot below shows a list of all Composer commands from the CLI:

How to Manage Drupal 8 Web Servers Using Composer

Overall, Drush is more powerful in managing Drupal 8 modules & themes, whereas Composer is more helpful in detecting the correct library files or server extensions.

Step Four: Managing Dependencies for Drupal 8 Projects

Composer makes it easy to install Drupal 8 modules using the command line. The information behind the Composer command is stored in the composer.json file:

composer require drupal/<modulename>

To define these values, developers need to include the installer path information in the composer.json file. For example, to require a specific Drupal 8 module version:

"require": {
        "drupal/token": "^1.5"
    }

To set the installer paths for the dependencies of the module:

"extra": {
    "installer-paths": {
        "core": ["type:drupal-core"],
        "libraries/{$name}": ["type:drupal-library"],
        "modules/contrib/{$name}": ["type:drupal-module"],
        "profiles/contrib/{$name}": ["type:drupal-profile"],
        "themes/contrib/{$name}": ["type:drupal-theme"],
        "drush/{$name}": ["type:drupal-drush"],
        "modules/custom/{$name}": ["type:drupal-custom-module"],
        "themes/custom/{$name}": ["type:drupal-custom-theme"]
    }
}

Practical examples of code values in composer.json:

composer require'drupal/token:^1.5'
composer require'drupal/simple_fb_connect:~3.0'
composer require'drupal/ctools:3.0.0-alpha26'
composer require'drupal/token:1.x-dev'

Normally, only Drupal 8 module & theme developers will need to worry about hard-coding the Composer dependencies in the composer.json file.

Step Five: Using Composer for Drupal 8 Site Maintenance

Composer & Drush work together to speed up common Drupal 8 maintenance tasks like core updates, applying module/theme security patches, or version upgrades.

To upgrade Drupal 8 core using Composer, first check the status of the install:

composer outdated drupal/*

Next, run the commands to update Drupal8 core files, database, & cache:

composer update drupal/core --with-dependencies
drush updatedb
drush cr

To backup the default database of a Drupal 8 installation:

drush sql-dump

To backup a zip archive of all Drupal 8 site files:

drush archive-dump

It is recommended to always make a database backup before performing operations like new module updates or Drupal core version upgrades.

Remember to always put the website in Maintenance Mode when working:

drush sset system.maintenance_mode1

To take the site out of Maintenance Mode again:

drush sset system.maintenance_mode0

Note: Users experiencing any issues with using Composer for Drupal 8 module, theme, & core updates can check this page for known issues, i.e. on Drupal 8.4 & 8.5.

Conclusion: Works Best with Drush for D8 Administration

Composer works seamlessly with Drush to provide powerful command line tools for Drupal 8 capable of installing/updating modules or themes, including Drupal 8 core.

Normally it will require VPS hosting,dedicated server hosting, or cloud hosting to install Composer & Drush, but some shared web hosting plans now offer this as part of Drupal 8 managed platforms. Use Composer & Drush to save hours of development & maintenance time.

Check out these top 3 Drupal hosting services:

Was this article helpful?