Install and Use Drush, the Drupal Shell

Leave your reply

Introduction

Learn how to use Drush, a utility which allows you to work with Drupal from the command line. Drush can be used to check the status of a website, install and manage modules and themes, and more.

Requirements

  • A server running Linux (CentOS 7 or Ubuntu 14.04).
  • Drupal installed and running.
  • At least one Drupal website.

For more information on installing Drupal, see our articles:

Install Drush

To install Drush on CentOS 7 and Ubuntu 14.04, you can use PHP's Composer utility with the command:

sudo php -r "readfile('http://files.drush.org/drush.phar');" > drush

Next, make the Drush installation executable with the command:

sudo chmod +x drush

Finally, move the Drush files so that you can use the drush command from anywhere on the server:

sudo mv drush /usr/local/bin

Test the Drush installation with the command:

sudo drush status

If Drush is installed correctly, this will return information about the server:

user@localhost:~# sudo drush status
 PHP configuration      :  /etc/php5/cli/php.ini
 PHP OS                 :  Linux
 Drush script           :  /usr/local/bin/drush
 Drush version          :  8.1.3
 Drush temp directory   :  /tmp
 Drush configuration    :
 Drush alias files      :

Using an Alias to Work With Multiple Sites

If you have more than one Drupal site on your server, you will need to create aliases for them.

Begin by editing the aliases.drushrc.php file with the command:

sudo nano /root/.drush/aliases.drushrc.php

This is a PHP script, which means that the first line of the file needs to be:

<?php

And the last line needs to be:

?>

In between those brackets, we will be putting the aliases for two sites. The format for this is:

$aliases['site name'] = array(
   'root' => '/path/to/website/files',
   'uri' => 'website URL',
 );

For example, to create an alias to main-site which has its files located in /var/www/html and is visible at http://example.com you would use the following:

$aliases['main-site'] = array(
   'root' => '/var/www/html',
   'uri' => 'example.com',
 );

Let's say we have a second site we will refer to as other-site. We will need to add a second alias for this site:

$aliases['other-site'] = array(
   'root' => '/var/www/other-site.com/html',
   'uri' => 'other-site.com',
 );

Altogether, the file will read:

<?php

 $aliases['main-site'] = array(
   'root' => '/var/www/html',
   'uri' => 'example.com',
 );

$aliases['other-site'] = array(
   'root' => '/var/www/other-site.com/html',
   'uri' => 'other-site.com',
 );

?>

Save and exit the file. Then verify that you have set it up correctly by checking the status of the sites with the commands:

sudo drush @main-site status
sudo drush @other-site status

Clear the Cache

Drupal administrators frequently have to clear the Drupal cache. This is easy to do from the command line:

sudo drush [site alias] cc

Drush will ask you which cache(s) you want to clear.

If you want to clear all of the caches, you can use this command instead:

sudo drush [site alias] cc all

Working With Modules

You can use Drush to download and enable modules from the command line. To do this, you will need the module's "machine name." This is the short, command-line version of the module's name.

To find a module's machine name, find the module on Drupal.org. Click to view the module, and note the URL. The module's machine name is the last part of its URL.

For example, Token is one of the most popular Drupal modules. As you can see from its URL on the Drupal website:

Drush find a module's machine name

Token's machine name is token.

Download and Enable a Module

To download a module, the command is:

sudo drush [site alias] dl [module's machine name]

For example, to download the Token module (machine name token) for the @main-site website, the command is:

sudo drush @main-site dl token

Next, the module needs to be enabled. The command for this is:

sudo drush [site alias] en [module's machine name]

For example, to enable token on the @main-site website, the command is:

sudo drush @main-site en token

Disable a Module

To disable a module, the command is:

sudo drush [site alias] dis [module's machine name]

For example, to disable token for the @main-site site, the command is:

sudo drush @main-site dis token

Working with Themes

You can use Drush to manage themes from the command line. To do this, you will need the "machine name." This is the short, command-line version of the theme's name.

The process for finding a theme's machine name is the same as finding a module's name in the section above. First, find the theme on the Drupal.org website Click to view that theme, and note the URL. The theme's machine name is its URL.

For example, Zen is one of the most popular Drupal themes. As you can see from its URL on the Drupal website:

Drush find a theme's machine name

Zen's machine name is zen.

There are many ways you can use Drush to administer themes. Some of the most common are:

Download and Enable a Theme

To download a theme, the command is:

sudo drush [site alias] dl [theme's machine name]

For example:

sudo drush @main-site dl zen

Then enable it with the command:

sudo drush [site alias] pm-enable [theme's machine name]

For example:

sudo drush @main-site pm-enable zen

Set a Default Theme

You can set a theme as the default with the command:

sudo drush [site alias] vset theme_default [theme's machine name]

For example:

sudo drush @main-site vset theme_default zen

Disable a Theme

To disable a theme, the command is:

sudo drush [site alias] pm-disable [theme's machine name]

For example: