Install Drush with fall back on Windows with Msys2 and WAMP

This is a very cool command line tool for Drupal development. It allows us to turn modules on/off. Clear the cache. Run cron. Import configuration. Do Drupal database updates and many many more things.

For a full list of commands see this: https://www.drush.org/12.x/commands/all/

For these instructions, you need to replace Guy with your user name. 

It is worth noting that the &&'s in the following commands just allows us to specify many commands to execute, one by one. So it's a separator of sorts.

Install Drush launcher

Download the files from here and extract them into c:/msys64/home/Guy/drush-launcher

Open Msys2 and run: 

cd ~/drush-launcher && composer install

Now update your .bashrc file using these commands. 

echo 'export PATH="~/drush-launcher/bin:$PATH"' >> ~/.bashrc && echo 'alias drush="/home/Guy/drush-launcher/bin/drush"' >> ~/.bashrc

For the changes to be picked up, you'll need to close and re-open your terminal. So this gives us Drush for our Drupal 8+ websites.

For each Drupal 8+ website, you need to require Drush, if it's not already included. Go to your Drupal websites folder and run:

composer require drush/drush

 Check to see if it's working with that version

drush --version

Install Drush 8

Drush 8 is only required if you work on Drupal 6 and 7 websites. Otherwise, you can ignore this and you're good to go. Personally I'd do it while my mind is on the task, just incase I took on older websites downstream. 

Create a directory to download it to and enter it

mkdir ~/drush8 && cd ~/drush8

Download the latest copy

composer require drush/drush:8.*

Now we need to tell Drush launcher to use this version as the fallback.

Add these settings to the end of .bashrc using these commands

echo 'export PATH="~/drush8/vendor/bin:$PATH"' >> ~/.bashrc && echo "export DRUSH_LAUNCHER_FALLBACK=/home/Guy/drush8/vendor/bin/drush.launcher.bat" >> ~/.bashrc

Run this inside a Drupal 6/7 website or outside of a Drupal website to check the fallback

drush --version

Conclusion

I just spent 2 days trying to get this is all to work properly. So hopefully I've saved you from lots of pain.

Drush is a very important  and powerful tool for anyone serious about Drupal development. Sometimes it is the only way to un-break a website.

Post type: 
Technical
Drupal version: 
Drupal 6 (D6)
Drupal 7 (D7)
Drupal 8/9/10