Drush for the new and old-ish

A 3 minute read written by Alfred December 15, 2015

Drush for the new and old-ish

Drush for the new and the old, ish.

So for those of you who have no idea what drush is, I shall explain. If you know what $~ drush cc is, then some of this you might already know.

Drush is a combination of Drupal and Shell - creating the word DRUSH. Drush is a command line tool that ships with an abundance of commands that interact with code and can clear the site cache or migrate a database. Drush can also be extended by 3rd party command files.

Today we’re installing different versions of drush. Drush is available for all versions of drupal from 5-8. Unfortunately, each release of drupal requires different releases of drush, but this can be solved. Here’s how.

First step is to remove any installs of drush you might have. I used Homebrew to install drush for drupal 7 sites - so I used homebrew to remove it.

~$ brew remove --force drush

Try typing drush and you should get an error. Still with me? Good!

In my opinion, using Composer is the easiest and fastest way to get drush installed and available in the terminal.

Now open a new terminal tab and install Composer by running these two commands from the root of the user directory. ** You can skip this if you have Composer installed already. Just run ~$ composer global update to make sure you’re up to date.

~$ composer global require drush/drush:7.*

** Might have to run with sudo/ admin permissions

~$ curl -sS https://getcomposer.org/installer | php
~$ mv composer.phar /usr/local/bin/composer

Now add Composer’s directory to your path by editing your .bash_profile

export PATH="$HOME/.composer/vendor/bin:$PATH"

Open a new terminal tab to load in the new composer configuration. Now decide where to install the drush versions. I went with my user directory on my mac to install the versions. I chose to install drush7 and drush8 since it’s all that I will use. You can follow the same steps for 5 & 6 if you require these versions as well. From the terminal and in the user directory: ** You might have to run some of these terminal commands with sudo in front or as an administrator.

Drupal 8

~$ mkdir ~/drush8
~$ cd ~/drush8
~$ composer require drush/drush:dev-master

Drupal 7

~$ mkdir ~/drush7
~$ cd ~/drush7
~$ composer require drush/drush:7.*

** If you choose to install drush 7 globally, you don’t have to type ~$ drush7 every time. You can just type ~$ drush, run this command, and skip the next step. But of course, now you just type drush to use the default drush 7 version.

~$ composer global require drush/drush:7.*

** Skip if installed drush7 globally.

Now to get things rolling. We have to make these directories accessible to us and easy to use from the terminal. We’re going to create an alias for these directories in our .bash_profile, the same .bash_profile from a few steps earlier.

alias drush7='~/drush7/vendor/bin/drush'
alias drush8='~/drush8/vendor/bin/drush'

And that’s it! You should be all done (as long as there were no big red errors and everything installed properly). Now test it out and see if it worked. You can test the versions by opening a new tab in the terminal and type:

~$ drush7 --version
~$ drush8 --version

These should all return the proper version numbers for each install.

~$ drush7 --version

Drush Version : 7.1.0

~$ drush8 --version

Drush Version : 8.1-dev

Now from here on out you have to use the proper version of drush for the particular version of drupal. Ex. A drupal 7 site uses drush7, and a drupal 8 site uses drush8.

For further testing you can go into the root of your drupal 7 site in the terminal and type

~$ drush cc all

and the return should be

'all' cache was cleared

For drupal 8 the command would be

~$ drush8 cr

and the return should be

Cache rebuild complete

That’s all folks. Now we can control the version of drush that we use with the version of drupal and put all of those useful commands to use!