Switching Drush Versions

Different versions of Drupal require different versions of Drush, here’s how to make the switch easily

Drush is great! I can’t manage Drupal without it. But now that Drupal 8 is nearing release I’ve run into a big problem. Drupal 8 requires the bleeding edge version of Drush, but that version of Drush won’t work with older Drupal sites. In particular, I was running into problems trying to switch between a Drupal 6 site and the Drupal 8 site I’m trying to migrate it into. Drupal 6 works with nothing later than Drush version 5, but Drupal 8 requires a minimum of Drush version 8! And in the meantime I’m still working on several Drupal 7 sites which have Drush scripts that only work with Drush version 6 or 7. What I needed was an easy way to switch versions of Drush for the task at hand.

I combed the web for instructions on how to switch Drush versions on a Mac and didn’t find what I needed. But I did find several articles that had parts of the answer. So I stitched things together and came up with the following system based on Composer.

1) Install Composer

Composer is the recommended method of installing Drush these days, certainly for the bleeding edge version. I’ll need composer to work with Drupal 8, so this makes sense anyway. It’s pretty easy to install following the instructions at https://getcomposer.org/doc/00-intro.md#globally.

I previously had Drush installed with homebrew and wanted to get rid of that installation, so I had to do this:

brew remove --force drush

Then I installed a default version of Drush, Drush version 7, globally:

composer global require drush/drush:7.*

2) Pick a Location

I decided to go whole hog and create a way to switch between every version I might need, Drush 5, 6, 7, or 8, by creating directories for each of these. I could do this anywhere, but the most logical place seemed to be in my user directory.

3) Install Drush 8

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

4) Install Drush 7

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

5) Install Drush 6

mkdir ~/drush6
cd ~/drush6
composer require "drush/drush:6.*"

6) Install Drush 5

cd ~
wget "https:// github. com/drush-ops/drush/archive/5.10.0.zip"
unzip 5.10.0.zip
sudo mv drush-5.10.0 drush5

7) Alias The Directories

To make them switchable I created an alias for each. In bash.profile I added the following:

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

Since I installed Drush version 7 globally, anytime I type “drush” without a version modifier, it will default to using Drush 7. Because of that I could have skipped the installation of the Drush7 version above, but I decided I liked the idea of having both a global default (that I might change later) and a definite way to invoke version 7 that will work without knowing or caring what the global default is.

8) Test The Aliases

To test the finished system, I made sure the aliases work as designed. I opened a new terminal window (so it picks up the changes in the bash profile) and typed:

drush5 --version
drush6 --version
drush7 --version
drush8 --version

From this point on any time I need to run a drush script that uses a particular version of drush I just need to use my new aliases to do so:

drush5 status
drush6 cc all
drush7 sql-sync

9) Profit

That’s it. Now I know I can control the drush version by adjusting my commands to invoke the right version.

The following articles provided fodder for this solution:

Get in touch with us

Tell us about your project or drop us a line. We'd love to hear from you!