Skip to content

Instantly share code, notes, and snippets.

@YewTreeWeb
Forked from mKeRix/getNodeLTS.sh
Created March 8, 2024 22:50
Show Gist options
  • Save YewTreeWeb/98e81d0a4db82770643cfcd7992406ed to your computer and use it in GitHub Desktop.
Save YewTreeWeb/98e81d0a4db82770643cfcd7992406ed to your computer and use it in GitHub Desktop.
Downloads the latest LTS version and installs it into /opt/nodejs. Supports armv6l for e.g. the Pi Zero W.
#!/bin/bash
# Took the good bits from sdesalas/node-pi-zero/
# Switches to unofficial repo since armv6 was removed from main downloads
#
PI_ARM_VERSION=$(
uname -a |
egrep 'armv[0-9]+l' -o
);
if [[ "$PI_ARM_VERSION" == "armv6l" ]]; then
LATEST_BASE_URL="https://unofficial-builds.nodejs.org/download/release"
LATEST_NODEJS_INDEX="${LATEST_BASE_URL}/index.json"
else
LATEST_BASE_URL="https://nodejs.org/dist"
LATEST_NODEJS_INDEX="${LATEST_BASE_URL}/index.json"
fi
VERSION=$(curl -sS $LATEST_NODEJS_INDEX | egrep $PI_ARM_VERSION | egrep '"lts":("[a-zA-Z]+")' | head -n 1 | egrep -o '("version":")(v[0-9]+.[0-9]+.[0-9]+)"' | sed 's/"version"://' | tr -d '"')
# Creates directory for downloads, and downloads node
cd ~/ && mkdir temp && cd temp;
wget "${LATEST_BASE_URL}/${VERSION}/node-${VERSION}-linux-${PI_ARM_VERSION}.tar.gz"
tar -xzf node-$VERSION-linux-$PI_ARM_VERSION.tar.gz;
# Remove the tar after extracing it.
sudo rm node-$VERSION-linux-$PI_ARM_VERSION.tar.gz;
# This line will clear existing nodejs
sudo rm -rf /opt/nodejs;
# This next line will copy Node over to the appropriate folder.
sudo mv node-$VERSION-linux-$PI_ARM_VERSION /opt/nodejs/;
# Remove existing symlinks
sudo unlink /usr/bin/node;
sudo unlink /usr/sbin/node;
sudo unlink /sbin/node;
sudo unlink /usr/local/bin/node;
sudo unlink /usr/bin/npm;
sudo unlink /usr/sbin/npm;
sudo unlink /sbin/npm;
sudo unlink /usr/local/bin/npm;
# Create symlinks to node && npm
sudo ln -s /opt/nodejs/bin/node /usr/bin/node;
sudo ln -s /opt/nodejs/bin/node /usr/sbin/node;
sudo ln -s /opt/nodejs/bin/node /sbin/node;
sudo ln -s /opt/nodejs/bin/node /usr/local/bin/node;
sudo ln -s /opt/nodejs/bin/npm /usr/bin/npm;
sudo ln -s /opt/nodejs/bin/npm /usr/sbin/npm;
sudo ln -s /opt/nodejs/bin/npm /sbin/npm;
sudo ln -s /opt/nodejs/bin/npm /usr/local/bin/npm;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment