Score:0

How to install npm with LTS version of Node.js?

bl flag

I don't understand why there is no simple way or explanation in the entire web on how to do this. sudo apt install npm installs npm with Node version 10.19.0, but I want to install the LTS version (16.13.0)

I tried following these steps to update to the LTS version of Node:

npm install n -g
n stable

And it does seem to install the LTS version, but the old version is not removed, and Ubuntu still uses version 10.19.0

So how can I either install npm with LTS Node, or at least install the LTS version and make Ubuntu use this version and not the older?

Eyeofpie avatar
in flag
_"there is no simple way or explanation in the entire web"_ - Did you check the [official Node docs](https://nodejs.org/en/download/)? I see instructions to download LTS on various distros.
Score:0
br flag

upgrade Node.js v14.4.0 to v16.13.2 on ubuntu 21.04

If you have recently upgraded your ubuntu to 21.04, you might have to upgrade Node.js to latest version. Follow these steps

check nvm list 
$: nvm list

upgrade to node.js 16.13.2

$: nvm install 16.13.2

after installation , check version by

$: node -v

Hope this helps :)

mickmackusa avatar
ve flag
Please see [Help/Formatting](https://askubuntu.com/help/formatting) to learn how to make numbered lists and better format your command lines. Please never include "hope this helps", "smileys", "have a nice day", "Gagan", or "let me know if this works for you" at the end of a post. These texts are never relevant to any readers -- they are pure noise on the page. You may [edit] your answer at any time and as many times as you wish.
Score:0
bl flag

Update: So I managed to change the version. You need to add the following command after the first two:

sudo n 16.13.0

So it goes like this:

npm install n -g
n stable
sudo n 16.13.0

And now I finally see:

user@ubuntu:~$ node -v
v16.13.0

And maybe this will help newbies like me in the future

Score:0
us flag

I ran into this problem myself a month ago. I also couldn't understand why a version of Ubuntu that was still actively supported would include versions of Node and npm that were not being supported. In the end I realized that along with wanting multiple versions installed simultaneously (e.g. LTS doesn't actually specify a single version - v16 is "Active LTS" and both v14 and v12 are currently "Maintenance LTS", and v16 comes with a version of npm which uses a different lockfileVersion than will be used by those who installed an LTS Node more than a few weeks ago). I was more comfortable installing the thousands of dependencies using an npm that did not have superuser privileges.

Since you felt that you'd looked at the entire web before coming here, you likely found the Node download page also mentioned in the comments, but (like me) didn't want to either manually check for security updates, or continue using an insecure version after they were released. Node does document how to do this in a page mentioned from the bottom of the Downloads page. You may or may not have found the npm install instructions, which partially disagree with the Node instructions despite it being difficult to use one without the other.

If you like Snaps then you can use these. As it was the only option listed specifically for Ubuntu, I started with this, but I don't like the way that snaps are installed as separate mounts and clutter my df output. I think I gave up when I needed to compare different versions and nvm was again recommended.

n is also mentioned in the package managers page but I didn't like the circularity of needing to install a different version of npm in order to install the version manager that would install the one that I wanted.

As was recommended in another answer, I eventually used nvm. It downloads and selects between the various versions of node for you, and has options to upgrade npm to a more recent version than ships with Node. It knows which the LTS versions are and lets you distinguish them by codename or version number. However, it is a shell script which will make things more difficult if you are using a non-POSIX shell. It doesn't have an Ubuntu or Debian package, but they have instructions for a git install which makes it easier to upgrade.

You complained that the apt version of Node is still installed when using n. This might be a problem if you install something else using apt that depends upon Node/npm, which might find the "system version" rather than the more recent one installed using n. Coming from the Python world this is why I never installed pip using apt: I would create virtual environments and use pip from within them. If I ever try to pip install something outside of a virtual environment then it fails because there is no system pip to install with. I use apt to install the different Python versions and get pip from them.

Score:0
in flag

Just use nvm to install and manage Node/npm versions. When you work on multiple projects, you'll definitely switch between different versions of Node, and nvm handles it smoothly. I would go as far as to say that it's a best practice.

In bash, you can install nvm like this:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

# when wget finishes, restart terminal and run this:
nvm install --lts && nvm use --lts

Edit: using the n package is fine too. Both nvm and n accomplish the same thing.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.