How to install the LTS version of Node and NPM

Background

If you divert from programming for a while (say 6 months to a year) and come back to your NPM and Node tools, you might find that your development environment suddently complains that things are out of date.

In general it’s super easy to upgrade NPM, mostly because NPM already tells you what to do. Node upgrading is slightly more work because it’s a two step process.. This quite guide is intended to assist with upgrading to the latest version of NPM, and “later” versions of Node on Debian based systems.

You can determine which current versions of the tools you have by doing this:

npm -v
node -v

Update 10 October 2022

Installing Node on Jellyfish, Jammy, and Linux Mint Vera continues to be a problem if installed using the stock apt command. On Linux Mint Vera for example, the problem is a really old version of Node version 12 is installed with apt.

To get the newer version 18, you can do this:

sudo apt remove nodejs

The next set up commands should be done as one:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - &&\
sudo apt-get install -y nodejs

Upgrade NPM to Latest

To upgrade NPM simply follow the prompt that tells you it’s out of date. Typically this prompt will look like this:

npm notice New minor version of npm available! 8.1.4 -> 8.13.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.13.2
npm notice Run npm install -g [email protected] to update!

Follow the instructions, or do this:

sudo npm install -g npm@latest

Upgrading Node to a Newer Version

Upgrading Node on a Debian based distrubutions is a two step process. First decide which version you require, and then substitude the version number in the line below. For example, we wanted to install version 16:

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

Now do this:

sudo apt-get install -y nodejs

Troubleshooting

2022 Errors

Unpacking nodejs (16.17.1-deb-1nodesource1) over (12.22.9~dfsg-1ubuntu3) ...
dpkg: error processing archive /var/cache/apt/archives/nodejs_16.17.1-deb-1nodesource1_amd64.deb (--unpack):
trying to overwrite '/usr/include/node/common.gypi', which is also in package libnode-dev 12.22.9~dfsg-1ubuntu3
dpkg-deb: error: paste subprocess was killed by signal (Broken pipe)
Errors were encountered while processing:
/var/cache/apt/archives/nodejs_16.17.1-deb-1nodesource1_amd64.deb

This was the sequence when node -v still gave the old version. The reason was node in an incorrect location.

rm /usr/local/bin/node
rm: remove write-protected regular file '/usr/local/bin/node'? y
rm: cannot remove '/usr/local/bin/node': Permission denied
➜ code sudo rm /usr/local/bin/node
[sudo] password for user:

We  deleted node and only then did it find the new version in the path.

➜ code node -v
v14.17.5

Contents of Old Article

Installing node and npm and even yarn on Ubuntu Linux or Linux Mint can be a nightmare because the usual ‘apt get’ commands do not work properly and want to install really old versions. The problem is once you’re stuck with these old version you have to go through a whole rigmarole to upgrade so it’s just not the effort the use the built-in apt version.

Instead you have to go to a little wild goose chase via the Node website. There you will find easily download instructions – but not install instructions. Instead you have to hunt deeper down and then you will find some more breadcrumbs. Here’s the cinch – you want the LTS version, and not the “Latest Version”. This is the most important thing to take note of. Life is too short for “Latest Version” in this case.

Steps:

From the Node website you’ll be going to the Nodesource website. There you will find these instructions:

Node.js v10.x:

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs

Once you have done the above out of the blue pops up the proper yarn instructions which you might want to proceed with. It seems alas that the default apt version of Yarn on Ubuntu is also an issue so follow the guide instead of apt.

References:
https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions-enterprise-linux-fedora-and-snap-packages

The wild goose chase goes to:
https://github.com/nodesource/distributions/blob/master/README.md

Tags

Share this article

Leave a Reply

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

Scroll to Top