How to install Drush — the Drupal shell

Drush is an amazing tool that makes the mundane tasks of managing Drupal websites to a pleasure. Drush is a must-have tool for serious Drupal developers and Drupal maintainers.

In this blog post, I will show you how to install it on Debian based GNU/Linux distributions such as Ubuntu.

Requirements

Drush requires PHP 5.2. That mens Etch or later for Debian, and Hardy or later for Ubuntu.

As of version 2.0, Drush is a stand-alone tool. It therefore requires the command line interface (CLI) of PHP. If not already available, you can install it with following command:

sudo apt-get install php5-cli

With the requirements taken care, we can now move on to the installation itself.

Installation

Download Drush and unpack it. I prefer to put software packages that I install manually in /opt. With my wtar alias the downloading and unpacking is done in a single step:

cd /opt sudo wtar http://ftp.drupal.org/files/projects/drush-All-Versions-2.0.tar.gz

Put Drush on the path by making a symbolic link from /usr/local/bin/drush to /opt/drush/drush:

sudo ln -s /opt/drush/drush /usr/local/bin/

First time Drush runs, it downloads and install PEAR's Console_Table. With Drush installed as above, this must be done by root. Let us therefore invoke it the first time with sudo:

sudo drush

Drush is now installed and ready to use.

Configuration

Drush requires no configuration. It works perfect out-of-box. But you should at least take a peek at the example configuration file, example.drushrc.php, to get a picture of what settings are available.

Drush looks for configuration files in several places. Settings in later found files override those in earlier. In order, the places where Drush looks are:

  • Drush installation directory,
  • your home directory (as .drushrc.php),
  • root directory of a Drupal installation, and
  • site directory of a Drupal installation.

Since the configuration files are just PHP scripts, they can contain any PHP code that you would like to have executed on startup of Drush. You could for instance call ini_set() to set the value of a PHP configuration option.

I have the configuration file /opt/drush/drushrc.php with following content:

<?php # Allow Drush to use 128 MB of memory. Use -1 for no limit. ini_set('memory_limit', '128M'); ?>

This will give Drush 128 MB of memory to use. It's necessary because PHP default value is less than Drupal's need. You could of course change the memory_limit directive in /etc/php/cli/php.ini instead. But since that would effect all PHP scrips, I prefer to leave it alone.

In the root directory of every Drupal installation I also have a drushrc.php with content similar to this:

<?php // Drupal root directory to use. $options['r'] = '/srv/001/web/www'; // URI of the drupal site to use. $options['l'] = 'http://example.com/'; ?>

The first line, $options['r'] = ..., tells Drush that the path to Drupal root directory. Usually, this is not necessary. Drush can derive the path from your current working directory. But without this line, symbolic links can confuse Drush.

The second line, $options['l'] = ..., tells Drush the URL of the site. This is not necessary if Drush can find the site's settings.php in sites/default. But I prefer to have the settings in a directory named after the sites domain, e.g. sites/example.com, and therefore need to tell Drush which site to work with. This is also true if you have a multisite installation.

Conclusion

Drush is almost trivial to install. Go ahead, install Drush and play around with it.

I will blog more about Drush. Next up is a blog post about how to use Drush to install a complete Drupal site.