While developing linux services with Node.js I found myself switching between various versions
of Node.js and eventually it just made sense to create a Bash script to manage the process of
switching between versions.
Rather than hunt for the needed download and manually installing all that is required is to
run a simple command...
sudo ./nodejs-install.sh install -v 10.15.3
As demonstrated in the following video, the process is simple. Use git to clone
the nodejs-install repository, then execute the
install script with root permissions while passing the install command and the version option. The Node.js
install package will be downloaded and setup for global use. If the package was previously installed
the download is skipped and the selected version is set as the global default. In the demonstration I install
an older version of Node.js, test the installed version, then install a newer version of Node.js, again test
the installed version, and finally run the install command for the older version again which will skip the
download and when complete I again test the installed version.
Auto Download
After validating the version on the download page, the script checks to see if the requested
version is alredy installed. If the version is already installed then the download step is
automatically skipped and the existing install is configured as the system default.
# install nodejs
function InstallNodeJS()
{
IsRoot
# get version details in array (filename version platform architecture)
local version=($(VersionAvailable "$NODE_PLATFORM" "$NODE_ARCHITECTURE"))
if [[ -z "$version" ]]; then exit 1; fi
local node_version="${version[1]}" # may differ if search was in "latest"
local node_file="${version[0]}"
local node_install_dir=$(VersionPath $node_version)
local node_download_path="${NODE_SELECTED_WEB_PATH}/v${NODE_VERSION}/${node_file}"
mkdir -p "$INSTALL_PATH"
cd $INSTALL_PATH
# get node package if not installed
if [ ! -d $node_install_dir ]; then
wget "$node_download_path" -O "$node_file"
local wgetreturn=$?
if [[ $wgetreturn -ne 0 ]]; then
Die "Download $node_download_path failed"
fi
# extract
tar xf "$node_file"
# remove tar
rm -f "$node_file"
fi
if [ $(UsingAlternatives) ]; then
InstallAlternatives "$INSTALL_PATH/$node_install_dir" "$node_version"
else
InstallSymlinks "$INSTALL_PATH/$node_install_dir"
fi
echo "Install complete."
}
Symlinks or Alternatives
Some linux distributions include the alternatives
command to manage symbolic links but the script is designed to manage the symbolic links when alternatives
is not available.
# check if system uses alternatives
function UsingAlternatives() {
command -v update-alternatives
if [ $? -eq 0 ]; then
true
else
false
fi
}
# install in alternatives
function InstallAlternatives()
{
local node_install_path=$1;
local node_version=$2
local priority=`echo $node_version | sed 's/[\.v]//g'`
update-alternatives --install /usr/bin/node node "$node_install_path/bin/node" "$priority"
update-alternatives --set node "$node_install_path/bin/node"
update-alternatives --install /usr/bin/npm npm "$node_install_path/bin/npm" "${priority}"
update-alternatives --set npm "$node_install_path/bin/npm"
if [ -f "$node_install_path/bin/npx" ]; then
update-alternatives --install /usr/bin/npx npx "$node_install_path/bin/npx" "${priority}"
update-alternatives --set npx "$node_install_path/bin/npx"
fi
}
# install as symlink
function InstallSymlinks()
{
local node_install_path=$1;
ln -sf "$node_install_path/bin/node" "$BIN_PATH/node"
ln -sf "$node_install_path/bin/npm" "$BIN_PATH/npm"
if [ -f "$node_install_path/bin/npx" ]; then
ln -sf "$node_install_path/bin/npx" "$BIN_PATH/npx"
fi
}