Backend and Infrastructure

Install Drush Using Composer for Drupal 7, 8, 9, and 10

Drush is the command line shell and Unix scripting interface for Drupal. The most common way to install Drush is to install it on a per-project basis using Composer. We'll walk through the steps to do that, as well as how to set up the Drush Launcher tool (to make it possible to execute Drush commands without having to specify a full path to the executable).

In this tutorial we'll:

  • Install Drush
  • Verify it worked

By the end of this tutorial you'll have Drush installed.

Goal

Install Drush and verify it's working.

Prerequisites

Install Drush using Composer

This assumes that your Drupal codebase is managed using Composer. The current recommendation is to install Drush on a per-project basis. This allows for having different versions of Drush installed for different projects. This is a best practice because different versions of Drush are compatible with a particular set of Drupal versions.

Run composer require

In most cases it's best to install Drush as a development dependency. From the root directory of your project run the following command:

composer require --dev drush/drush

Once that's completed run the command ./vendor/bin/drush --version to verify it worked.

./vendor/bin/drush --version
# > Drush Commandline Tool 10.6.2

Using the Drush command

The standard way to invoke Drush is by using the full path to the executable. For example, running ./vendor/bin/drush <command> from the root directory of your Drupal project. How exactly you call the drush command will depend on your local environment and its specific configuration.

If you're using DDEV, you can invoke Drush from outside the container with ddev drush <command>, or inside the web container (after running ddev ssh) with a plain drush <command>.

In other local environments, you may need to configure your $PATH environment variable to include ./vendor/bin, to avoid having to type the full path every time.

The important thing to be aware of is that if you copy/paste Drush commands, you might need to adjust the path to the Drush executable for your specific environment.

Recap

In this tutorial we learned how to use Composer to install Drush.

Further your understanding

  • Run the drush command with no arguments to see a list of available Drush commands.

Additional resources

Command Line Tools for Drupal