Project

General

Profile

Installing Drush for working with Drupal 6-8 and Backdrop » History » Version 35

Jack Aponte, 08/12/2018 10:51 PM

1 14 Jack Aponte
{{lastupdated_at}} by {{lastupdated_by}}
2
3 34 Jack Aponte
{{>toc}}
4
5 1 Jack Aponte
h1. Installing Drush for working with Drupal 6-8 and Backdrop
6
7 9 Jack Aponte
Palante has a few reasons to run different versions of Drush:
8
9
* We're currently supporting or building Drupal 6, 7, 8 _and_ Backdrop sites.
10
* We've fallen victim to the "weird gotcha where Drush destroys your git repo and other non-Drupal files":https://github.com/drush-ops/drush/issues/1581 that seems to happen more often with Drush 8 than other versions.
11
* Some of our clients' servers don't support anything newer than Drush 6; if we run remote commands against those servers, like @drush sql-sync@, we need to use Drush 6 locally, too.
12 1 Jack Aponte
13 6 Jack Aponte
Below is a solution that allows us to use Drush 6, 7 and 8 in the same environment.
14 1 Jack Aponte
15 6 Jack Aponte
h2. Install Drush 6, 7 and 8 for all users
16 1 Jack Aponte
17 11 Jack Aponte
I've followed the "To install for all users on the server" instructions under "Composer - One Drush for all Projects" in the "Drush 7 installation documentation":http://docs.drush.org/en/7.x/install/, adapted for different versions of Drush.
18 1 Jack Aponte
19 28 Jack Aponte
TODO: Update the instructions below to indicate that running @composer install@ as root is a "security risk":https://getcomposer.org/doc/faqs/how-to-install-untrusted-packages-safely.md, give alternative approach.
20
21 8 Jack Aponte
Some of our clients' servers aren't capable of running anything newer than Drush 6. If you need to run commands like @drush sql-sync@ against those servers from your local or dev environment, you'll need Drush 6 installed. Run the following commands as root or using @sudo@.
22
23
<pre>
24
git clone https://github.com/drush-ops/drush.git -b 6.6.0 /usr/local/src/drush6
25
cd /usr/local/src/drush6
26 1 Jack Aponte
composer install
27
ln -s /usr/local/src/drush6/drush /usr/bin/drush6
28 16 Jack Aponte
</pre>
29
30 1 Jack Aponte
To install Drush 7, run the following commands as root or using @sudo@.
31
32
<pre>
33 24 Jack Aponte
git clone https://github.com/drush-ops/drush.git -b 7.4.0 /usr/local/src/drush7
34 1 Jack Aponte
cd /usr/local/src/drush7
35
composer install
36
ln -s /usr/local/src/drush7/drush /usr/bin/drush7
37
</pre>
38
39 24 Jack Aponte
Replace "7.4.0" with the tag of the most recent stable 7.x release (see https://github.com/drush-ops/drush/releases).
40 1 Jack Aponte
41
Then install Drush 8, again running these commands as root or using @sudo@.
42
43 6 Jack Aponte
<pre>
44 29 Jack Aponte
git clone https://github.com/drush-ops/drush.git -b 8.1.15 /usr/local/src/drush8
45 1 Jack Aponte
cd /usr/local/src/drush8
46 6 Jack Aponte
composer install
47 1 Jack Aponte
ln -s /usr/local/src/drush8/drush /usr/bin/drush8
48
</pre>
49
50 35 Jack Aponte
Replace "8.1.17" (last updated in this documentation on 8/12/18) with the tag of the most recent stable 8.x release (see https://github.com/drush-ops/drush/releases)
51 1 Jack Aponte
52
You can now use the @drush6@, @drush7@ or @drush8@ commands to run each specific version of Drush.
53
54 30 Jack Aponte
h3. Updating Drush when installed this way
55 1 Jack Aponte
56 30 Jack Aponte
To update a Drush installation installed in this manner, run the following commands within the @/usr/local/src/<drush-version>@ directory:
57
58 1 Jack Aponte
<pre>
59 30 Jack Aponte
git fetch
60 1 Jack Aponte
git pull origin master
61
git checkout tags/<new version> -b <new version>
62
composer install
63
</pre>
64
65 31 Jack Aponte
Replace @<new version>@ with "the most recent stable release":https://github.com/drush-ops/drush/releases of the Drush version you're updating.
66 17 Jack Aponte
67 18 Jack Aponte
h2. Install Drush on May First/People Link (MFPL) and other shared hosting environments
68 17 Jack Aponte
69 22 Jack Aponte
Primarily drawn from "this MFPL ticket;":https://support.mayfirst.org/ticket/11691#comment:3 thanks Ivan!
70 17 Jack Aponte
71
First, install Composer following "these instructions":https://getcomposer.org/download/.
72
73
Then to install Drush:
74
75
<pre>
76
echo 'export PATH="$HOME/.composer/vendor/bin:$PATH"' >> ~/.bashrc
77
source ~/.bashrc
78 23 Jack Aponte
cd ~/<organization.org>/bin
79 25 Jack Aponte
php composer.phar global require drush/drush:7.4.0
80 1 Jack Aponte
which drush
81
</pre>
82 23 Jack Aponte
83
Note that sometimes @cgi-bin@ is present on MFPL sites instead of @bin@.
84 19 Jack Aponte
85 20 Jack Aponte
If using Drush aliases, you may need to specify the new Drush installation in the shared @aliases.drushrc.php@ file or a local aliases file. Here's an example:
86 19 Jack Aponte
87
<pre>
88 20 Jack Aponte
$aliases['organization'] = array(
89
  'remote-host' => 'organization.org',
90
  'remote-user' => 'organization',
91
  'root' => '/home/members/organization/sites/organization.org/web',
92
  'uri' => 'organization.org',
93 19 Jack Aponte
  'path-aliases' => array(
94 21 Jack Aponte
    '%drush-script' => '/home/members/organization/sites/organization.org/users/organization/.composer/vendor/bin/drush',
95 19 Jack Aponte
    '%dump-dir' => '~/drush-backups',
96 20 Jack Aponte
    '%files' => '/home/members/organization/sites/organization.org/web/sites/default/files',),
97 19 Jack Aponte
);
98 17 Jack Aponte
</pre>
99 1 Jack Aponte
100 33 Jack Aponte
h2. Install and update Drush Backdrop commands
101 4 Jack Aponte
102
In order for Drush to work with Backdrop, you must install the "Drush Backdrop commands":https://github.com/backdrop-contrib/drush within the Drush 8 installation itself.
103
104
Assuming that Drush 8 is installed in @/usr/local/src/drush8@, as specified above:
105
106
<pre>
107
git clone https://github.com/backdrop-contrib/drush.git /usr/local/src/drush8/commands/backdrop
108 1 Jack Aponte
</pre>
109 32 Jack Aponte
110 33 Jack Aponte
To update Drush Backdrop commands later, run @git pull origin@ from within the @drush8/commands/backdrop@ directory.
111 1 Jack Aponte
112
h2. Create script to automatically switch Drush versions based on git configuration setting
113
114
As per "this excellent guide from Marc van Gend of Triquanta":https://www.triquanta.nl/blog/automatically-switch-drush-versions-project, I've set up a script to let us switch between versions of Drush on a project-by-project basis using git configuration settings.
115
116
The script, which lives at @/usr/bin/drush@ and is therefore called whenever someone runs the @drush@ command:
117
118
<pre>
119
#!/bin/bash
120
version=$(git config --get drush.version)
121
if [ "$version" = '8' ];
122
then
123
    drush8 "$@"
124 6 Jack Aponte
elif [ "$version" = '6' ];
125
  then
126
  drush6 "$@"
127 1 Jack Aponte
else
128
    drush7 "$@"
129
fi
130 2 Jack Aponte
</pre>
131 1 Jack Aponte
132
h2. Set drush.version git variable on a per-project basis
133
134 8 Jack Aponte
In this configuration, Drush 7 is the default version of Drush. For the script above to automatically switch to a non-default version of Drush for a given project, set the @drush.version@ value in the git repo for the project by running @git config drush.version <version>@. For example:
135 1 Jack Aponte
136
<pre>
137
$ drush --version
138
 Drush Version   :  7.2.0
139
$ git config drush.version 8
140
$ drush --version
141
 Drush Version   :  8.0.5
142
</pre>
143
144
h2. Update Drush remote site aliases to use correct Drush scripts
145
146
If you've got Drush site aliases set up for remote sites on servers where multiple versions of Drush are available, update your @aliases.drushrc.php@ file to include the specific drush script that should be used on the remote site. See the "example.aliases.drushrc.php":https://github.com/drush-ops/drush/blob/master/examples/example.aliases.drushrc.php file if you need help creating your @aliases.drushrc.php@ file or learning about <code>%drush-script</code>.
147 7 Jack Aponte
148 1 Jack Aponte
An example @aliases.drushrc.php@ file with <code>%drush-script</code> specified:
149
150
<pre>
151
$aliases['drupal8'] = array(
152
  'remote-host' => 'fake-dev-server.palantetech.coop',
153
  'remote-user' => 'jack',
154
  'root' => '/var/www/dev/drupal8',
155
  'uri' => 'drupal8.palantetech.coop',
156
  'path-aliases' => array(
157
    '%drush-script' => '/usr/bin/drush8',
158
  )
159
);
160 8 Jack Aponte
$aliases['drupal7'] = array(
161 1 Jack Aponte
  'remote-host' => 'fake-dev-server.palantetech.coop',
162
  'remote-user' => 'jack',
163 8 Jack Aponte
  'root' => '/var/www/dev/drupal7',
164
  'uri' => 'drupal7.palantetech.coop',
165 1 Jack Aponte
  'path-aliases' => array(
166 8 Jack Aponte
    '%drush-script' => '/usr/bin/drush7',
167 1 Jack Aponte
  )
168
);
169
</pre>
170 10 Jack Aponte
171
h2. Additional resources
172
173
* https://www.triquanta.nl/blog/automatically-switch-drush-versions-project
174
* https://www.lullabot.com/articles/switching-drush-versions
Go to top