Install Drush 7 and 8 Side-by-Side and Automatically Switch Versions Based on Each Project | Modules Unraveled
Have you started working with Drupal 8 yet? If so, you might have noticed that Drush 7 doesn't play nice with Drupal 8. And if you install Drush 8, that won't work with your Drupal 7 sites. Yikes!
Have no fear!
Here's how to install BOTH Drush 7 and Drush 8 AND have each project automatically use the version that corresponds to that install. It's stinkin' awesome!
(The following is an combination and adaptation of techniques I learned from two blog posts. Both were a bit outdated when I came across them, so I've updated the techniques here. The first is a Lullabot article, and the second is on the Triquanta blog)
Uninstall existing Drush instances
Okay, the first thing you'll want to do is uninstall every version of Drush that you already have installed. This process varies depending on how you installed it, but for example, if you installed with homebrew, the command would be something like
brew remove --force drush.
We're going to install multiple versions of Drush using Composer, so we need to make sure you have that installed first. Detailed instructions on how to install Composer globally are on their website, but here's the gist.
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
Note: If this fails due to permissions, run the mv line again with sudo. You may also have to create the /usr/local/bin directory first, depending on your existing system.
- To confirm composer was successfully installed, type
composer --versionand you should see something like "Composer version 1.0-dev (...) 2016-01-20 11:17:40"
Install Drush 8
Okay, let's install Drush 8!
composer require drush/drush:8.x
ln -s /usr/local/bin/drush-8/vendor/bin/drush /usr/local/bin/drush8
- The "composer require..." line will download the latest stable release, you could replace "8.x" with "8.0.x-dev", for example, to download the dev version
- The "ln -s..." line creates a "symbolic link" called "drush8" in the /usr/local/bin directory to the location where Drush 8 is installed. This means that we can call it from anywhere on the system by typing "drush8 --version", for example.
Install Drush 7
Now, we'll install Drush 7!
composer require drush/drush:7.x
ln -s /usr/local/bin/drush-7/vendor/bin/drush /usr/local/bin/drush7
- The "composer require..." line will download the latest stable release, you could replace "7.x" with "7.x-dev", for example, to download the dev version.
- The "ln -s..." line creates a "symbolic link" called "drush7" in the /usr/local/bin directory to the location where Drush 7 is installed. This means that we can call it from anywhere on the system by typing "drush7 --version", for example.
Create Shell Script to Automatically Select Version Based on Git Config
Now, if you're already used to typing something like "drush --version" (without the specific version number), remembering to use it can be a little cumbersome, so now, we're going to create a little shell script that will automatically use the correct one for each project based on a git config variable that we set.
- Press the "i" key to enter "insert" mode
- Paste the following
version=$(git config --get drush.version)
if [ "$version" = '7' ];
- Press "esc", then type ":wq" and press "enter" to save and quit this file
chmod +x drush(This makes the "drush" script we just created executable.)
Now, when we type a command like "drush --version" it will use Drush 8 by default. In order to use Drush 7, we need to set a configuration variable in the git repo of the project that should use it.
Set Drush 7 as the Required Version for a Project
git config drush.version 7
- The first time you run "drush --version" it should return something like "Drush Version : 8.0.0-rc3" showing that you're using Drush 8
- The "git config..." line declares that you want to use Drush 7 for this project
- The second time you run "drush --version" It should show something like "Drush Version : 7.1.0". If so, you're all set!
You might want/need to close and re-open all terminal windows to make sure it takes effect.
If you have any questions about, or issues with, this setup, let me know in the comments!