-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall_nodejs.sh
359 lines (331 loc) · 12.1 KB
/
install_nodejs.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/bin/bash
installVer=$1 #NodeJS major version to be installed
minVer=$1 #min NodeJS major version to be accepted
NODE_MAJOR=$( [[ $installVer == *.* ]] && echo $installVer | cut -d'.' -f1 || echo $installVer )
if [ "$LANG_DEP" = "fr" ]; then
step 10 "Prérequis"
else
step 10 "Prerequisites"
fi
#ipv4 first for dns (like before nodejs 18)
export NODE_OPTIONS="--dns-result-order=ipv4first"
#prioritize nodesource nodejs : just in case
sudo bash -c "cat >> /etc/apt/preferences.d/nodesource" << EOL
Package: nodejs
Pin: origin deb.nodesource.com
Pin-Priority: 600
EOL
if [ "$LANG_DEP" = "fr" ]; then
step 15 "Installation des packages nécessaires"
else
step 15 "Mandatory packages installation"
fi
# apt-get update should have been done in the calling file
try sudo DEBIAN_FRONTEND=noninteractive apt-get -o Acquire::ForceIPv4=true install -y lsb-release build-essential apt-utils git gnupg
if [ "$LANG_DEP" = "fr" ]; then
step 20 "Vérification du système"
else
step 20 "System Check"
fi
arch=`arch`;
#jessie as libstdc++ > 4.9 needed for nodejs 12+
lsb_release -c | grep jessie
if [ $? -eq 0 ]; then
today=$(date +%Y%m%d)
if [[ "$today" > "20200630" ]]; then
if [ "$LANG_DEP" = "fr" ]; then
echo "$HR"
echo -n "== KO == Erreur d'Installation"
echo
echo "$HR"
echo "== ATTENTION Debian 8 Jessie n'est officiellement plus supportée depuis le 30 juin 2020, merci de mettre à jour votre distribution !!!"
else
echo "$HR"
echo -n "== KO == Installation Error"
echo
echo "$HR"
echo "== WARNING Debian 8 Jessie is not supported anymore since the 30rd of june 2020, thank you to update your distribution !!!"
fi
exit 1
fi
fi
#stretch doesn't support nodejs 18+
lsb_release -c | grep stretch
if [ $? -eq 0 ]; then
today=$(date +%Y%m%d)
if [[ "$today" > "20220630" ]]; then
if [ "$LANG_DEP" = "fr" ]; then
echo "$HR"
echo -n "== KO == Erreur d'Installation"
echo
echo "$HR"
echo "== ATTENTION Debian 9 Stretch n'est officiellement plus supportée depuis le 30 juin 2022, merci de mettre à jour votre distribution !!!"
else
echo "$HR"
echo -n "== KO == Installation Error"
echo
echo "$HR"
echo "== WARNING Debian 9 Stretch is not supported anymore since the 30rd of june 2022, thank you to update your distribution !!!"
fi
exit 1
fi
fi
#x86 32 bits not supported by nodesource anymore
bits=$(getconf LONG_BIT)
if { [ "$arch" = "i386" ] || [ "$arch" = "i686" ]; } && [ "$bits" -eq "32" ]; then
if [ "$LANG_DEP" = "fr" ]; then
echo "$HR"
echo -n "== KO == Erreur d'Installation"
echo
echo "$HR"
echo "== ATTENTION Votre système est x86 en 32bits et NodeJS 12 n'y est pas supporté, merci de passer en 64bits !!!"
else
echo "$HR"
echo -n "== KO == Installation Error"
echo
echo "$HR"
echo "== WARNING Your system is x86 in 32bits and NodeJS 12 doesn not support it anymore, thank you to reinstall in 64bits !!!"
fi
exit 1
fi
if [ "$LANG_DEP" = "fr" ]; then
step 25 "Vérification de la version de NodeJS installée"
else
step 25 "Installed NodeJS version check"
fi
silent type node
if [ $? -eq 0 ]; then actual=`node -v`; else actual='Aucune'; fi
testVer=$(php -r "echo version_compare('${actual}','v${minVer}','>=');")
if [ "$LANG_DEP" = "fr" ]; then
echo -n "[Check Version NodeJS actuelle : ${actual} : "
else
echo -n "[Check Current NodeJS Version : ${actual} : "
fi
if [[ $testVer == "1" ]]; then
echo_success
new=$actual
else
if [ "$LANG_DEP" = "fr" ]; then
echo "Correction..."
step 30 "Installation de NodeJS $NODE_MAJOR"
else
echo "Fixing..."
step 30 "Installing NodeJS $NODE_MAJOR"
fi
#if npm exists
silent type npm
if [ $? -eq 0 ]; then
npmPrefix=`npm prefix -g`
else
npmPrefix="/usr"
fi
silent sudo DEBIAN_FRONTEND=noninteractive apt-get -y --purge autoremove npm
silent sudo DEBIAN_FRONTEND=noninteractive apt-get -y --purge autoremove nodejs
if [[ $arch == "armv6l" ]]; then
#version to install for armv6 (to check on https://unofficial-builds.nodejs.org/download/release/)
if [[ $NODE_MAJOR == "12" ]]; then
armVer="12.22.12"
elif [[ $NODE_MAJOR == "14" ]]; then
armVer="14.21.3"
elif [[ $NODE_MAJOR == "16" ]]; then
armVer="16.20.2"
elif [[ $NODE_MAJOR == "18" ]]; then
armVer="18.19.0"
elif [[ $NODE_MAJOR == "20" ]]; then
armVer="20.11.0"
fi
if [ "$LANG_DEP" = "fr" ]; then
echo "Jeedom Mini ou Raspberry 1, 2 ou zéro détecté, non supporté mais on essaye l'utilisation du paquet non-officiel v${armVer} pour armv6l"
else
echo "Jeedom Mini or Raspberry 1, 2 or zero detected, unsupported but we try to install unofficial packet v${armVer} for armv6l"
fi
try wget -4 https://unofficial-builds.nodejs.org/download/release/v${armVer}/node-v${armVer}-linux-armv6l.tar.gz
try tar -xvf node-v${armVer}-linux-armv6l.tar.gz
cd node-v${armVer}-linux-armv6l
try sudo cp -f -R * /usr/local/
cd ..
silent rm -fR node-v${armVer}-linux-armv6l*
silent ln -s /usr/local/bin/node /usr/bin/node
silent ln -s /usr/local/bin/node /usr/bin/nodejs
#upgrade to recent npm
forceUpdateNPM=1
else
if [ "$LANG_DEP" = "fr" ]; then
echo "Utilisation du dépot officiel"
else
echo "Using official repository"
fi
#old method
#curl -fsSL https://deb.nodesource.com/setup_${installVer}.x | try sudo -E bash -
#try sudo DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs
#new method
sudo mkdir -p /etc/apt/keyrings
silent sudo rm /etc/apt/keyrings/nodesource.gpg
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
silent sudo rm /etc/apt/sources.list.d/nodesource.list
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | silent sudo tee /etc/apt/sources.list.d/nodesource.list
try sudo apt-get -o Acquire::ForceIPv4=true update
try sudo DEBIAN_FRONTEND=noninteractive apt-get -o Acquire::ForceIPv4=true install -y nodejs
fi
silent npm config set prefix ${npmPrefix}
new=`node -v`;
if [ "$LANG_DEP" = "fr" ]; then
echo -n "[Check Version NodeJS après install : ${new} : "
else
echo -n "[Check NodeJS Version after install : ${new} : "
fi
testVerAfter=$(php -r "echo version_compare('${new}','v${minVer}','>=');")
if [[ $testVerAfter != "1" ]]; then
echo_failure -n
if [ "$LANG_DEP" = "fr" ]; then
echo " -> relancez les dépendances"
else
echo " -> restart the dependancies"
fi
else
echo_success
fi
fi
silent type npm
if [ $? -ne 0 ]; then
if [ "$LANG_DEP" = "fr" ]; then
step 35 "Installation de npm car non présent"
else
step 35 "Installing npm because not present"
fi
try sudo DEBIAN_FRONTEND=noninteractive apt-get -o Acquire::ForceIPv4=true install -y npm
forceUpdateNPM=1
fi
npmver=`npm -v`;
echo -n "[Check Version NPM : ${npmver} : "
echo $npmver | grep "8.11.0" &>/dev/null
if [ $? -eq 0 ]; then
echo_failure
forceUpdateNPM=1
else
if [[ $forceUpdateNPM == "1" ]]; then
if [ "$LANG_DEP" = "fr" ]; then
echo "[ MàJ demandée ]"
else
echo "[ Update requested ]"
fi
else
echo_success
fi
fi
if [[ $forceUpdateNPM == "1" ]]; then
if [ "$LANG_DEP" = "fr" ]; then
step 37 "Mise à jour de npm"
else
step 37 "Updating npm"
fi
try sudo -E npm install -g npm
fi
silent type npm
if [ $? -eq 0 ]; then
npmPrefix=`npm --silent prefix -g`
npmPrefixSudo=`sudo npm --silent prefix -g`
npmPrefixwwwData=`sudo -u www-data npm --silent prefix -g`
if [ "$LANG_DEP" = "fr" ]; then
echo -n "[Check Prefixe : $npmPrefix et sudo prefixe : $npmPrefixSudo et www-data prefixe : $npmPrefixwwwData : "
else
echo -n "[Check Prefix : $npmPrefix and sudo prefix : $npmPrefixSudo and www-data prefix : $npmPrefixwwwData : "
fi
if [[ "$npmPrefixSudo" != "/usr" ]] && [[ "$npmPrefixSudo" != "/usr/local" ]]; then
echo_failure
if [[ "$npmPrefixwwwData" == "/usr" ]] || [[ "$npmPrefixwwwData" == "/usr/local" ]]; then
if [ "$LANG_DEP" = "fr" ]; then
step 40 "Réinitialisation prefixe ($npmPrefixwwwData) pour npm `sudo whoami`"
else
step 40 "Prefix reset ($npmPrefixwwwData) for npm `sudo whoami`"
fi
sudo npm config set prefix $npmPrefixwwwData
else
if [[ "$npmPrefix" == "/usr" ]] || [[ "$npmPrefix" == "/usr/local" ]]; then
if [ "$LANG_DEP" = "fr" ]; then
step 40 "Réinitialisation prefixe ($npmPrefix) pour npm `sudo whoami`"
else
step 40 "Prefix reset ($npmPrefix) for npm `sudo whoami`"
fi
sudo npm config set prefix $npmPrefix
else
[ -f /usr/bin/raspi-config ] && { rpi="1"; } || { rpi="0"; }
if [[ "$rpi" == "1" ]]; then
if [ "$LANG_DEP" = "fr" ]; then
step 40 "Réinitialisation prefixe (/usr) pour npm `sudo whoami`"
else
step 40 "Prefix reset (/usr) for npm `sudo whoami`"
fi
sudo npm config set prefix /usr
else
if [ "$LANG_DEP" = "fr" ]; then
step 40 "Réinitialisation prefixe (/usr/local) pour npm `sudo whoami`"
else
step 40 "Prefix reset (/usr/local) for npm `sudo whoami`"
fi
sudo npm config set prefix /usr/local
fi
fi
fi
else
if [[ "$npmPrefixwwwData" == "/usr" ]] || [[ "$npmPrefixwwwData" == "/usr/local" ]]; then
if [[ "$npmPrefixwwwData" == "$npmPrefixSudo" ]]; then
echo_success
else
echo_failure
if [ "$LANG_DEP" = "fr" ]; then
step 40 "Réinitialisation prefixe ($npmPrefixwwwData) pour npm `sudo whoami`"
else
step 40 "Prefix reset ($npmPrefixwwwData) for npm `sudo whoami`"
fi
sudo npm config set prefix $npmPrefixwwwData
fi
else
echo_failure
if [[ "$npmPrefix" == "/usr" ]] || [[ "$npmPrefix" == "/usr/local" ]]; then
if [ "$LANG_DEP" = "fr" ]; then
step 40 "Réinitialisation prefixe ($npmPrefix) pour npm `sudo whoami`"
else
step 40 "Prefix reset ($npmPrefix) for npm `sudo whoami`"
fi
sudo npm config set prefix $npmPrefix
else
[ -f /usr/bin/raspi-config ] && { rpi="1"; } || { rpi="0"; }
if [[ "$rpi" == "1" ]]; then
if [ "$LANG_DEP" = "fr" ]; then
step 40 "Réinitialisation prefixe (/usr) pour npm `sudo whoami`"
else
step 40 "Prefix reset (/usr) for npm `sudo whoami`"
fi
sudo npm config set prefix /usr
else
if [ "$LANG_DEP" = "fr" ]; then
step 40 "Réinitialisation prefixe (/usr/local) pour npm `sudo whoami`"
else
step 40 "Prefix reset (/usr/local) for npm `sudo whoami`"
fi
sudo npm config set prefix /usr/local
fi
fi
fi
fi
fi
if [ "$LANG_DEP" = "fr" ]; then
step 50 "Nettoyage"
else
step 50 "Cleaning"
fi
# on nettoie la priorité nodesource
silent sudo rm -f /etc/apt/preferences.d/nodesource
# ADDING ERROR HANDLERS
# fix npm cache integrity issue
add_fix_handler "EINTEGRITY" "" "sudo npm cache clean --force"
# fix npm cache permissions
add_fix_handler "npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/" "*code 128" "sudo chown -R root:root /root/.npm"
# check for ENOTEMPTY error in both /usr and /usr/local
add_fix_handler "npm ERR! dest /usr/local/lib/node_modules/.homebridge-config-ui-x-" "*ENOTEMPTY local config-ui-x" "sudo rm -fR /usr/local/lib/node_modules/.homebridge-config-ui-x-*"
add_fix_handler "npm ERR! dest /usr/lib/node_modules/.homebridge-config-ui-x-" "*ENOTEMPTY config-ui-x" "sudo rm -fR /usr/lib/node_modules/.homebridge-config-ui-x-*"
add_fix_handler "npm ERR! dest /usr/local/lib/node_modules/.homebridge-alexa-" "*ENOTEMPTY local alexa" "sudo rm -fR /usr/local/lib/node_modules/.homebridge-alexa-*"
add_fix_handler "npm ERR! dest /usr/lib/node_modules/.homebridge-alexa-" "*ENOTEMPTY alexa" "sudo rm -fR /usr/lib/node_modules/.homebridge-alexa-*"
add_fix_handler "npm ERR! dest /usr/local/lib/node_modules/.homebridge-gsh-" "*ENOTEMPTY local gsh" "sudo rm -fR /usr/local/lib/node_modules/.homebridge-gsh-*"
add_fix_handler "npm ERR! dest /usr/lib/node_modules/.homebridge-gsh-" "*ENOTEMPTY gsh" "sudo rm -fR /usr/lib/node_modules/.homebridge-gsh-*"