Drush – the essential Drupal command line tool

Drush is a command line tool for Drupal that allows developers to both undertake routine tasks from the cli as well as create their own tasks or manage more complex processes.

Using Drush is something of a no-brainer for developers and you can make as much or as little of it as you want.  We’ll be coming back to Drush time and again in this blog, but for now here’s how to get started and some useful commands to run.

Installing Drush

Drush is installed via Composer when you setup your Drupal 10 site.  You don’t need to install anything yourself.  However, you may need to specify the version of PHP specifically for Drush, depending on your setup.  You can do this in your bash profile, like this (in this example we’re setting Drush to run PHP8.1):

export DRUSH_PHP=/Applications/MAMP/bin/php/php8.1.13/bin/php

Running Drush

Head to your Drupal 10 root in Terminal and run drush.  You'll see a long list of commands you can use, if everything works.  You may find that Drush is not accessible globally and you need to be more specific.  In which case you can run vendor/bin/drush to directly run drush as installed on your Drupal 10 site.

Useful commands

  • drush cr clear the Drupal cache
  • drush pm:install <module_name> install a Drupal module
  • drush uli generate a one-time link to login to the CMS as admin user
  • drush sql-dump --gzip --result-file="/@DATABASE_@DATE.sql" dump the database as gzip file with database name and date as the filename
  • drush cex export the configs to the sync directory