4

So in my environment, I have some websites on Drupal 7 and some on Drupal 8, and since the latest Drush 9 works with Drupal 8.4+ I was wondering if there's a way to be able to have both Drush 9 and (put another version here) to work on both major Drupal versions.

I'd like to be able to just use drush as my main alias and not define different aliases for different Drupal versions.

  • 3
    instead of using one global drush you can install drush via composer for each website in a verison you want so it would be scoped only to that particular website. – user21641 Aug 14 '18 at 9:04
  • 2
    Or just install Drush 8 globally and Drush 9 local to each project. It will automatically use the correct one when in a project folder. – Clive Aug 14 '18 at 9:32
  • 3
    Yeah you can install them both globally, but it's totally up to you how you manage that. You could create aliases, for example (drush8 cc all, drush9 cr, etc), or take any number of other approaches. Using project-specific versions is recommended, though – Clive Aug 14 '18 at 9:48
  • 1
    The documentation for Drush suggest not to globally install it, if I recall correctly. It is even not supported anymore. – kiamlaluno Aug 14 '18 at 10:18
  • 2
7

Ideal way would be to use Composer to set up your projects. I'd recommend Composer templates for Drupal projects. Then add Drush to your project: composer require drush/drush.

Now install Drush Launcher on your machine, globally. Drush Launcher provides a drush command that always finds and uses the locally installed Drush from the /vendor folder.

$ cd MYDRUPAL
$ drush --version
Drush Launcher Version: 0.6.0
Drush Commandline Tool 9.3.0

But probably you can not simply rebuild your projects. Then you may want to install two different global Drush versions. See https://github.com/drush-ops/drush/releases for all available versions.

drush8

$ sudo git clone https://github.com/drush-ops/drush.git /usr/local/src/drush8

$ cd /usr/local/src/drush8

$ sudo git checkout 8.1.17

# Linux:
$ sudo ln -s /usr/local/src/drush8/drush /usr/bin/drush8

# Mac:
$ sudo ln -s /usr/local/src/drush8/drush /usr/local/bin/drush8

$ sudo composer install

$ drush8 --version
Drush Version   :  8.1.17

drush9

$ sudo git clone https://github.com/drush-ops/drush.git /usr/local/src/drush9

$ cd /usr/local/src/drush9

$ sudo git checkout 9.3.0

# Linux:
$ sudo ln -s /usr/local/src/drush9/drush /usr/bin/drush9

# Mac:
$ sudo ln -s /usr/local/src/drush9/drush /usr/local/bin/drush9

$ sudo composer install

$ drush9 --version
Drush Version   :  9.3.0

Source: Installing & Using Drupal Drush on Debian 7 (Bookmark this!)

5

I do a slightly different method than @leymannx outlined.

I have Drush 8 installed globally. In my case, I have a git clone and I periodically fetch and checkout one of the 8.x.y tags. This is on my bash $PATH. This is what I use to manage my Drupal 7 sites.

All of my Drupal 8 sites use drupal-composer/drupal-project, and include a dependency on drush/drush. I also put

"config": {
    "bin-dir": "bin/"
},

in my composer.json. Then I use direnv to set up a path for the root directory in drupal project. My .envrc will contain a line like

PATH_add /var/www/prod/bin

This prepends $PATH with this directory, so this version of drush will be picked up for that project. This ensures that drush is pulling the same dependencies as that particular drupal install.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.