An Introduction to Drush and the Drupal Console

Install Drupal 8 and Write a Module

Drush and the Drupal Console are both command line tools that allow you to greatly speed up administration and development tasks for Drupal websites. After installing these tools, you will be able to perform actions simply by typing commands into a terminal window—actions that would usually take multiple steps using a web browser, or perhaps might ordinarily require that you write some code. Drupal Console only works with Drupal 8, whereas Drush runs on Drupal 6, 7 and 8. If you are starting to work with Drupal 8, then you should use both!

Drush & Drupal Console Capabilities

What can you do with these tools? A brief, partial list of capabilities is shown below:

Drush Features

Drupal Console Features

  • Download Drupal
  • Download contrib modules
  • Install Drupal
  • Update Drupal and contrib module versions
  • Run updatedb
  • Clear the cache
  • Run cron
  • Run Drupal with a lightweight web server
  • Import, export and merge configuration
  • Add users and set their roles
  • Add permissions to roles
  • Back up and restore Drupal
  • Copy your database and files to a remote server
  • Compile twig templates
  • Generate code for a:
    • Console command
    • Content type
    • Controller
    • Entity
    • Form alter hook
    • Module
    • Field type, widget and formatter
    • Image effect
    • Rest resource
    • Service
    • Theme
  • Switch maintenance mode on or off
  • Run unit tests

Intrigued, and ready to get started? The first step is to open up a terminal window. Linux users should be no stranger to the command line, but if you use MacOS or Windows, you might not have used this very much. See the table below for instructions on how to do this for your platform.

  MacOS   Windows

Click on the Unity icon and type “terminal”. Double-click on the Terminal icon.


Click on the “LaunchPad” and type “terminal” into the search field. Double-click on the Terminal icon.


Press the START (Windows) key and type “git bash”. Select the Git bash icon shown above to launch the terminal.

The terminal is pre-installed on Linux and MacOS, but you will probably need to install it if you are using Windows. If you do not see the “Git Bash” item shown above on your system, download and install the Git for Windows package from Google. This is the best terminal available for that platform; other options such as PowerShell and the built-in “Run Programs” terminal (the DOS shell) work differently, and are likely to cause problems.

Once you have your terminal window open, you will need to become familiar with a few shell commands. If you are new to the command line, try ls to list the files in the current directory, cd to change directory, and pwd to show which directory you are currently in (print working directory). Reading a tutorial on the shell, such as An introduction to Bash, by Ray Toal, will help you get started. The command line might feel awkward at first, but as you get used to it, it will really help to speed up your tasks.

[Related] Applying Updates to Drupal Core on Patheon with Drush

Working with Drush and the Drupal Console

(This assumes that you have already installed enough additional packages to do local Drupal development; if you have not already installed sqlite, you might want to install it first.) Drush and Drupal Console are very similar, but they are packaged separately, and will need to be individually installed. You should follow the online instructions to install Drush with Composer, and then go on to install Drupal Console.

Next, check and see if they installed correctly. In the examples that follow, type the green text (drush version and drupal --version) into your terminal window, and confirm that you see output similar to what is shown below. The version numbers might be slightly different on your system.

Drush Drupal Console
drush version
 Drush Version   :  8.0-dev 
drupal --version
Drupal Console version 0.9.1

Now, let’s show the power of Drush and the Drupal Console working together! First, we will use Drush to download, install and run Drupal 8, using pm-download (dl) and quick-drupal (qd) commands:

Install and Run Drupal with Drush

drush dl drupal-8.0.x
Project drupal (8.0.x-dev) downloaded to /Users/ga/tmp/intro-drush/drupal-8.0.x-dev.
Project drupal contains:
 - 1 profile: standard
 - 13 themes: …
 - 62 modules: …

cd drupal-8.0.x
$ drush qd --use-existing --uri=http://localhost:8383 --profile=standard
You are about to DROP all tables in your ‘…/drupal-8.0.x-dev.sqlite’ database. Do you want to continue? (y/n): y
Starting Drupal installation. This takes a while. Consider using the --notify global option.
Installation complete.  User name: admin  User password: …
Congratulations, you installed Drupal!
Caching 'uri' localhost:8383 in …/drush/drushrc.php
HTTP server listening on localhost, port 8383 (see http://localhost:8383/), serving site localhost:8383, logged in as admin...
PHP 5.6.12 Development Server started at Thu Sep 24 15:02:53 2015
Listening on http://localhost:8383
Document root is …/drupal-8.0.x-dev
Press Ctrl-C to quit.

After the output above has printed out, there should be a brief pause, and then you should see your web browser launch, and open a page displaying the Drupal site that you just installed. That was fast and easy!

Next, we’ll use Drupal Console to create a Drupal 8 module for us. Press CONTROL-C in your terminal window, to stop the web server. Then, run the commands shown in the example below:

Create a module with Drupal Console

drupal generate:module

 Welcome to the Drupal module generator 

Enter the new module name: hello
Enter the module machine name [hello]: hello 
Enter the module Path [/modules/custom]: /modules/custom
Enter module description [My Awesome Module]: Hello World
Enter package name [Other]: Other
Enter Drupal Core version [8.x]: 8.x
Do you want to add a composer.json file to your module [no]? yes
Would you like to add module dependencies [no]? no
Do you confirm generation [yes]? yes

 Generated or updated files 

Site path: /drupal-8.0.x-dev
1 - modules/custom/bello/
2 - modules/custom/bello/bello.module

Did you think you knew how to write a Drupal 8 module? Whether or not you thought you did, with the help of Drupal Console, you just wrote one. That’s pretty cool, but the module is missing its controller. A Drupal 8 module without a controller is like a Drupal 7 module without a hook_menu, so let’s go ahead and add one, so our module will do something. We’ll use Drupal Console again to do this:

Add a controller to our module with Drupal Console

drupal generate:controller

 Welcome to the Drupal Controller generator 

Enter the module name: hello
Enter the Controller class name [DefaultController]: HelloController
Controller title: Hello World
Enter the action method name [index]: greet
Enter the route path [hello/greet/{param_1}/{param_2}]: hello/greet/{world}
Controller title (empty to start with code generation): 
Do you want to generate a unit test class [yes]? yes
Do you want to load services from the container [no]? no 
Do you confirm generation [yes]? yes

 Generated or updated files 

Site path: …/drupal-8.0.x-dev
1 - modules/custom/hello/src/Controller/HelloController.php
2 - modules/custom/hello/hello.routing.yml
3 - modules/custom/hello/Tests/Controller/HelloControllerTest.php

[+] Rebuilding routes, wait a moment please
[+] Done rebuilding route(s).

So far so good. What did that generate for us?

Show the files generated by Drupal Console using the Tree command
tree modules/custom/
└── hello
    ├── Tests
    │   └── Controller
    │       └── HelloControllerTest.php
    ├── composer.json
    ├── hello.module
    ├── hello.routing.yml
    └── src
        └── Controller
            └── HelloController.php

Open up the file HelloController.php in your favorite editor, and replace the provided implementation with the green text shown below:

Modify the generated code to provide our own message
class HelloController extends ControllerBase {
   * Greet.
   * @return string
   *   Return Hello string.
  public function greet($world) {
    return [
        '#type' => 'markup',
        '#markup' => $this->t('Hello, @world!', array('@world' => $world))

That looks like a proper “Hello World” program, so let’s try it out. First, though, we must enable the module.  We can do this with Drush as well:

Enable the “hello” module with Drush
drush en hello
The following extensions will be enabled: hello
Do you really want to continue? (y/n): y
hello was enabled successfully.

We stopped the webserver above in order to run Drupal Console, so let’s fire it up again, this time using the runserver (rs) command. This will log you in again, and open up a new web browser pane to the page you specify on the command line:

Start up the webserver with Drush
drush rs /hello/greet/world
HTTP server listening on localhost, port 8383 (see http://localhost:8383/hello/greet/world), serving site localhost:8383, logged in as admin...
PHP 5.6.12 Development Server started at Thu Sep 24 22:29:24 2015
Listening on http://localhost:8383
Document root is …/drupal-8.0.x-dev
Press Ctrl-C to quit.

This should display the page you created in your new module in your web browser. Success! This Drupal 8 stuff isn’t so hard after all, when using the right tools.

Start Your Drupal 8 Projects on Pantheon Today

For years you’ve seen the platform designed for Drupal, now see the Drupal designed for the platform.