
Drush Made Simple
The word Drush is a combination of two words: Drupal + Shell. A shell is a command line tool that lets us manage our website by writing commands.
Why should you use it?
Drush is a Drupal developer's best friend, it makes your life easier.
Instead of hovering around the interface, Drush is an excellent alternative tool for doing various tasks with Drupal using commands. Save yourself from all those page clicks and more!
Here are some of the main reasons why you should use Drush:
- Drush is faster to use. Using the interface you need to load pages, click on menus, look for the information. With Drush, you look for a command using Drush help, and that’s it.
- Drush is faster for PHP to execute. No need to load the website interface, it goes straight to PHP, no browser needed.
- You can automate things. With Drush, you can make a script to enable ten modules in a row, apply some updates, clear the cache … etc. Any command from Drush can be made as part of a script.
- Avoid mistakes. A wrong click? Not possible with Drush.
Still not convinced?
A quick example of a task is downloading and enabling a Drupal module.
Without Drush:
- Download the module in drupal.org,
- Go to your website in home > admin > module, click choose file,
Choose the downloaded module from the ‘Downloads’ folder in your computer’ and then click install.
With Drush: you just have to write two lines of code in the command line tool to do this task.
$ drush pm-download modulename
In the command, "pm-download" can be shortened to "dl".
$ drush pm-enable modulename
Note that "modulename" is referring to the module’s machine name, as in the module’s URL. Example: the machine name of the “Chaos tool suite” module is ctools, it’s URL is https://www.drupal.org/project/ctools.
In the command, “pm-enable” can be shortened to “en”.
Two commands and some minutes saved!
Drush is really popular and stable. As of July 6, 2015, the Drush project had already been downloaded 1,531,560 times.
Install Drush
Install Drush using Composer:
The recommended way to install Drush is to use Composer because it will be easier to keep Drush up to date. Composer will manage Drush as a dependency.
Also, installing with Composer will be the only option moving forward, as noted on Drush’s website: "Note: Drush 9 (coming soon!) only supports one install method. It requires that your Drupal 8 site be built with Composer and Drush be listed as a dependency."
If your project is not using Composer:
$ drush pm-enable modulename
If your project is already using composer:
$ drush pm-enable modulename
Then install the Drush launcher.
To test if you had successfully downloaded and installed Drush, open your terminal, type Drush help and hit enter. You should see a long list of Drush commands along with descriptions of what each of the commands does. You can shorten the list of Drush commands and target specific result with the help of “grep” and keywords.
For example, if you are looking for the command to enable modules, you can use “enable” as your keyword:
$ drush pm-enable modulename
What about Drupal Console?
A tool became popular with Drupal 8 and it’s called Drupal Console. It can do a lot of things in a similar way as Drush, but it has a different focus.
Drush has been around for a very long time and has a lot of options available. Drush aims at helping the site builder into regular maintenance or administration tasks. On the other hand, Drupal Console is more modern and came with the version 8 of Drupal (if you know Symfony, you won’t be lost with Drupal Console).
Drupal Console focuses more on helping the Developers. You can do some administration tasks like enabling a module, but not as many as Drush. But Drupal Console can, for example, generate PHP code for you. This is more helpful for Developers than for maintenance tasks. Both can work together, so you can always install Drupal Console as well. But this will be another article. 🙂
Want to learn more about Drupal? Click this link to head to our training schedule.