Our Blog

Drush – the command line tool for Drupal sites

Kalin Chernev
by Kalin Chernev on Mon 20 May 2013 No comments

What is the first word you come up in mind when you hear about Drupal?

Configuration options? if you are a site builder, for sure.

Excellent API? Most probably, if you are a developer.

Command line? This is for anyone who doesn’t like using the mouse, but rather typing commands in the terminal to directly instruct the program what to do 🙂

The command line is fun – it’s straightforward, it’s fast, and it can automate many tasks you do on everyday basis. Especially when one has to work on a server remotely, or when a website should be maintained without interface options. And this is the key of finding the reason of using the command line, and with Drush tool for Drupal – you can work with the software without being dependent on the interface, at least to the most part.

If you don’t know what a command line is, you can quick-jump in following this article: The Command Line is Your Best Friend.

So, let’s go and look at some examples of using Drush in our projects.

Installing Drush

Drush can be set up and used on any operating system, and it works best on Linux/Unix/Mac operating systems.

Installing Drush on Linux

If you don’t have pear installed:
shell > sudo apt-get install php-pear

Installing Drush
shell > pear channel-discover pear.drush.org
shell > pear install drush/drush
shell > drush version

Upgrading Drush version
shell > pear upgrade drush/drush

Installing Drush on Ubuntu

If you don’t have pear installed:
shell > sudo apt-get install php-pear

Installing Drush
shell > sudo apt-get install drush
shell > sudo drush dl drush --destination='/usr/share‘
shell > drush --version

Upgrading Drush version
shell > drush up

Installing Drush on Windows

Use the following installation guide (yep, there is a Windows Installer too 🙂

Setting up Drush aliases

Aliases simplify the process of specifying which working website we target in Drush.

Instead of this command:
drush username@myserver.com/path/to/drupal#mysite.com status

We can get the same result with a setup alias, with the following command:
drush @dev status

Create a directory called .drush in your home directory, if it doesn’t exist already. Add the drush aliases file is this directory.
shell > mkdir ~/.drush
shell > vi ~/.drush/aliases.drushrc.php

Add an entry for your development website, i.e. “dev”:

<?php
$aliases['dev'] = array(
'uri' => 'dev.loc', // Replace with the actual uri of your local Drupal site installation
'root' => '/www/sites/drupal/dev', // This is the Drupal root directory.
);
?>

That’s it! Now, if I want to run a drush command on your local dev.loc website, you can type the following command through any folder of your computer:
shell > drush @dev status

You can also setup to work with remove websites and groups of aliases. More information can be found at Drush setup example file at drush.ws.

Common Drush commands

Checking currently installed version of Drush
shell > drush version

Checking the website status
shell > drush status

Listing installed projects to update
shell > drush @alias pm-update projects
shell > drush pm-update projects

Running update.php
shell > drush updatedb

Rebuilding the registry (useful when moving modules around)
shell > drush rr
Note: registry rebuild helper files should be preliminary installed at Drush

Clearing the cache
shell > drush cc all

Modules management
shell > drush pm-list
shell > drush pm-download
shell > drush pm-enable
shell > drush pm-disable
shell > drush pm-uninstall

Useful resources

Some further resources you can check upon your learning of Drush are:

And 2 resources on using the command line

Wrap up

Hopefully, this article has been of useful for you. Using Drush can automate a lot of the everyday tasks we do, which take more time using the interface. Drush can also help local development to staging/product synchronizations.

Kalin ChernevDrush – the command line tool for Drupal sites

Join the conversation