Drush

Body

Drush is one of the most popular, if not the most popular, Drupal development and sysadmin tool out there. Drush is the command line and scripting interface for Drupal. You can use it to speed up installing, developing, debugging and maintaining Drupal sites. It is also capable of generating boilerplate code for custom developments. It takes some getting used to, and there are a lot of commands to remember but, once you do, it is a real time saver. Rather than clicking through the admin interface to achieve something, you simply type in one command. When building Drupal sites and developing custom modules and themes, this can really help.

Historically, Drush was also used to download modules, but this is now done with Composer.

Drush is not a module and its code is hosted outside Drupal code on GitHub: https://github.com/drush-ops/drush

The official documentation can be found at https://www.drush.org (https://docs.drush.org/ for version 9 or less) and here's the basic fact to know about it:

Installation

It's recommended to use composer to install Drush inside your project

composer require drush/drush

Local dev tools such as DDEV or Lando provide aliases to run drush without path and from any directory.

If installed and run globally it will detect the local installation and use it instead. You can use the Drush Launcher,  but beware that since drush 12, this method is no more possible.

Compatibility

If you're not using the latest major version of Drupal, you may have to choose an older version of Drush: check the compatibility chart on drush web site.

Basic Usage

# Commands list : run drush without option
drush 

# Help about a command
drush help cache:clear

# Help about a more detailed topic
drush topic [topic]

# Empty cache (The most used command)
drush cache:rebuild

# The same with an alias 
drush cr

You will certainly use it for installing (or uninstalling) modules, importing or exporting configuration, run cron, manager users, ... see the list of all commands and options.

You can also extend it by creating your own command.

Tips

Updating Drupal the correct way from the command line

When updating a Drupal site, always respect this order in order to avoid configuration problems.

drush updatedb --no-cache-clear
drush cache:rebuild
drush config:import
drush cache:rebuild

If you want to execute post config import commands you could use a hook_post_update_NAME or the drush deploy command

Cannot use simply drush?

See info on how to add Drush to the $PATH. If this is not possible, you may need to use the full path (relative to you project root):

vendor/bin/drush status

See Document how to most easily make vendor/bin/drush available as drush #5828 for hints, like how to reset PATH after experimenting. It is recommended to read up on PATH and understand the fundamentals.

Drupal 7 and Drupal 10 on same web hotel

You may have both Drupal 7 and Drupal 10 on the same web hotel. In that case you will end up with a complex situation of both a global Drush 8 for Drupal 7 and local Drush 12 for each Drupal 10 installation.

To make this set up work, check if some paths are being registered, for example with something like this in ~/.bash_profile to find out where to place Drush 8:

PATH=$PATH:$HOME/.local/bin:$HOME/bin

Check the paths available with this:

$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/var/www/example.org/.local/bin:/var/www/example.org/bin

If a path in your own directory is available use that, like ~/.local/bin or ~/bin. Some prefer ~/.local/bin since that folder is hidden. You can install Drush 8 with something like this:

wget https://github.com/drush-ops/drush/releases/download/8.4.12/drush.phar
chmod +x drush.phar
mv drush.phar ~/.local/bin/drush

For more see:

Knowledge Category