Last active
April 16, 2023 20:36
-
-
Save Alexthemediocre/f6ba9e4e2faaab7aa4441104baa51ca3 to your computer and use it in GitHub Desktop.
A script for installing Node.JS on Linux. Defaults to the newest available version and the linux-x64 distro. Also see https://nodejs.org/en/download/current and https://github.com/nodejs/help/wiki/Installation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# shellcheck shell=sh | |
help () { | |
echo "Usage: ./node_install.sh [version[, distro]]" | |
echo "Pass 'help' as the first argument or '-h' or '--help' in any position to view this menu." | |
echo "The default distro is linux-x64. The default version is the newest release." | |
echo "Examples:" | |
echo $'./node_install\t\t\t\tInstalls the linux-x64 release of the newest version of Node.JS.' | |
echo $'./node_install v18.0.0\t\t\tInstalls the linux-x64 release of Node.JS v18.0.0.' | |
echo $'./node_install v18.0.0 linux-arm64\tInstalls the linux-arm64 release of Node.JS v18.0.0.' | |
echo | |
echo "Based on the instructions given in https://github.com/nodejs/help/wiki/Installation." | |
exit | |
} | |
if [[ $1 == 'help' ]]; then | |
help | |
fi | |
for arg in "$@" | |
do | |
if [[ $arg == '-h' ]] || [[ $arg == '--help' ]]; then | |
help | |
fi | |
done | |
VERSION=$1 | |
DISTRO=$2 | |
INSTALL_DIR='/usr/local/lib/nodejs' | |
BIN_PATH='/usr/bin' | |
TMP_PATH='/tmp' | |
# If no version was provided through the arguments | |
if [ -z $VERSION ]; then | |
echo "Fetching versions..." | |
# Fetch versions, get the most recent version - should be on the second line of the file | |
newest_version_info=$(curl https://nodejs.org/dist/index.tab -s | head -2 | tail -1) | |
# Split on whitespace | |
info_array=(${newest_version_info///}) | |
VERSION=${info_array[0]} | |
echo "Versions fetched. Newest version is $VERSION." $'\n' | |
fi | |
# Set default distro if none was provided | |
if [ -z $DISTRO ]; then | |
DISTRO=linux-x64 | |
fi | |
version_name="node-$VERSION-$DISTRO" | |
file_name="$version_name.tar.xz" | |
file_path=$TMP_PATH/$file_name | |
download_path="https://nodejs.org/dist/$VERSION/$file_name" | |
echo "Installing version $VERSION and distro $DISTRO." | |
echo $'\nDownloading...' | |
# Download file | |
curl $download_path -o $file_path | |
echo 'Downloaded.' | |
echo $'\nMaking install dir; extracting...' | |
# Make install dir, if it doesn't already exist | |
sudo mkdir -p $INSTALL_DIR | |
# Extract tarball | |
sudo tar -xJf $file_path -C $INSTALL_DIR | |
echo 'Extracted.' | |
echo $'\nMaking symlinks...' | |
# Symlink executables | |
sudo ln -sf $INSTALL_DIR/$version_name/bin/node $BIN_PATH/node | |
sudo ln -sf $INSTALL_DIR/$version_name/bin/npm $BIN_PATH/npm | |
sudo ln -sf $INSTALL_DIR/$version_name/bin/npx $BIN_PATH/npx | |
echo 'Symlinks made.' | |
# Cleanup | |
rm $file_path | |
echo 'Finished.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment