Drush is a great tool for quickly managing routine tasks with a Drupal installation.
Drush allows you to search the drupal.org site, install Drupal, install modules, enable modules and much more.
This is going to be a tutorial aimed at Drush beginners. We'll explain how to install Drush, how to use it and how to perform some common tasks.
Some Notes Before We Start
- In this tutorial, we're working with a Drupal site that is already installed. To follow this tutorial, have a Drupal site to practice on and please make sure it's one you don't mind breaking!
- This tutorial explains only of several ways to use Drush. Using PEAR is another recommended method and explained on the Drush download page.
- Even though Drush is listed as a module on drupal.org, it's not actually a module. It is something you will need to install on your server, not upload through Drupal's module management.
- If you are familiar with using shell access and the command line for doing things like copying, moving, backing up, and installing, you'll get the hang of Drush right away. If you're not familiar with either, don't worry if the concepts in this tutorial are confusing at first. With our help and some persistence, you will get the hang of it quickly.
- Yes, this tutorial is a simplification, perhaps even an over-simplification. This tutorial is called the Absolute Beginner's Guide to Drush for a reason. It's a simple introduction to a complex topic. Those expecting a great level of detail are advised to visit https://drupal.org/node/477684.
Step 1: Setting up SSH Access
To access the server directly from your computer you will need shell (SSH) access.
What is SSH? Technically, SSH allows you to control another computer (ie, your web server) over the Internet through a command line interface (CLI). SSH is actually included by default on many Mac and Linux computers.
You will probably need to get your hosting company to enable SSH for you. Most hosting companies won't have this turned on for you.
Step 2: Install PuTTy and Connect
There are many ways that you can connect to your site using SSH, but in this example we're going to use Putty.
- As soon as you enter your host name and click Open, you'll be taken to the command line screen. This will be a black screen with white text as in the image below.
- Putty will ask you for your login username and password. This will often be identical to your FTP login or hosting account login.
- You'll know that you logged in successfully if you get a message like the one below. That means that you're connected and ready to start work. If you get an Access Denied message, you'll need to try your details again.
Step 3: Download Drush
- Go to https://drupal.org/project/drush.
- Right-click on the download link for Drush. You want to copy the URL link. It will look like this: https://ftp.drupal.org/files/projects/drush-7.x-5.4.zip
- Paste this into Putty.
- That command will run and should tell you that it connected to Drupal.org and saved Drush:
Step 4: Unzip Drush
- Enter the command below and that will extract the zipped Drush file that's on your server.
Step 5: Change Drush's Permissions
- For security reasons, by default you will not have permission to use Drush. The following command will allow your system to use Drush:
- You can also do this via FTP. Find the file called Drush and set it's permissions to 744.
Step 6: Tell your System Where Drush is
- The following command will give your system the information it needs to access Drush.
- Be careful with the path. You are looking for the path to drush.php. If you have the wrong path, you'll get an Access Denied error. Try again with a new path.
Step 7: Flush the Cache
- The following command will reset your system that it is ready to use Drush:
Step 8: Check that Drush is Working
- Run this command to check if Drush is working:
- If Drush is working you will see a result like the one below. If Drush isn't working, go back and check the earlier steps.
Step 9: Browse to Your Drupal Files
- In the example below, our Drupal site is in the /shell/ folder. Adjust this command to navigate to the folder where you have Drupal installed:
- You will know you're not in the root any more because the next line will read:
Step 10: Download a Module
- Now let's test out our Drush install by trying to install a module. This command will download the Token module:
Step 11: Enable a Module
- Now let's try to enable that Token module:
More Common Drush Commands
Congratulations. If the download and enable commands work for you, then you're ready to use Drush.
We have seen several common Drush commands above. There is an entire list of the Drush commands at https://drush.org. Here are some of the most popular:
- drush dl Download and install a drupal module. This defaults to the sites/all/modules directory.
- drush en Enable a module
- drush dis Disable a module
- drush up Check for available updates, download updated modules, and run update.php
- drush up Check to see if the specific module needs updating, and if so, download it and run update.php
- drush sql-dump --result-file=db-backup.sql Dump the entire Drupal database into a file called db-backup.sql. In other words, backup your database.
- drush sql-cli < db-backup.sql Connect to the database server and run the commands in db-backup.sql. In other words, restore the database from db-backup.sql
- drush cc all Clear all caches
- drush vset preprocess_css 0 --yes Turn off CSS caching. This is useful when developing themes.
- drush cron Run cron
- drush vset site_offline 1 --yes Put a site into maintenance mode (D6 only)
- drush vset maintenance_mode 1 --yes Put a site into maintenance mode (D7 only)
- drush vset site_offline 0 --yes Take a site out of maintenance mode (D6 only)
- drush vset maintenance_mode 0 --yes Take a site out of maintenance mode (D7 only)