Install Drush 7 and 8 Side-by-Side and Automatically Switch Versions Based on Each Project
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!
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
.
Install Composer
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<br>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 --version
and 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!
cd /usr/local/bin<br>mkdir drush-8<br>cd drush-8<br>composer require drush/drush:8.0.x-dev<br>ln -s /usr/local/bin/drush-8/vendor/bin/drush /usr/local/bin/drush8
- The "composer require..." line will download the latest dev release, you could replace "8.0.x-dev" with "8.0.2", for example, to download that specific 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.
Easy!
Install Drush 7
Now, we'll install Drush 7!
cd /usr/local/bin<br>mkdir drush-7<br>cd drush-7<br>composer require drush/drush:7.x-dev<br>ln -s /usr/local/bin/drush-7/vendor/bin/drush /usr/local/bin/drush7
- The "composer require..." line will download the latest dev release, you could replace "7.x-dev" with "7.1.0", for example, to download that specific 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.
cd /usr/local/bin<br>vi drush
- Press the "i" key to enter "insert" mode
- Paste the following
#!/bin/sh<br>version=$(git config --get drush.version)<br>if [ "$version" = '7' ];<br>then<br> drush7 "$@"<br>else<br> drush8 "$@"<br>fi
- Press "esc", then type ":wq" and press "enter" to save and quit this file
- Type
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
cd /path/to/project<br>drush --version<br>git config drush.version 7<br>drush --version
- 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 somethign like "Drush Version : 7.1.0". If so, you're all set!
YAY!!!
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!