Upgrading to more recent versions of Node.js on the Raspberry Pi

Published October 28, 2016 · 4 min ·  

Javascript Node.js Raspi
Summary

Upgrading to more recent versions of Node.js on the Raspberry Pi is straightforward and can also be applied to other Debian-based systems. To upgrade within a major version, simply check your current Node version, update the package list with sudo apt update, and then run sudo apt install nodejs to install the latest version available in that branch. For major version upgrades, use the NodeSource setup script to update the package repository before running the same install command. After completing these steps, verifying the Node version will confirm a successful upgrade.

upgrading nodejs

I’ve received questions from readers of my Beginner’s Guide to Installing Node.js on a Raspberry Pi wanting to know how to upgrade to more recent versions of Node.js on the Raspberry Pi.  The steps are quite easy and can be adapted to other Debian variants as well including Ubuntu.  I’m assuming you followed the steps in my Beginners’ Guide, especially under the “Install Node.js” section where we update the Raspbian/Debian package repository to include the Node.js binaries provided by NodeSource.  Let’s get started!

Minor/Patch Version Upgrades

This section explains the steps necessary to upgrade Node to the latest version within a given major version.  For example, upgrading from Node 22.7.0 to Node 22.14.0.

Our first step is to ascertain the current version of Node we are running.  Run the following command from the terminal:

$ node -v
v22.7.0

I see that I am running Node 22.7.0 and I really want to be running the latest stable build within this Node branch (Node 22.x).

First, we run the apt “update” command. This command will not actually update any software on the system, but will download the latest package lists from the software repositories so that Raspbian will be aware of all new software available along with dependencies. Issue the following command at the “$” prompt:

sudo apt update

Note: we are using the newer Debian apt command as opposed to the older apt-get command.  Please see my Debian apt command cheat sheet for more information.

If you are not sure if you are already running the latest version of Node available, you have two options:

  • Option 1 (the “diligent” option): Issue the following command to determine the latest version that is available in the package repository:
apt list nodejs

This command will provide output that looks something like this:

Listing... Done
nodejs/unknown 22.14.0-1nodesource1 armhf [upgradable from: 22.7.0-1nodesource1]
N: There are 3 additional versions. Please use the '-a' switch to see them.

You could optionally add the -a switch  (apt list -a nodejs) to review additional versions of nodejs available; however, the basic apt list command will show the most recent version of Node available.  In this case, we see that Node 22.14.0 is available so we want to continue and upgrade.

  • Option 2 (the “lazy” option) : As a second option if you are not sure if you are already running the latest version of Node, you can simply power through and proceed with sudo apt install (listed next).  If you are already running the latest version, the apt package manager will simply tell you that you are already running the latest version of Node.  Your existing installation will not be impacted in any way.

Ok, we are now ready to upgrade our current version of Node.js to the latest version.  We use the apt install command which seems a bit counter-intuitive, but apt install will upgrade the package to the latest version in the package repository even though it is already installed.

sudo apt install nodejs

Let’s determine what version of Node we are running now:

$ node -v
22.14.0

We see that we are running the latest version of Node so we know we have been successful!

Major Version Upgrades

In this scenario, we check the current version of Node we are running by invoking the following command from the terminal:

$ node -v
v20.18.2

We see that we are running 20.18.2 and we really want to be running Node 22.14.0.  We want to stay on the cutting edge rather than fading into obsolescence.  😎

Let’s update our package repository to use the latest version of Node which is Node 22.14.0 by using the setup_22.x script from NodeSource.

curl -sL https://deb.nodesource.com/setup_22.x | sudo -E bash -

In addition to updating your package repository, the setup script that runs in the previous command also invokes a sudo apt update command as one of its final steps.  Once again, the  sudo apt update command does not actually update any software on the system, but will download the latest package lists from the software repositories so that Raspbian will be aware of all new software available along with dependencies.

We are now ready to upgrade our current version of Node.js to the latest version.  We use the “apt install” command which will upgrade the package even though it is already installed.  Please note that “apt install” will install the latest version of Node that is available in a given Node branch.  For example, if you are using the setup_22.x script, the most recent version of Node 22.x will be installed.

sudo apt install nodejs

When we now request the current version of Node running, we see a more recent version of Node running so we can declare victory!  The old Node version is replaced and no longer running on the machine.

$ node -v
v22.14.0

That’s it for now; thanks for reading!  Follow @thisDaveJ (Dave Johnson) on X and subscribe to my RSS feed to stay up to date on the latest tutorials and tech articles.

Share this Article