Install multiple versions of Drush

Drush is a command line utility for Drupal.

To get started you need to have Composer already installed. Check my other tutorial on how to install Composer (on Linux or Mac OS).

Note before we begin: You can find the versions of Drush you want to run here https://packagist.org/packages/drush/drush

Install Drush to use with Drupal 8 (default version)

Since i work mostly on Drupal 8 now, i thought it would make more sense to run default version of Drush for this. 

Open up a terminal and let's install Drush 9 (latest version), this version only supports Drupal 8 or higher and not Drupal 7. Run this command to install latest version:

composer global require drush/drush

Then edit your .bash_profile (if using MacOS) .bashrc (for Linux) so your system can find drush to execute without needing to remember to use the absolute path to the executable file by using the following command (i'm using Linux Mint):

nano ~/.bashrc

Add this to the bottom of the .bashrc script:

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

and save the file.

Run:

source ~./bashrc

drush --version

This should print out the version of Drush, meaning it is working.

Install another Drush Version

Now we can install Drush 8 (this version only supports: D6, D7, D8.3-) to use with Drupal 7.

Run this command to change directory to your home dir and create a new directory called drush-d7

cd ~/

mkdir drush-d7

cd drush-d7

Now we can install Drush8 into drush-d7 dir, using this command:

composer global require drush/drush:8.x-dev

Open up your .bashrc file and add an alias path so we can run Drush without needing to include the absolute path and use a different command since the 'drush' command is already taken for our default Drush installation that was installed earlier. Here i used 'drush7' to run this version.

nano ~/.bashrc

in the .bashrc script, add this line at the bottom of the file:

alias drush7='~/drush-d7/vendor/bin/drush’

Save your file and restart terminal, using:

source ~/.bashrc

Now run this to make sure it is installed and working:

drush7 --version

Drush can only be executed inside of any Drupal installation.

Remember, inside you Drupal 8 installation, use 'drush' as the command. Inside your Drupal 7 installation, use 'drush7' as the command.

Well done, you have installed 2 different versions of Drush.