Repository navigation
Expand file tree
/
Copy pathsetup_nodeJs.sh
More file actions
executable file
·128 lines (111 loc) · 4.51 KB
/
Copy pathsetup_nodeJs.sh
File metadata and controls
executable file
·128 lines (111 loc) · 4.51 KB
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
#!/bin/bash
# Usage: ./setup_nodeJs.sh [version]
# If version is not provided, installs the latest LTS version of Node.js.
# If a different version is already installed in /opt, it will be removed and replaced.
# Log file location
LOG_FILE="/var/log/node_install.log"
# Create or clear the log file
echo "Node.js Installation Log - $(date)" > $LOG_FILE
echo "-----------------------------------" >> $LOG_FILE
set -o pipefail
# If installed before, remove the old system packages with the available package manager
if command -v apt-get >/dev/null 2>&1; then
apt-get purge -y nodejs npm
elif command -v dnf >/dev/null 2>&1; then
dnf remove -y nodejs npm
elif command -v yum >/dev/null 2>&1; then
yum remove -y nodejs npm
else
echo "Warning: No supported package manager found for removing system Node.js packages."
fi
# Parse command-line arguments for version
if [ $# -gt 0 ]; then
NODE_VERSION="$1"
# Check if it's a major version number (e.g., "20")
if [[ "$NODE_VERSION" =~ ^[0-9]+$ ]]; then
# Find the latest version in this major series
NODE_VERSION=$(curl -s https://nodejs.org/dist/index.json | jq -r "[.[] | select(.version | startswith(\"v${NODE_VERSION}.\"))][0].version" 2>/dev/null)
if [ -z "$NODE_VERSION" ] || [ "$NODE_VERSION" = "null" ]; then
echo "Error: No versions found for major version $1" | tee -a $LOG_FILE
exit 1
fi
echo "Using latest Node.js version in $1.x series: ${NODE_VERSION}" | tee -a $LOG_FILE
else
echo "Using specified Node.js version: ${NODE_VERSION}" | tee -a $LOG_FILE
fi
else
# Try to detect the latest Node.js LTS version from the official index.json
DEFAULT_NODE_VERSION="v22.11.0"
NODE_VERSION=$(curl -s https://nodejs.org/dist/index.json | jq -r '[.[] | select(.lts != false)][0].version' 2>/dev/null || echo "$DEFAULT_NODE_VERSION")
if [ -z "$NODE_VERSION" ]; then
NODE_VERSION="$DEFAULT_NODE_VERSION"
fi
echo "Using latest LTS Node.js version: ${NODE_VERSION}" | tee -a $LOG_FILE
fi
INSTALL_DIR="/opt"
BIN_DIR="/usr/local/bin"
# Detect system architecture and adapt Node binary suffix
ARCH=$(uname -m)
case "$ARCH" in
x86_64|amd64)
NODE_ARCH="x64";
;;
aarch64|arm64)
NODE_ARCH="arm64";
;;
armv7l|armhf)
NODE_ARCH="armv7l";
;;
*)
NODE_ARCH="x64";
echo "Warning: Unknown architecture $ARCH, falling back to x64" | tee -a $LOG_FILE
;;
esac
# Rebuild distro name and URL with architecture
NODE_DISTRO="node-${NODE_VERSION}-linux-${NODE_ARCH}"
NODE_TAR="${NODE_DISTRO}.tar.xz"
NODE_URL="https://nodejs.org/dist/${NODE_VERSION}/${NODE_TAR}"
echo "Detected architecture: $ARCH -> using $NODE_ARCH" | tee -a $LOG_FILE
# Check for existing Node.js installation in /opt
EXISTING_NODE_DIR=$(ls /opt 2>/dev/null | grep '^node-' | head -n1)
if [ -n "$EXISTING_NODE_DIR" ]; then
EXISTING_VERSION=$(echo "$EXISTING_NODE_DIR" | sed -E 's/node-(v[0-9]+\.[0-9]+\.[0-9]+)-.*/\1/')
if [ "$EXISTING_VERSION" != "$NODE_VERSION" ]; then
echo "Removing old Node.js version $EXISTING_VERSION from /opt..." | tee -a $LOG_FILE
sudo rm -rf "/opt/$EXISTING_NODE_DIR"
fi
fi
# Step 1: Download the Node.js tarball
echo "Downloading Node.js LTS version ${NODE_VERSION}..." | tee -a $LOG_FILE
echo "Extracting Node.js to $INSTALL_DIR..." | tee -a $LOG_FILE
curl -fsSL "$NODE_URL" | tar -xJf - -C "$INSTALL_DIR"
if [ $? -ne 0 ]; then
echo "Error: Failed to download Node.js." | tee -a $LOG_FILE
exit 1
fi
# Step 2: Create symbolic links for node, npm, and npx
echo "Creating symbolic links for Node.js binaries..." | tee -a $LOG_FILE
sudo ln -sf $INSTALL_DIR/$NODE_DISTRO/bin/node $BIN_DIR/node
sudo ln -sf $INSTALL_DIR/$NODE_DISTRO/bin/npm $BIN_DIR/npm
sudo ln -sf $INSTALL_DIR/$NODE_DISTRO/bin/npx $BIN_DIR/npx
sudo ln -sf $INSTALL_DIR/$NODE_DISTRO/bin/node $BIN_DIR/nodejs
if [ $? -ne 0 ]; then
echo "Error: Failed to create symbolic links." | tee -a $LOG_FILE
exit 1
fi
# Step 3: Test if the binaries are callable and log the result
echo "Testing Node.js, npm, npx, and nodejs..." | tee -a $LOG_FILE
NODE_VERSION_TEST=$(node -v)
NPM_VERSION_TEST=$(npm -v)
NPX_VERSION_TEST=$(npx -v)
NODEJS_VERSION_TEST=$(nodejs -v)
if [ $? -eq 0 ]; then
echo "Node.js version: $NODE_VERSION_TEST" | tee -a $LOG_FILE
echo "npm version: $NPM_VERSION_TEST" | tee -a $LOG_FILE
echo "npx version: $NPX_VERSION_TEST" | tee -a $LOG_FILE
echo "nodejs version: $NODEJS_VERSION_TEST" | tee -a $LOG_FILE
else
echo "Error: One or more binaries are not callable." | tee -a $LOG_FILE
exit 1
fi
echo "Node.js installation completed successfully." | tee -a $LOG_FILE