Quick BASH script to install a chosen version of node.js on Debian/Rasbian/etc

Updated the script to add error checking. Thanks @jbudd! :slight_smile:


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.

1 Like

I think this should be (.x appended to $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

You really should check the curl exit code is zero before executing the resulting file!

Thanks.

But no, the line as typed was correct. You are grabbing the script for the MAJOR version only, the minor version installations are controlled by apt thereafter.

What their script is doing is simply replacing the appropriate apt repo in your systems list with the one for the chosen major version.

Yes, that is true. About to update the script - GitHub Copilot really is GREAT for stuff like this. :slight_smile:

I only tried it with the version suggested - 20 - and this is what I got

pi@zero2amber:~ $ bash ./junk
Enter Node.js version (e.g. 20): 20
+ curl -fsSL https://deb.nodesource.com/setup_20 -o nodesource_setup.sh
curl: (22) The requested URL returned error: 404
+ echo 22
22
+ exit
pi@zero2amber:~ $

A screengrab from nodesource.com has .x (for node 20)

Sheesh! My bad, you are right. Another correction on its way.

I trusted Copilot a bit too much there!