Installing Node.js on a low end VPS

in Experimental


As a pre-requisite to installing GatsbyJS on a VPS, I needed it to run a compatible version of Node.js first

#!/bin/bash

# General housekeeping
apt-get update
apt-get upgrade

# Some minimal Debian distributions are too minimal
apt-get install --no-install-recommends curl apt-transport-https ca-certificates

# Add the NodeSource package signing key
KEYRING=/usr/share/keyrings/nodesource.gpg
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | tee "$KEYRING" >/dev/null
gpg --no-default-keyring --keyring "$KEYRING" --list-keys

# Replace with the branch of Node.js or io.js you want to install: node_6.x, node_8.x, etc...
VERSION=node_14.x

# The below command will set this correctly, but if lsb_release isn't available, you can set it manually:
# - For Debian distributions: jessie, sid, etc...
# - For Ubuntu distributions: xenial, bionic, etc...
# - For Debian or Ubuntu derived distributions your best option is to use the codename corresponding to the upstream release your distribution is based off. This is an advanced scenario and unsupported if your distribution is not listed as supported per earlier in this README.
DISTRO="$(lsb_release -s -c)"
echo "deb [signed-by=$KEYRING] https://deb.nodesource.com/$VERSION $DISTRO main" | tee /etc/apt/sources.list.d/nodesource.list
echo "deb-src [signed-by=$KEYRING] https://deb.nodesource.com/$VERSION $DISTRO main" | tee -a /etc/apt/sources.list.d/nodesource.list

apt-get update
apt-cache policy nodejs

# Consult the apt-cache policy output for a specific Node version, e.g.:
#apt-get install nodejs=14.16.1-1nodesource1

Note: 16.x won't work on Debian 8 (Jessie), but 14.x will and shouldn't get EOL until well into 2023.

Heads up though: with 14, you'll probably run into signatures couldn't be verified because the public key is not available: NO_PUBKEY. To solve,

curl -fsSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -

Sources:

#node-js #low-end-box #vps