Use Drush command line shell to manage Drupal CMS

You can download this article in PDF format via the link below to support us.

Download the guide in PDF format

turn off


Drush is Drupal’s command line shell and scripting interface. Drupal is a veritable Swiss army knife designed to make it easier for people who spend a lot of time hacking at the command line prompt.

Drush Core comes with many useful commands that can be used to interact with modules/themes/configuration files and other codes. Similarly, it runs update.php, performs SQL queries and database migrations, and other miscellaneous utilities, such as running cron or clear cache. Developers like the generate command, which can start your coding project by writing easy-to-customize PHP and YML files.

In this guide, we will execute the drush command on Ubuntu 20.04. Follow

Server requirements

Before continuing to use Drush, be sure to check whether your server meets the following requirements.

  • Nginx or Apache (mod_rewrite enabled)
  • PHP 7.2.9+ has the following extensions: curl, dom, gd, json, mbstring, openssl, pdo_mysql, tokenizer, zip
  • MySQL 5.6 or above or MariaDB 10.0.5 or above
  • SSH (command line) access to run Composer
  • Composer is required to build a Drupal site and Drush is listed as a dependency.
  • Optional. To be able to call drush from anywhere, please install Drush Launcher

Step 1: Update and install basic tools

After entering the terminal of the new Ubuntu server, update it and install the basic tools that will be used during the installation process

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

Step 2: Install and set up the database

We will use MariaDB for this setup. Fortunately, we already have a detailed guide to install MariaDB database server.

check out:

How to install MariaDB on Ubuntu 20.04 (Focal Fossa)

After installing the database, the next step is to create the database and users for Drupal CMS. Create the database and username as shown below. Please note that you are free to use different names for the database and users, and make sure to use secure passwords.

$ 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)]> exit;

Step 3: Configure PHP and web server

In order to provide Drupal pages, there must be a web server. Here, you can choose Apache or Nginx. We will use Nginx in this guide. In addition, Drupal requires PHP, so we must also set it up. We have a complete guide covering installing Nginx and PHP-FPM on Ubuntu. Please follow it and proceed to the next step.

Add recommended PHP settings

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

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

memory_limit = 512M

date.timezone = Africa/Nairobi

Install composer

Composer must be installed to install Drupal’s core files and its dependencies. Do the following to set up the composer

cd ~
curl -sS -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Test whether 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 a Drupal project and configure Nginx

In this step, we will download the Drupal core and execute composer install to sort out all the PHP dependencies

The root directory is the folder where our web server will check out Drupal files and provide them as needed. You can create a different object as needed.

$ 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 may take some time to complete.

A new directory called drupalcore will be created in our home directory and the files will be copied into it. Copy or move the new directory (drupalcore) created to the webroot directory that Nginx of your choice can read.

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

Configure Nginx

We have to make some changes to the Nginx configuration defaults by adding the details required by Drupal. Change to enable the site, back up the default file and create a new file with the new configuration.

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

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

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

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        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 file permissions

In order for Nginx to read the file, we must grant permissions to it. Issue the following commands to complete the task.

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 install Drupal from the command line using Drush. Add Drush to the project by running the following command: Composer requires Drush / Drush.

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

Install Drush Launcher

Drush Launcher is a small wrapper of Drush for your global $PATH. Not only in the Drupal project, you can also call the drush command anywhere in the terminal.

By installing the drush launcher globally on your local computer, you only need to type drush on the command line, and the launcher will find and execute the project-specific version of drush in the supplier directory of your project.

Installation – Phar

Download the latest stable version via CLI as shown below.

cd ~
wget -O drush.phar

Make the downloaded file executable:

chmod +x drush.phar

Move drush.phar to the location listed in $PATH and rename it to drush:

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

Run the drush command

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

If you receive the following error message: “Drush Launcher cannot find the Drupal site to run. Please do one of the following”

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

Please do the following:

$ vim ~/.bash_profile

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

After editing the .bash_profile file. The source is as follows

source ~/.bash_profile

Now, if you are not in the Drupal root directory, you should be able to run drush –root=/path/to/drupal/root to get all drupal options.

Install Drupal with Drush

Use drush site: install to run the command line installation wizard. Without any parameters, it will install the standard configuration file and only ask for database credentials.

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

You will get the output shown below, asking you to provide database credentials, and then it will start the Drupal installation

Database name [drupal]:
 > drupaldb

 Database driver [mysql]:

 Database username [drupal]:
 > drupaluser

 Database password [drupal]:

 Database host []:

 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 site’s ip or FQDN.

Step 6: Drush usage example

You can run Drush in the shell by typing drush in the project root directory or anywhere in Drupal. Navigate to your root directory and execute the drush command

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

Drush Launcher Version: 0.6.0
Drush Commandline Tool 10.3.1

The Drush command follows 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

Environment overview-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      :                                             
 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 user

Drush provides a simpler and faster interface to create users in your Drupal site, as shown below:

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

Set a password for the new user created

Run the following command to set the password of the user account with the specified name.

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

Print information about specified users

In order to get detailed information about users in your environment, there is a simple command that can be run 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 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] (docs:readme)

As you can guess now, Drush has a rich ecosystem, a lot of commands and support from developers and contributors. This guide cannot cover all the details Druch can complete. For more information, please refer to Flush the usage page, its GitHub page as well as Assault order For complete documentation and references.

Concluding thoughts

Drupal is an open source CMS that can help you as a developer provide ambitious, elegant and efficient digital experiences on a large scale. You can take advantage of its features and combine it with Drush, and your work will become exceptionally good. Finally, we thank you for your visit and hope that this guide will be helpful to you. You can jump into 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

You can download this article in PDF format via the link below to support us.

Download the guide in PDF format

turn off