Updated the script to add error checking. Thanks @jbudd!
Hi all,
From time-to-time, we get people getting confused or stuck when trying to manage node.js versions on Debian based OS's (includes Debian, Rasbian and many others).
While most people may prefer to run Dave's excellent script, some may prefer to do this themselves. Here is a script that will do that for you to the latest install standards from nodesource.
#!/bin/bash
# Get required node version from console input
read -p "Enter Node.js version (e.g. 20): " NODE_VERSION
# Download the correct install script for the required version
curl -fsSL https://deb.nodesource.com/setup_${NODE_VERSION}.x -o nodesource_setup.sh
# Check whether the download was successful
if [ $? -ne 0 ]; then
echo "Failed to download the setup script for node.js version $NODE_VERSION"
exit 1
fi
# Run the install script
sudo -E bash nodesource_setup.sh
# Check whether the script was executed successfully
if [ $? -ne 0 ]; then
echo "Failed to execute the setup script for node.js version $NODE_VERSION"
exit 1
fi
# Install Node.js
sudo apt-get install -y nodejs
# Check whether the installation was successful
if [ $? -ne 0 ]; then
echo "Failed to install node.js version $NODE_VERSION"
exit 1
fi
# Clean up
rm nodesource_setup.sh
# Check the installed version
node -v
Just save this to a file such as node_install.sh
and then mark it as executable:
chmod +x node_install.sh
Note that you only need to provide the major version (e.g. 18, 20, or 22) because after that, you only need to do the following to update to the latest minor versions:
sudo apt update && sudo apt upgrade
Of course, you need to restart Node-RED after the update so that it will use the new version of node.