Running Multiple Versions of Drush at Once

Animation showing switching between drush versions in the terminalSometimes it can be useful have more than one version of Drush globally available. In my situation, we have servers with old versions of Drush and a bunch of scripts that expect Drush to behave in a certain way. None of this can be upgraded overnight. We also want to move forward and develop on the latest stuff. So, we need several versions of Drush available to bridge the gap between maintaining what we have and upgrading to the latest and greatest. We can see what we are going for in this setup by watching the animated gif.


We may want to move the current version of Drush out of the way, so we don't get confused. We will also want to make sure we have Composer as that is the preferred way to install Drush. In order to support a wide range of Drush versions, PHP 5.6 is working pretty well for me. I am not sure how well older versions of Drush work with PHP 7.x. This setup was done on MacOS Sierra, but may be adaptable to your system. 

Installing Multiple Versions of Drush

I like to have a single place to find all of my Drushes, so I made a Drush folder in my home directory. The exact place doesn't really matter, as long as we update the paths in the examples accordingly.

mkdir ~/drush

Drush versions 6 and greater can be installed with composer, so let's get Drush 8. We can get Drush 6 and 7 using a similar strategy. 

mkdir ~/drush/drush8
cd ~/drush/drush8
composer require "drush/drush:8.*"

If we need an older version of Drush we can get a zip or tarball from GitHub by looking through the releases. For example the tarball for version 5.10.0 can be found here.

cd ~/drush
curl -LO
tar -xzvf 5.10.0.tar.gz
mv drush-5.10.0 drush5

We will also want to alter things a little to make our PATH environment variable a little cleaner when we get to the last step. We'll make a bin folder that contains a symbolic link to the executable.

cd drush5
mkdir bin
cd bin
ln -s ../drush ./drush

Adding Aliases to Run These Drushes

Now that we have the binaries we need, we will want to run them without referencing the full path. Add the following to the ~/.bash_profile.

# Use drush version 5 as the default when running the drush command.
export PATH=$PATH:~/drush/drush5/bin

# drush version 8 available as the drush8 command.
alias drush8='~/drush/drush8/vendor/bin/drush'

# drush version 5 available as the drush5 command.
alias drush5='~/drush/drush5/bin/drush'

After saving, source ~/.bash_profile or open a new terminal window and test.

drush --version

should return version 5.10.0 and

drush8 --version

should return version 8.1.12 (or whatever the latest 8.x version is). We can add as many aliases as Drush versions we require.

BONUS: Switch Versions of Drush on the Fly

Wouldn't it be nice to be able to switch which version of Drush will be used when we issue the Drush command on a session by session basis? In the configuration above we set the Drush command to run version 5 by default. But, what if we want to temporarily run version 8 instead? We can do that by adding an alias that will add the appropriate Drush to the PATH environment variable. Add the following aliases to the ~/.bash_profile.

# Switch drush versions for session.
alias d8='export PATH=~/drush/drush8/vendor/bin:$PATH'
alias d5='export PATH=~/drush/drush5/bin:$PATH'

After saving, source ~/.bash_profile or open a new terminal window again and test. Now, we can just run d8 and get Drush 8 when we run Drush. Then if we want to switch to version 5 we can run d5 or open a new terminal window to get the default. By adding to the PATH this should allow it to work within non-interactive sessions such as scripts.