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

**URL:** https://discourse.nodered.org/t/quick-bash-script-to-install-a-chosen-version-of-node-js-on-debian-rasbian-etc/95117
**Category:** FAQs
**Tags:** nodejs
**Created:** [4 February 2025 16:49 UTC](https://discourse.nodered.org/t/quick-bash-script-to-install-a-chosen-version-of-node-js-on-debian-rasbian-etc/95117 "2025-02-04T16:49:31Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [4 February 2025 16:49 UTC](https://discourse.nodered.org/t/quick-bash-script-to-install-a-chosen-version-of-node-js-on-debian-rasbian-etc/95117/1 "2025-02-04T16:49:31Z")

</div>

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](https://nodered.org/docs/getting-started/raspberrypi), some may prefer to do this themselves. Here is a script that will do that for you to the latest install standards from nodesource.

```bash
#!/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:

```bash
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:

```bash
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.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [4 February 2025 17:00 UTC](https://discourse.nodered.org/t/quick-bash-script-to-install-a-chosen-version-of-node-js-on-debian-rasbian-etc/95117/2 "2025-02-04T17:00:04Z")

</div>

> [@TotallyInformation](#):
>
> ```auto
> # Download the correct install script for the required version
> curl -fsSL https://deb.nodesource.com/setup_$NODE_VERSION -o nodesource_setup.sh
> 
> ```

I think this should be (.x appended to $NODE\_VERSION)

```auto
# 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!

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [4 February 2025 17:08 UTC](https://discourse.nodered.org/t/quick-bash-script-to-install-a-chosen-version-of-node-js-on-debian-rasbian-etc/95117/3 "2025-02-04T17:08:11Z")

</div>

Thanks.

> [@jbudd](#):
>
> I think this should be (.x appended to $NODE\_VERSION)

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.

> [@jbudd](#):
>
> You really should check the curl exit code is zero before executing the resulting file!

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

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [4 February 2025 17:13 UTC](https://discourse.nodered.org/t/quick-bash-script-to-install-a-chosen-version-of-node-js-on-debian-rasbian-etc/95117/4 "2025-02-04T17:13:25Z")

</div>

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

```auto
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](http://nodesource.com) has .x (for node 20)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b2ba0cf088e9d2a5ad538be0a6c81efedb394bb7.png)

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [4 February 2025 17:16 UTC](https://discourse.nodered.org/t/quick-bash-script-to-install-a-chosen-version-of-node-js-on-debian-rasbian-etc/95117/5 "2025-02-04T17:16:43Z")

</div>

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

I trusted Copilot a bit too much there!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [6 March 2025 16:49 UTC](https://discourse.nodered.org/t/quick-bash-script-to-install-a-chosen-version-of-node-js-on-debian-rasbian-etc/95117/6 "2025-03-06T16:49:32Z")

</div>

This topic was automatically closed after 30 days. New replies are no longer allowed.
