Drush is a command-line shell and scripting interface for Drupal, a veritable Swiss Army knife designed to make life easier for those who spend their working hours hacking away at the command prompt.

Drush core ships with lots of useful commands for interacting with code like modules/themes/profiles. Similarly, it runs update.php, executes SQL queries and DB migrations, and misc utilities like run cron or clear cache. Developers love the generate command, which jump starts your coding project by writing ready-to-customize PHP and YML files.

We are going to execute drush commands on Ubuntu 20.04 in this guide. Follow along

Server Requirements

Before we go ahead to take advantage of Drush, it is important to check that your server meets the folloeing requirements.

  • Nginx or Apache (with mod_rewrite enabled)
  • PHP 7.2.9 with the following extensions: curl, dom, gd, json, mbstring, openssl, pdo_mysql, tokenizer, zip
  • MySQL 5.6 or MariaDB 10.0.5
  • SSH (command-line) access to run Composer
  • It is required that Drupal sites be built using Composer, with Drush listed as a dependency.
  • Optional. To be able to call drush from anywhere, install the Drush Launcher

Step 1: Update and install essential tools

Once in the terminal of your fresh Ubuntu server, update it and install esential tools we shall use in our installation process

sudo apt update && sudo apt upgrade
sudo apt install vim wget unzip curl -y

Step 2: Install and setup database

We are going to use MariaDB for this setup. Fortunately, we have a detailed guide already to get MariaDB Database server installed.

Check out:

How To Install MariaDB on Ubuntu 20.04 (Focal Fossa)

After you have the database installed, the next step is to create a database and user for Drupal CMS. Create database and username as illustrated below. Note that you are free to name your database and user differently and ensure you use a safe password.

$ mysql -u root -p
Enter password: 

MariaDB [(none)]> CREATE DATABASE drupaldb;
MariaDB [(none)]> CREATE USER 'drupaluser'@'localhost' IDENTIFIED BY 'StrongPassword';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON drupaldb.* TO 'drupaluser'@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit;

Step 3: Setup PHP and web server

In order to get Drupal’s pages served, there has to be a web server. Here, you have the freedom of either picking Apache or Nginx. We shall use Nginx for this guide. Additionally, Drupal requires PHP and therefore we will have to set it up as well. We have a complete guide that covers the installation of Nginx and PHP-FPM on Ubuntu. Kindly follow it then proceed to the next step.

Add recommended PHP Settings

Open up your php-fpm ini file and add/edit the details shown below. They include Timezone, and memory limit settings. Add your date.timezone (at about line 955) and change memory_limit (at about line 400) to 512MB.

$ sudo vim /etc/php/7.4/fpm/php.ini

memory_limit = 512M

[Date]
date.timezone = Africa/Nairobi

Install composer

Composer is required to in order to get Drupal’s core files and its dependencies installed. Do the following to setup composer

cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Test if composer is successfully installed by running the composer command

$ composer -V
Composer version 1.10.9 2020-07-16 12:57:00

Step 4: Create Drupal project and configure Nginx

In this step, we shall download Drupal core and execute composer install to get all PHP dependencies sorted out

The root directory is the folder our Webserver will check for out Drupal’s files and serve them upon request. You can create a different one according to your needs.

$ cd ~
$ composer create-project drupal/recommended-project drupalcore

Creating a "drupal/recommended-project" project at "./drupalcore"
Installing drupal/recommended-project (9.0.2)

The above command might take some time to complete.

A new directory inside our home directory called drupalcore will be created and files copied into it

Copy or move the new directory created (drupalcore) to the webroot directory of your choice where Nginx can read.

sudo cp -rv ~/drupalcore /var/www/html/

Configure Nginx

We have to make a few changes to the Nginx configuration defaults by adding the details we need for Drupal. Change into sites-enabled, back up the default file and create a new one with new configurations.

cd /etc/nginx/sites-enabled/
sudo mv default /tmp

Create a new file and add the details shown below. If you have an FQDN, replace example.com with it.

$ sudo vim /etc/nginx/sites-enabled/drupal.conf

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  example.com;
        root         /var/www/html/drupalcore/web;
        index index.html index.htm index.php;

        location / {
                try_files $uri /index.php$is_args$args;
        }

        location ~ .php$ {
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_read_timeout 240;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_split_path_info ^(. .php)(/. )$;
        }
    }

Change Drupal’s files permissions

In order for Nginx to read the files, we have to grant it the rights and the right permissions. Issue the commands below to get that done.

sudo chown -R www-data:www-data /var/www/html/drupalcore
sudo chmod -R 755 /var/www/html/drupalcore/
sudo systemctl restart nginx php7.4-fpm

Step 5: Install Drupal using the command line

You can use Drush to install Drupal from the command line. Add Drush in your project by running: composer require drush/drush.

cd /var/www/html/drupalcore
sudo composer require drush/drush

Install Drush Launcher

Drush Launcher is a small wrapper around Drush for your global $PATH. It makes it possible to call drush command anywhere in your terminal not only within Drupal’s project.

By installing the drush launcher globally on your local machine, you can simply type drush on the command line, and the launcher will find and execute the project specific version of drush located in your project’s vendor directory.

Installation – Phar

Download latest stable release via CLI as shown code below.

cd ~
wget -O drush.phar https://github.com/drush-ops/drush-launcher/releases/latest/download/drush.phar

Make downloaded file executable:

chmod  x drush.phar

Move drush.phar to a location listed in your $PATH, rename to drush:

sudo mv drush.phar /usr/local/bin/drush

Run drush command

drush --root=/var/www/html/drupalcore

If you get this error message:

“The Drush launcher could not find a Drupal site to operate on. Please do one of the following”

  • Navigate to any where within your Drupal project and try again.
  • Add –root=/path/to/drupal so Drush knows where your site is located.

Do the following:

$ vim ~/.bash_profile

export PATH="$PATH:/var/www/html/drupalcore/vendor/bin/"

After you have edited the .bash_profile file. Source it as follows

source ~/.bash_profile

You should now be able to run drush –root=/path/to/drupal/root to get all options of drupal incase you are outside Drupal root directory.

Install Drupal using drush

Use drush site:install to run the command line setup wizard. Without any arguments it’ll install the standard profile and ask only for database credentials.

cd /var/www/html/drupalcore/
sudo drush site:install

You will get an output like below asking you for database credentials then it will begin Drupal Installation

Database name [drupal]:
 > drupaldb

 Database driver [mysql]:
 > 

 Database username [drupal]:
 > drupaluser

 Database password [drupal]:
 > 

 Database host [127.0.0.1]:
 > 

 Database port [3306]:
 >

 You are about to:
 * Create a sites/default/settings.php file
 * DROP all tables in your 'drupaldb' database.

 Do you want to continue? (yes/no) [yes]:
 > 

 [notice] Starting Drupal installation. This takes a while.
 [notice] Performed install task: install_select_language
 [notice] Performed install task: install_select_profile
 [notice] Performed install task: install_load_profile
 [notice] Performed install task: install_verify_requirements
 [notice] Performed install task: install_settings_form
 [notice] Performed install task: install_verify_database_ready
 [notice] Performed install task: install_base_system
 [notice] Performed install task: install_bootstrap_full
 [notice] Performed install task: install_profile_modules
 [notice] Performed install task: install_profile_themes
 [notice] Performed install task: install_install_profile
 [notice] Performed install task: install_configure_form
 [notice] Cron run completed.
 [notice] Performed install task: install_finished
 [success] Installation complete.  User name: admin  User password: WTcLHCPWUg

Once Drupal is installed, you can access it via the web as usual by pointing your browser to the ip or FQDN of your site.

Step 6: Drush Usage Examples

Drush can be run in your shell by typing drush from within your project root directory or anywhere within Drupal. Navigate to your root directory and execute drush commands

$ cd /var/www/html/drupalcore/
$ drush --version

Drush Launcher Version: 0.6.0
Drush Commandline Tool 10.3.1

Drush commands follow the following syntax

drush [options]  [argument1] [argument2]

Use the list command to get a list of available commands:

drush list

Examples of drush commands include the following

An overview of the environment – Drush and Drupal.

Run the following commands to get an overview of the environment – Drush and Drupal

$drush core:status

Drupal version   : 9.0.2                                                 
 Site URI         : http://default                                        
 DB driver        : mysql                                                 
 DB hostname      : 127.0.0.1                                             
 DB port          : 3306                                                  
 DB username      : drupaluser                                            
 DB name          : drupaldb                                              
 Database         : Connected                                             
 Drupal bootstrap : Successful                                            
 Default theme    : bartik                                                
 Admin theme      : seven                                                 
 PHP binary       : /usr/bin/php7.4                                       
 PHP config       : /etc/php/7.4/cli/php.ini                              
 PHP OS           : Linux                                                 
 Drush script     : /usr/local/bin/drush                                  
 Drush version    : 10.3.1                                                
 Drush temp       : /tmp                                                  
 Drush configs    : /var/www/html/drupalcore/vendor/drush/drush/drush.yml 
 Install profile  : standard                                              
 Drupal root      : /var/www/html/drupalcore/web                          
 Site path        : sites/default                                         
 Files, Public    : sites/default/files                                   
 Files, Temp      : /tmp                                                  

Create users

Drush provides an easier and fast interface to create users in your Drupal site as follows:

$ drush user:create tech
[success] Created a new user with uid 2

Set password for the new user created

Run the command below to set the password for the user account with the specified name.

$ drush user:password tech "StrongPassword"
[success] Changed password for tech.

Print information about the specified users

To get detailed information about users in your environment, there is an easy command to run just in your terminal

$ drush user:information user
$ drush user:information admin

 --------- ----------- ------------------- --------------- ------------- 
| User ID | User name | User mail         | User roles    | User status |
 --------- ----------- ------------------- --------------- ------------- 
| 1       | admin     | [email protected] | authenticated | 1           |
|         |           |                   | administrator |             |
 --------- ----------- ------------------- --------------- ------------- 

For even more documentation, use the topic command:

$ drush topic

Choose a topic:
  [0 ] Cancel
  [1 ] All global options. (core:global-options)
  [2 ] Bashrc customization examples for Drush. (docs:bashrc)
  [3 ] Bootstrap explanation: how Drush starts up and prepares the Drupal environment. (docs:bootstrap)
  [4 ] Configuration overview with examples from example.drush.yml. (docs:configuration)
  [5 ] Creating site aliases for running Drush on remote sites. (docs:aliases)
  [6 ] Crontab instructions for running your Drupal cron tasks via `drush cron`. (docs:cron)
  [7 ] Deploy command for Drupal. (docs:deploy)
  [8 ] Drupal config export instructions, including customizing config by environment. (docs:config:exporting)
  [9 ] Drush's support for Git Bisect. (docs:bisect)
  [10] Example Drush command file. (docs:examplecommand)
  [11] Example policy file. (docs:policy)
  [12] Extend sql-sync to allow transfer of the sql dump file via http. (docs:example-sync-via-http)
  [13] Instructions on creating your own Drush commands. (docs:commands)
  [14] Instructions on creating your own Drush Generators. (docs:generators)
  [15] Output formatters and filters: how to control the output produced by Drush commands (docs:output-formats-filters)
  [16] README.md (docs:readme)

As you can guess by now, Drush has a rich ecosystem, a lot of commands and support from developers and contributors. This guide cannot cover all of the details that Druch can accomplish. For more, we refer you to Drush usage page, its GitHub page as well as Drush commands for complete documentation and reference.

Drupal is the open-source CMS that helps you as a developer deliver ambitious, elegant, and performant digital experiences at scale. You can take advantage of its features and couple it up with Drush and your work becomes amazing. Finally, we appreciate your visit and we hope the guide as helpful. You can jump on other similar guides below for more fun.

Install and Configure Drupal 9 CMS on CentOS 8

How To Install Drupal 9 CMS on Ubuntu

Install and Configure Drupal 8 on CentOS 8 / RHEL 8

What Drupal 8 Can Do for Your Business

Install and Configure October CMS on CentOS 8