Switching between Drush major versions
As I do a lot of work on legacy code and sites, I find it useful to have multiple versions of Drush installed on my laptop; it's also handy for testing version compatibility of my own projects like Drush Instance.
However, for convenience, I also want to be able to refer to any given Drush version as just "drush" at the command prompt. If you want to do this, you need local installations of more than one Drush versions, in different locations; and a switching mechanism. Here's one possible solution, for Debian/Ubuntu.
Installing Drush versions in /opt
My personal preference for manual installations is in.
/opt
I believe this can cause problems on some OSX versions at least, so if you've got some peculiarity with your own system then do feel free to install these packages where you see fit.
To begin, create if
/opt
it doesn't already exist, and make it owned by you.
Then install some Drush versions in some subdirectories. Drush 6 to 8 are straightforwardly installable using composer:
Drush 5 and earlier don't seem to be installable using composer, but it's very quick to install these manually using the Github zip file(s):
You can now manually reference different versions of Drush thus:
The next step is to make these switchable so that you can change the version called when you just type "drush".
Installing into the "alternatives" framework
On Debian systems, many executables can be managed so that anyone of them at any time "serves" the actual command in e.g..
/usr/bin
The command to manage these alternative versions is called.update-alternatives
Install your three different Drush versions into the alternatives system using the following commands:
The last figure is actually a priority: as it's currently set, the system will automatically prioritize Drush 8, because its priority "8" is highest. You could, of course, set different priorities, so that your preferred major version comes out on top, but we'll see below why it's useful to set the major version number as the priority.
Switching between Drush versions
As mentioned above, the highest-priority alternative will be the default, if the alternatives are configured in "auto mode" (again, the default.) However, it's very quick to switch to manual mode, and thus switch to a manually selected alternative.
The recommended way to choose a different alternative at the command line is the following, which calls up an interactive user interface in the terminal:
This interface does require you to then manually press the number of the alternative you want (1..3 in this case) and press enter. Also, the non-interactive alternatives require you to give the full path to the Drush executable, which seems to be a failing of the alternatives system: why register those alternatives in the first place, if you then have to supply their full paths at a later date in order to switch them?
However, there's a shortcut you can use. If you've set your priorities to be your Drush major versions, then the following one-liner will use the filtered output from one
update-alternatives
command to automatically set the alternative in a second command:
You could actually save this as a file called:
/usr/bin/switchdrush
Then
sudo chmod +x
this file, and you can run it in the future as follows:Summary
You should now be able to switch your Drush version very quickly using the alternatives system.
In principle, much of what the alternatives system provides, you could code yourself. Especially when it comes to having to write the executable
switchdrush
. However, in practice, it's reassuring to be able to rely on an existing framework that you know is robust: if in doubt, you can always drop back down to that system.
Comments
Post a Comment