Installing the latest version of Drush

Mon, 08/12/2014 - 23:21 -- James Oakley

In case you missed it, Drush has evolved recently.

Quick primer for beginners follows. (Although, if you haven't heard of Drush, the chances are this post was not written with you in mind. I blog about many subjects, and there aren't many readers who are interested in all of them!)

Drush stands for Drupal shell - it's a very powerful shell environment for managing Drupal sites using the command-line shell.

Drush used to have its main page on the Drupal website, and bugs and feature-requests were handled via the issue queue there. The easiest way to install it (IMO) was using PEAR.

Drush is now hosted on Github, where it has its own issue queue. and is dependent on a PHP package manager called Composer. In my opinion, it is now best installed by checking out the code directly from Github, and then using Composer to install it.

The documentation for this is well written. It works. Here's the pertinent bit for this post:

curl -sS | php
mv composer.phar /usr/local/bin/composer
ln -s /usr/local/bin/composer /usr/bin/composer

cd /usr/local/src
git clone /usr/local/src/drush
cd /usr/local/src/drush
git checkout 7.0.0-alpha5  #or whatever version you want.
ln -s /usr/local/src/drush/drush /usr/bin/drush
composer install
drush --version

There's one problem with that, and it's the bit I highlighted in bold type.

I'm the sort of person who likes to document code that I invoke, so that I can call it again when I need to. Sometimes, I even save them in a BASH script ready to run again when I need to.

But I don't want to save the code with the precise release tag hardwired into the code. Here's the case in point: At the time of writing, Drush 7 is on 7.0.0-alpha7, so the documentation is already out of date. To find that out, you have to go to the Releases tab on Github, and look for the most recent release on the 7.x branch. Installing Drush to the latest version, or updating it when there's a new release, is starting to involve more steps than I'd like.

So here's the solution. Replace the "checkout" command in the documentation with this one - it will check out the latest tagged release on the branch you are currently on in your git clone:

git checkout $(git describe --abbrev=0 --tags)

Just to unpack that a little. The $ means "evaluate this command, and insert the result here". The "describe" command means "tell me something about this repository". The "--tags" switch means "give me a list of tags". The "--abbrev=0" means "just give me the first one you find".

Try it. Clone Drush from Github, as per the command in the documentation, then run

git describe --abbrev=0 --tags

You will get (at time of writing) the result "7.0.0-alpha7" - the identifier for the most recent tag on the current branch. The $(...) syntax substitutes that result, so effectively you're running

git checkout 7.0.0-alpha 7

only if there were any new tagged releases you'd be checking out whichever is the most recent (and not 7.0.0-alpha 7).

What if you wanted to run Drush 6, not the still-in-pre-release Drush 7? Easy: switch to the 6.x branch, then run this statement:

git checkout 6.x
git describe --abbrev=0 --tags

gives you "6.5.0". Try it.

Want to go back to Drush 7. There's a slight gotcha here. Drupal's Git repository has got rid of any branches called "master", so the trap is to try this:

git checkout 7.x
git describe --abbrev=0 --tags

The first command fails: "pathspec '7.x' did not match any file(s) known to git"

That's because the Drush repository on Github does not have a 7.x branch, but does have a master branch. No idea why. It's not the Drupal way, but then again it's not either, so fair enough.

git checkout master
git describe --abbrev=0 --tags

It works.

All of this means we now have a way to check out Drush from Github, and to get the latest tagged release on whichever branch we want. That code snippet from the Documentation is now much more useful.

Specifically, it makes it easy to checkout and run the latest release of Drush 7, which is the Drush you need to use for Drupal 8. So if this blog post helps get more people testing Drush 7, and more people testing Drupal 8, then good. The more people involved, the sooner we'll hit that magic moment when Drupal 8 can finally release.

Over at OakHosting.NET, everything is now moved over to Drush 7. Yes, it's still in pre-release, but the features that work with Drupal 7 sites are stable, so it means it's possible for people to start working with the beta phase of Drupal 8.

Blog Category: 

Add new comment

Additional Terms