Drush is a command-line shell and scripting interface for Drupal. It provides a set of commands which act as shortcuts to perform common tasks with Drupal sites. The purpose of this book is to explain Drush capabilities through practical examples. This chapter starts by showing some advanced examples and then focuses on installation, usage, and some basic concepts. The following chapters cover the command toolkit, customization, and extension.
These are the major things we will be covering in this chapter:
How to install Drush in different operating systems
The general syntax of a Drush command
How to tell Drush which site we want it to work with
Imagine the following scenario: you have been told to clear the cache of a website called http://somewebsite.com. Normally, this would involve the following steps:
1. Open the login form of that website in a web browser.
2. Enter the username and password of a user with administrative rights.
3. Navigate through Administration | Configuration | Performance and hit the button labeled as Clear cache.
4. Log out.
If you had Drush installed on your system, you would just need to open a terminal and execute the following command:
$ drush @somewebsite.com cache-clear all
That's it. You did not even have to provide user credentials. This little demonstration shows the potential that Drush has to simplify processes. Once you have configured Drush, you can do even cooler things such as downloading a database from a remote site to your local environment, excluding cache tables, and automatically resetting user e-mails and passwords with a command such as the following one:
$ drush sql-sync @somewebsite.com @somewebsite.local
Drush is a great tool to automate and speed up common tasks involving Drupal sites. If you have ever felt that you are doing the same thing many times, keep on reading to discover more effective ways to manage your Drupal sites!
Imagine the following scenario: you have been told to clear the cache of a website called http://somewebsite.com. Normally, this would involve the following steps:
1. Open the login form of that website in a web browser.
2. Enter the username and password of a user with administrative rights.
3. Navigate through Administration | Configuration | Performance and hit the button labeled as Clear cache.
4. Log out.
If you had Drush installed on your system, you would just need to open a terminal and execute the following command:
$ drush @somewebsite.com cache-clear all
That's it. You did not even have to provide user credentials. This little demonstration shows the potential that Drush has to simplify processes. Once you have configured Drush, you can do even cooler things such as downloading a database from a remote site to your local environment, excluding cache tables, and automatically resetting user e-mails and passwords with a command such as the following one:
$ drush sql-sync @somewebsite.com @somewebsite.local
Drush is a great tool to automate and speed up common tasks involving Drupal sites. If you have ever felt that you are doing the same thing many times, keep on reading to discover more effective ways to manage your Drupal sites!
Drush can be installed manually or automatically, depending on your operating system and preferences. These are explained in this chapter through step by step instructions. You should evaluate them and, whichever you follow, make sure that you end up with Drush 4.5 or higher installed on your system.
Drush is designed for working on Unix-based systems such as Linux and Mac OS X. However, a lot of effort has been put into making some of its commands available in Windows systems.
Note
Windows users can install Ubuntu within VirtualBox to use Drush within a Unix-based environment. See http://www.ubuntu.com and https://www.virtualbox.org for more details.
Linux and Mac users need to verify that they can run PHP on the command line and its version is 5.2 or higher. If you are using Windows, you can jump to the next section as the Drush installer configures PHP automatically for your terminal. The easiest way to check whether you have PHP-CLI installed and configured, is by opening a terminal and executing the following command:
$ php -v
PHP 5.3.5-1ubuntu7.4 with Suhosin-Patch (cli) (built: Dec 13 2011 18:25:14)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
If you got an output similar as the previous one, you can jump to the next section. If not, here are some tips to get PHP-CLI installed on your system.
Debian and Ubuntu users can easily install php-cli
with the following command:
$ sudo apt-get install php5-cli
Once the installation completes, verify the installed version with php -v
.
Mac users have php-cli installed by default but need to make it available from anywhere in the terminal. The whereis
command helps to find where a command is located. The following command shows how the whereis
command is used to find where a command is located, if we have the PHP binary in our system:
$ whereis -b php
php: /usr/bin/php /usr/share/php /usr/share/man/man1/php.1.gz
If the command has found any results, as in the previous example, try executing them appending -v
to see if the version is 5.2 or higher. If you have found it, create a symbolic link that points to any of the paths listed within your PATH
environment variable. Now, in the future you can just type php
instead of the full path where it's located. We will first print the available paths and then create a symbolic link to one of them:
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin: /usr/games:/home/juampy/local/bin:/opt/android-sdk-linux_x86/tools
$ sudo ln -s /path/to/php/executable /usr/local/bin/php
After that, you should be able to run php -v
without errors.
Note
You can find further information about requirements at the Drush README.txt
(http://drupalcode.org/project/drush.git/blob/HEAD:/README.txt).
Drush can be installed in Linux or Unix systems, manually or automatically, by a package management system such as PEAR, Aptitude, Port, Homebrew, and others (depending on your distribution). Although the automatic approach offers a quick and easy way to install Drush, there are some disadvantages:
Some package management systems do not have Drush 4.5 available yet. For example, Ubuntu 11.04 installs Drush 4.4 through Aptitude.
Drush may not be available at the official channels and you may need to add a backports channel in order to discover it. For example, the PEAR installation requires that you first install PEAR and then add the channel where Drush can be found and installed. Moreover, PEAR has to be installed previously and the installation process varies depending if you are using Linux or Mac.
Some of them may not install everything in the right place. Aptitude installs Drush in Ubuntu without its documentation, so the
drush topic
command would not work.
After this reasoning, the most effective method is installing Drush manually, as it ensures that we are installing the correct release. Installing a previous version may result in some commands used in this book being made redundant, or unexpected results from other commands.
Note
For instructions on how to install Drush on shared hosting systems, read the contents of http://drupal.org/node/1181480.
1. In a web browser, open the Drush 4.5 release page at http://drupal.org/node/1247086.
2. Download the zip or tar.gz to your home path. You can check where your home path is, with the following command:
$ echo $HOME /home/juampy
3. Extract the contents of the compressed file. For
drush-7.x-4.5.tar.gz
use the following command:$ tar -zxvf drush-7.x-4.5.tar.gz drush/ drush/examples/ drush/examples/example.aliases.drushrc.php ...
Note
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
$ unzip drush-7.x-4.5.zip Archive: drush-7.x-4.5.zip creating: drush/ creating: drush/examples/ ...
4. Change directory to
drush
.$ cd /home/juampy/drush
5. Set permissions to the drush executable file in order to be able to run Drush with the following command:
$ chmod u+x drush
6. Now try to execute it by typing its name:
$ ./drush Execute a drush command. Run `drush help [command]` to view command-specific help. Run `drush topic` to read even more documentation.
You should see the Drush help as shown in the previous output.
7. Create a symbolic link of the Drush executable to any of the directories listed at your
PATH
environment variable; so you do not have to type/home/juampy/drush/drush
every time you use it. Note that this command will ask you for your administrator password in order to proceed:$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin: /usr/games:/home/juampy/local/bin: /opt/android-sdk-linux_x86/tools $ sudo ln -s /home/juampy/drush/drush /usr/local/bin/drush
8. Verify the installation by going to another directory and running
drush:
$ cd /home/juampy
$ drush
You should see an output similar to the one in step 6. Now you can start using Drush in your Drupal projects.
Drush 4 does not support Windows. However, there is an automatic installer for Drush 5. Although it does not have a stable release yet, it has all the capabilities of Drush 4.5. Hence, it is the preferred installation method in this case.
In case you experience problems using the installer, the manual installation process is explained as well in this section.
Note
Drush on Windows supports three shells: DOS, PowerShell, and msysgit (mingw). Cygwin is not formally supported, but is very similar to msysgit and should work.
Drush has a Windows installer that sets up everything you need to start using Drush. The process is very similar to installing any other software in Windows.
Here are the steps to get it working:
1. Download the installer and the Installation Guide from http://www.drush.org/drush_windows_installer.
2. Open the Installation Guide and follow instructions to install Drush.
3. Once the installation completes, you can open a Drush terminal from the Windows Start menu. A shortcut on your desktop has been set up as well.
4. Both shortcuts open a Windows console configured to work with Drush. You can execute Drush commands there by typing
drush
.5. If, when you enter
drush
and you get the error php.exe could not be found, run PHP manually and then try again, as in the following example:
C:\> php -v C:\> drush
You should see now the list of commands and options for Drush.
The alternative installation method requires knowledge of the Windows system variables configuration and administrative permissions to replace a Windows library. The result has Drush 5 installed and works as the automatic installer does.
Drush needs a few libraries to be installed in order to work correctly. Take into account the following tips prior to installing them:
Each installer can be found in their home pages at the first setup link of the Download section.
When you open an installer, you may see up to three security warnings, as shown in the following screenshot, before the installation starts. Accept them, as these libraries should not involve any risk for your system.
Now, we will replace the Windows Tar library (tar) with the one we just installed, by performing the following steps:
1. Open the file explorer and browse to the folder
C:\Program Files\GnuWin32\bin\
.2. Rename the file
tar
totar_default
. If Windows asks you to confirm this operation, accept it.3. Make a copy of the file
bsdtar
(located in the same directory) and rename the copy totar
. If you can see the file extensions, keep them.4. Your
bin
folder should look as shown in the following screenshot, after doing this.
In a web browser, open the Drush Project page and download the zip package of the All-versions-5.x-dev release: http://drupal.org/project/drush.
Extract the contents of the zip
file to the root of your C drive, so drush.bat
can be reached at C:\drush\drush.bat
.
Configure your Path
system variable by going to My Computer | Properties | Advanced System Settings | Environment Variables. At the System Variables list, double-click on Path and append this at the end of the Variable value field: ;C:\php;C:\drush;C:\Program Files\GnuWin32\bin.
As you can see, we are providing the Path variable with the location of the PHP, Drush, and libraries directories. Make sure that these paths are correct as in some installations they may vary (for example, WAMP installs PHP at C:\wamp\bin\php
and some Windows versions have Program Files (x86) instead of Program Files). Save the value and close all the windows you opened for doing this.
Open the command-line interface at Start | Programs | Accessories | Command Prompt. If you had it already opened, then close and open again so it loads the new configuration. Once in, type the following:
C:\> drush core-status
PHP configuration : C:\php\php.ini Drush version : 5.0-dev Drush configuration : Drush alias files :
If you did not get the previously shown output, try to identify the error with the message reported or find more debugging information at http://drupal.org/node/594744.
If you use the default php.ini
configuration file, you may encounter unexpected errors using Drush such as timeouts, errors not being shown, or functions not being found. This is because the default configuration of PHP is too restrictive. Therefore, we will first identify where it is and if is not shared with the web server, configure it. If it is, an alternative method to override some of its settings will be explained.
You can easily find the php.ini
file being used by Drush with the core-status
command:
$ drush core-status PHP
PHP configuration : /etc/php5/cli/php.ini
Now, in order to find out if the web server uses this file, create a file called info.php
in a directory visible by your web server:
<?php phpinfo():
Now open it in a web browser and look for the line Loaded Configuration File:
If the file path is different from the one that drush core-status PHP
reported, you are safe to go ahead configuring it. If not, create a php.ini
file within the Drush installation directory (for example, at /home/juampy/drush/php.ini)
with the following contents:
memory_limit = 128M error_reporting = E_ALL | E_NOTICE | E_STRICT display_errors = stderr safe_mode = open_basedir = disable_functions = disable_classes =
These settings make sure that Drush has enough memory to run; errors are printed onscreen and some PHP variables do not restrict it.
Drush ships with a set of grouped commands to perform different tasks. If you are fluent at executing commands in the terminal, you can skip this section and start exploring the details of each Drush command in the next chapter. If not, you should understand what arguments and options are to a command and how they affect its behavior.
Let's start with a very simple command such as core-status
, which prints configuration information about Drush and, if applicable, a Drupal site. If executed at the root of a Drupal directory with its database configured at sites/default/settings.php
, the following command would return:
$drush core-status
Drupal version : 7.4
Site URI : http://drupal7.localhost
Database driver : mysql
Database hostname : localhost
Database username : root
Database name : drupal7
Database : Connected
Drupal bootstrap : Successful
Drupal user : Anonymous
Default theme : bartik
Administration theme : seven
PHP configuration : /etc/php5/apache2/php.ini
Drush version : 4.5
Drush configuration :
Drush alias files :
Drupal root : /home/juampy/myDrupalSite
Site path : sites/default
File directory path : sites/default/files
This output is informing us the main configuration of our site and Drush, which is its default behavior. Now, we can print its help information to find out that it can actually do more than that:
$ drush help core-status
Provides a birds-eye view of the current Drupal installation, if any.
Examples: drush status version Show all status lines that
contain version information.
drush status --pipe A list key=value items
separatedby line breaks.
drush status drush-version--pipe Emit just the drush version
with no label.
Arguments: item Optional. The status item
line(s) to display.
Options: --show-passwords Show database password.
Topics:
docs-readme README.txt
Aliases: status, st
As we can see, the core-status
command accepts arguments and options when being called. We will now see how to use them.
An argument is a piece of information that acts as input data for a command. They are typed next to the command name and separated by spaces.
The help information of the core-status
command (type drush help core-status
to see it again) says that we can specify the items which we want it to print. Therefore, if we need to print just the items containing version
in the item name, we could do the following:
$ drush core-status version Drupal version : 7.10
Drush version : 4.5
You can try and change version
by something else or even add more parameters after it, so the command will print items containing them as well. If we wanted version and database information to be printed, the following command would do it:
$ drush core-status version database
Drupal version : 7.10
Database driver : mysql
Database hostname : localhost
Database username : root
Database name : drupal7db
Database : Connected
Drush version : 4.5
You can give any number of arguments to a command. Beware that some commands expect the arguments to be given in a certain order. For example, the command variable-set
expects that the variable name to be set is the first argument and its new value is the second argument.
Hence, the following example sets the variable site-name
with the value My Drupal site:
$ drush variable-set site_name "My Drupal site"
Drush commands accept options, which modify their default behavior. If, for example, we wanted to list the database connection details of a Drupal site, we would do the following:
$ cd /path/to/drupal/root/
$ drush core-status --show-passwords database
Database driver : mysql
Database hostname : localhost
Database username : root
Database name : drupal7site
Database password : drupal7sitePassword
Database : Connected
The option --show-passwords
is telling Drush that we want to see the database password of the site where we currently are. This option is needed because, by default, the status
command does not show database passwords.
Here is a full command that prints version and database information with database passwords in a key=value format. Its full syntax is detailed as follows:
$ drush core-status --show-passwords --pipe version database
command options arguments
As you can see, options are given after the command name and arguments are given at the end. You can actually change the order and even mix them, but for clarity we will follow the given structure.
In order to read the description and available arguments and options for a command, use drush help
and append to it the command name as an argument, such as:
$ drush help core-status
In the previous example, core-status
is not a command but an argument for the help command telling it that we want to see help information about the core-status
command.
Most of the Drush options have a short and long format and they may accept a value too. You can see if an option has a short format in the command help. As an example, if we wanted to tell a Drush command the URL of our site, we could do it in two ways. Here is the short one:
$ drush cache-clear -l drupal7.localhost all
And this is the long one:
$ drush cache-clear --uri=drupal7.localhost all
In this book, we will use the long format as it makes clearer the difference between option values from arguments.
There are some options which are applicable to most Drush commands. You can see a list of these using the following command:
$ drush topic core-global-options
Most of the Drush commands have a shorter alias to help us type less. You can see them between parenthesis next to each command name. Therefore, the following command $ drush status
is a shortcut for $ drush core-status
.
For clarity, we will not use command aliases in this book, but you should learn and use them. Here is an example showing a portion of the help information of the core-status
command where its aliases are listed in parenthesis.
$ drush help core-status
... core-status (status, st) Provides a birds-eye view of the current Drupal
installation, if any
...
This means that the three following commands give the same result:
$ drush core-status
$ drush status
$ drush st
Some Drush commands are to be executed on a non-Drupal directory (for example, site-install)
, others behave differently when executed on a Drupal directory (such as pm-download)
, and the remaining ones must be called within or referencing a Drupal directory (sql-connect
). Here, we will explain how to tell Drush explicitly or implicitly that we want it to work with a Drupal site in particular.
Drush gathers information from arguments, options, and configuration files, creating a context.
The recommended method for working with Drush, against a Drupal site, is to run all commands from the root path of the site (where the index.php
file is). This means that if, for example, a Drupal site is installed at /var/www/drupal7/
and its settings file is located at /var/www/drupal7/sites/default/settings.php
, you could clear the cache by doing the following:
$ cd /var/www/drupal7/ $
drush cache-clear
The previous command will work correctly because Drush realizes that we are at the root of a Drupal directory and will find a database configuration at sites/default/settings.php
. However, if we were using the multi-site feature of Drupal, settings.php
would not be at sites/default
, but at something such as sites/drupal7.localhost
. In this scenario, we can still run commands easily by placing ourselves at the same level of the settings.php
. This means the following commands:
$ cd /var/www/drupal7/sites/drupal7.localhost $ drush cache-clear
This command works because Drush finds a settings.php
file right where we are. If we would have tried to clear the cache from the root path (/var/www/drupal7/) as in the previous example, Drush would have printed an error:
$ cd /var/www/drupal7 $ drush cache-clear
The drush command 'cache-clear' could not be executed. [error] Could not find a Drupal settings.php file at ./sites/default/settings.php. [error]
This command fails because Drush looks for /var/www/drupal7/sites/default/settings.php
with no success. It does not have a database to clear its cache, and hence fails. To overcome this, we can make use of explicit methods to help Drush find our site, as explained in the next section.
Note
If you do not know how the Drupal multi-site feature works, read about it at http://drupal.org/documentation/install/multi-site.
By informing Drush where the root path is and the site name within sites subdirectory, we can execute commands from any directory. For example, if our Drupal root path is located at /var/www/drupal7
and the settings.php
is in a different directory than sites/default
(which happens on multi-site installations), then we could invoke the command from the Drupal root path specifying the site name as an option:
$ cd /var/www/drupal7 $ drush cache-clear --uri=drupal7.localhost
We can even go one step further and clear the cache of our Drupal site without even being at the root path by running the following commands:
$ cd /home/juampy $ drush cache-clear --root=/var/www/drupal7 --uri=drupal7.localhost
This command is saying "Hey Drush, I want you to clear the cache of a Drupal site which has its root path at /var/www/drupal7
and its settings.php
at /var /www/drupal7/sites/drupal7.localhost"
.
Whenever you want to check the current active context, you can make use of the core-status
command. See the following example, where it is executed in a non-Drupal directory:
$ cd /home/juampy $ drush core-status
PHP configuration : /etc/php5/apache2/php.ini Drush version : 4.5 Drush configuration : Drush alias files :
You can see that Drush just prints its version info and the location of the loaded php.ini
configuration file. Now, we are going to run drush core-status
from the root of a Drupal directory with its settings.php
file located at sites/default:
$ cd /home/juampy/myDrupalSite
$ drush core-status
Drupal version : 7.4
Site URI : http://drupal7.localhost
Database driver : mysql
Database hostname : localhost
Database username : root
Database name : drupal7
Database : Connected
Drupal bootstrap : Successful
Drupal user : Anonymous
Default theme : bartik
Administration theme : seven
PHP configuration : /etc/php5/apache2/php.ini
Drush version : 4.5
Drush configuration :
Drush alias files :
Drupal root : /home/juampy/myDrupalSite Site
path : sites/default
File directory path : sites/default/files
As you can see in the previous example, Drush discovers the site and prints its configuration details.
Drush incorporates a killer feature to provide all the information needed to run a command against a Drupal site from any directory with just one parameter (for example, drush @mysite core-status)
. It is called Drush site alias and it is explained in Chapter 3, Customizing Drush. There is another way of doing this which is with Drush configuration files, which will be explained as well.
After reading this chapter, you are ready to blast off. You have installed Drush and its software requirements and know how to do it in other operating systems. You tested some commands and understood how to use options and arguments with them. Finally, you found out that you can move around directories and still execute commands towards a particular Drupal site.
In the next chapter, you will discover what each Drush command can do with a practical example.