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

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 20.1.0 to Node 20.5.1.

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

$ node -v
v20.1.0

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

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 20.5.1-1nodesource1 armhf [upgradable from: 20.1.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 20.5.1 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
v20.5.1

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
v10.12.0

We see that we are running Node 10.12.0  and we really want to be running Node 20.x.  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 20.5.1 by using the setup_20.x script from NodeSource.

$ curl -sL https://deb.nodesource.com/setup_20.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_20.x script, the most recent version of Node 20.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
v

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

Related Articles

Beginner’s Guide to Installing Node.js on a Raspberry Pi
While I napped, we got a new apt – Debian apt command cheat sheet
Learning through Making – Getting Started with Node.js
Create a Web Server in Node without any Code
Make Your Raspberry Pi Web Server Available on the Internet with Node.js

Share

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

  1. Hi! Thanks for the great tutorials one question I have, if I type type ‘nodejs -v’ into the command line I get ‘v7.0.0’, but if I type in ‘node -v’ I get ‘v4.3.2’, am I missing something? Are nodejs and node different frameworks? How do I update both? Thanks so much!

    1. Hi Dan, you are very welcome!

      It sounds like you might have previously installed the “node” package which is used for Amateur Packet Radio and this has created some conflicts with the “nodejs” package which we use for Node.js. As another option, you might have previously installed Node.js through another method such as “dpkg” rather than through my recommended method with apt via the NodeSource repository. Here’s what you can try:

      $ which node
      (This will show the path of the node binary such as /usr/bin/node and might yield some clues if it is not “/usr/bin/node”. For example, it *might* be “/usr/sbin/node” if you installed the Amateur Packet Radio “node” package.)

      $ ls -la `which node`
      (Those are backticks surrounding which node. This may show you that “node” is pointing to a symbolic link (symlink) such as /etc/alternatives/node which you could update to point to /usr/bin/nodejs.

      When you run “apt install nodejs”, it creates a binary at /usr/bin/nodejs, but it also tries to creates a shortcut at /usr/bin/node that points to /etc/alternatives/node. If there are conflicts, “node” may point to the wrong place while “nodejs” points to the Node v7 package you just installed. One solution is to always invoke node.js using “nodejs” and all will be good. (You will update Node.js versions following my guide above and using the package name of “nodejs” like everyone else.) If you want to invoke with “node”, you can do the following (assuming you installed the “node” package by mistake in addition to “nodejs” via my guide):


      $ sudo apt remove node
      $ sudo apt remove nodejs
      $ sudo apt install nodejs
      $ sudo reboot

      Hope this helps!

      1. Dave, When you run “sudo apt install nodejs” how do you manage/control the version you will get. Myself, I’d like to upgrade fro 6.12.1 to 8.9.2, the v8 LTS version. Can I specify a version in some way?

        If I use curl to install Node 8.9 2 will it, as I wish to, uninstall 6 ? Or will I have two versions installed which I actually don’t want. Thanks.
        Thanks.

        1. Richard, great questions! I added some additional notes in my post to try to make it more clear. Yes, if you are currently running Node 6.12.1 and you follow the steps in the post for “Major Version Upgrades” with the setup_8.x script, your system will replace Node 6.12.1 with the latest version of Node in the Node 8 branch. You will not be running both Node 6.x and Node 8.x. Also, you will receive the latest version of Node in a given Node branch you specify (for example, Node 8.x for the setup_8.x script) and you don’t have the ability to specify a specific version.

      2. So sorry to keep writing but one more question. In your curl example, do I really specify 8.x to install the current LTS 8.9.2? Or is the x a placeholder intending for me to replace with the sub-version I actually want? Thx.

        1. Hi Richard. The x is not a placeholder for the sub-version. The whole name of the script that installs the latest version of node v8 is called “setup_8.x”. You can have a look at that bash script by entering URL “https://deb.nodesource.com/setup_10.x” in your navigator

      3. Dave, thanks for the answer above, it makes sense to me, but I’m still quite lost as to how I resolve my issue.

        I followed the process you suggested as above, but when I ‘ls -la `which node`, the result points to a folder which doesn’t exist – /usr/local/bin/node. Which has me totally bewildered. Even after running:

        $ sudo apt remove node
        $ sudo apt remove nodejs
        $ sudo apt install nodejs
        $ sudo reboot

        My node -v is still 6.10.3 and my nodejs version is 10.5.0, so how do I alias node so it points to nodejs only?

    2. I had this very issue with that very same version of node. In my case, running the instructions above did install a new version of node into /usr/bin. Unbeknownst to me, somebody had also installed node version 4.3.2 in /usr/local/bin. Since my PATH was set to look in /usr/local/bin before it looked in /usr/bin, it would always run version 4.3.2. Not sure how safe it is just to flat out delete the file in /usr/local/bin so I just renamed it along with npm and voila, now I’m running the newer version. I’d like to know how to properly uninstall version 4.3.2 but don’t have that figured out quite yet.

  2. Hi,

    What if

    $ apt list nodejs

    shows some old version like:

    Listing… Done
    nodejs/stable,now 0.10.29~dfsg-2 armhf [installed,automatic]

    And this is the version that is currently installed. I would like to install the version 4 or up.

    thanks,
    sigman

    1. Hi sigman, you can follow my upgrade steps under “Major Version Upgrades” and substitute “setup_7.x” with your desired version of Node. For example, “setup_6.x” for Node 6.x. As part of the major upgrade process (when you sudo apt install nodejs), the current Node version installed on your system (node 0.10.29, in your case) will be removed and replaced with the updated version of Node you specified. At that point, you will be good to go and can follow the “Minor/Patch Version Upgrades” section of my post for minor (security, etc.) updates in the future.

      1. Hi Dave,

        I have the same issue, but I do not understand your comment. If I check for the available version, i´m also told it´s 0.10.29. If I check my version, it´s 7.9.0. Even if I do remove node and nodes and ask again “node -v”, I still get the answer 7.9.0.
        Looking for the path, it´s in usr/local/bin/node. Might this be an issue?

        1. Hi Rolf, I’m a little confused on the issue you are experiencing. If you run “node -v” and see “7.9.0”, this is desirable since node 7.9.0 is much newer than the ancient node 0.1.29, and your system will thus use node 7.9.0 when you run node programs. I do recommend running the setup_8.x script to install node 8.* since this is the current LTS (long term support) version of node.

  3. Hi i am running node red in my latest raspberry pi 3 with node red version of 6.10. it repeatedly crashes with errors uncaught exception and enoment ? the graphical page is not opening. how do i rectify it

  4. Hi, I followed your post here, but still I get: The package nodejs needs to be reinstalled, but I can’t find an archive for it.

    I use Raspbian Jessie on RPi3.

        1. Markus, glad you were able to get it working. Thanks for sharing the solution that worked for you.

  5. I’m on a Raspberry Pi Zero W, and attempting to upgrade to 8.x gives the message below. Any ideas for an upgrade path via apt instead of manually installing tarballs?

    pi@rpiw2:~ $ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash –

    ## Installing the NodeSource Node.js v8.x repo…

    ## You appear to be running on ARMv6 hardware. Unfortunately this is not currently supported by the NodeSource Linux distributions. Please use the ‘linux-armv6l’ binary tarballs available directly from nodejs.org for Node.js v4 and later.

    pi@rpiw2:~ $

    1. Great question. Unfortunately the Raspberry Pi Zero is based on an ARMv6 architecture unlike the Raspberry Pi 3 (and Pi 2). I am not aware of an apt solution for ARMv6 so tarballs are the only way at the moment for the Pi Zero.

  6. hi davej
    you can help me
    plase
    how to install nodejs v4.7.7 on raspberrypi3
    plase fast hellp
    im wating mr.davej

  7. Thanks for the great tutorial.
    Now I’m on Node 10.10.0

    But what about the old Node folders?
    /usr/local/n/versions/node

    There a some old folders like 9.10.1, 10.1.0, 10.7.0

    can I just delete them for the reason of saving space on the SD?
    Or any special advice how to do so? Thanks.

  8. Hi!
    Drops the following error on Raspberry Pi 4

    he following packages have unmet dependencies:
    nodejs : Depends: libstdc++6 (>= 5.2) but 4.9.2-10+deb8u2 is to be installed
    E: Unable to correct problems, you have held broken packages.

Leave a Reply

Your email address will not be published. Required fields are marked *