Introduction to Drush

Objectives
  • Describe the purpose of Drush.
  • Describe how to install Drush.
  • Display your version of Drush.
  • Describe how to add more Drush commands.
  • Describe how Drupal core and PHP versions influence which version of Drush you can use.
Prerequisites
  • Basic command-line usage.
  • Basic DDEV usage recommended but not required.
Last revision:

Introduction

Drush, short for "Drupal Shell", is a command-line utility for managing Drupal sites.  

Why would you manage a website from the command-line? Performing administrative tasks from the command-line is often quicker and takes less effort than clicking through the web-based admin interface.

And if you want to make your own life easier with some custom scripts for one-time actions, or need to perform automated actions as part of a testing or deployment pipeline, a command-line tool such as Drush is absolutely necessary.

You can use Drush to perform a variety of administrative tasks, such as:

  • perform a full site (re-)installation
  • install modules and themes
  • manage content and users
  • make or synchronise configuration changes
  • run database updates
  • run custom scripts and SQL queries
  • clear the caches
  • read and update configuration settings
  • create one-time login links
  • show the latest log entries
  • ...

Everyone can use Drush, regardless of their level of expertise. Beginners might only use Drush to clear caches and install modules, while more experienced users can use Drush to generate and debug code, create and restore database snapshots, or import and export configuration changes.
 

Drush installation method

As of January 2022, Drush 10 and higher must be installed with Composer: 

composer require drush/drush

→ See the section on installing Drush to learn how to install Drush.

Note on using DDEV
If you are using local development environments based on virtual machines or containers (such as DDEV or Lando), be sure to install Drush inside your development environment, not on your host machine.

Note on previous installation methods
Drush 9 and older versions could be installed per-site or site-wide using different installation methods. As of D10 installing Drush sitewide is no longer recommended.
 

Drush versions

The version of Drush you need depends on which version of Drupal and PHP you are using:

Drupal 10

  • Drush 12
  • PHP 8.1 or higher

Drupal 11

  • Drush 12 or 13
  • PHP 8.3 or higher

See https://www.drush.org./12.x/install/#drupal-compatibility for the full Drupal compatibility overview. 
 

Displaying your Drush version

If you're not sure which version you have installed, run drush --version, which should result in something like this:

Drush Commandline Tool 12.4.3.0

Getting more Drush commands

You can extend Drush by writing your own custom Drush commands, or by installing contributed modules that do so.

Example

Drupal's views data export module provides a way to make Views data available for download in various formats (JSON, XML, CSV, XLS, ...). It also provides a Drush command to perform this data export from the command-line.

Activity

What is Drush and who uses it?

Answer / solution

Drush is a command-line tool to manage Drupal sites.

Site builders, developers, themers, and sysadmins use Drush for various aspects of the site building and maintenance process.

Activity

How do you check which version of Drush you have installed?

Answer / solution
drush --version

Activity

Can I install Drush site-wide or do I have to install it for each individual site?

Answer / solution

While it might be technically possible to use one version of Drush for all of your sites, as of D10 this is no longer recommended. Every site should have it's own copy of Drush as a dependency.

Summary

  • "Drush" stands for Drupal Shell.
  • Drush is a command-line utility for managing Drupal sites.
  • Drush comes with many commands; more can be added via custom code or by installing modules that provide their own Drush commands.
  • Use composer require drush/drush to install Drush via Composer. All other installation methods are outdated.
  • The version of Drush you need depends on which version of PHP and Drupal you are using.
  • Use drush --version to check which version you have installed.