181

I was recently going through the version of node in my ubuntu 16.04 when node -v command was used it shows me version 6.9.1 but when nodejs -v it shows 6.9.2 previously before using this commands npm update command was used.

Now what's these difference in node -v and nodejs -v? and how to update to the latest LTS version of node/nodejs?

ankur
  • 1,959
  • 2
  • 9
  • 12

15 Answers15

372

To update, you can install n

sudo npm install -g n

Then just :

sudo n latest

or a specific version

sudo n 8.9.0
wal
  • 16,922
  • 8
  • 71
  • 105
Camille Gerin-Roze
  • 4,066
  • 1
  • 10
  • 15
  • 36
    You can also use `sudo n current` or `sudo n lts` if you want more guarantee of stability and don't need the latest bleeding-edge features. – thund Jun 08 '17 at 16:53
  • 13
    @thund I upvoted your comment, but actually `sudo n current` does not exist, I think you mean `sudo n stable` – musicformellons Jul 11 '17 at 13:55
  • 4
    For security reasons I would write `sudo n lts` – ksopyla Jul 24 '17 at 10:00
  • Excellent, sudo n current this also do the same :). Thank you all. – Kaushik Das Jul 25 '17 at 11:09
  • 1
    @Enrique : Did you install "n" with npm first ? what's your distro ? – Camille Gerin-Roze Oct 12 '17 at 19:41
  • With this solution only node version got updated and not nodejs version, how to get latest versions for both ? – vikramvi Dec 08 '17 at 16:02
  • If I refer to this post https://stackoverflow.com/questions/22457834/what-is-the-difference-between-node-vs-nodejs-command-in-terminal node is just a symbolic link for nodejs. You start your application with the node or nodejs command ? – Camille Gerin-Roze Dec 08 '17 at 16:06
  • this didn't work for me! it still installed only node8 in my ubuntu 18.04 – Akhil Surapuram Aug 16 '19 at 06:09
  • @AkhilSurapuram If you ran another version earlier, that old version is likely cached in your shell. Two ways: (1) use the full path (i.e. `/usr/local/bin/node`) or (2) try `hash node` which should reset the cache for the `node` command. You can also open a new shell, log out and back in, or reboot. – Alexis Wilke Dec 09 '20 at 16:46
  • using this way you will install a new version of node but you won't remove the old one, thus, using `sudo node` will refer to the new version, and `node` will refer to the old one – shamaseen Jul 06 '21 at 12:50
  • Does this work if I install `n` on Windows? – Jim Raynor Oct 13 '21 at 12:39
  • @JimRaynor according to this issue : https://github.com/tj/n/issues/511 it could work. I think you should try ! – Camille Gerin-Roze Oct 14 '21 at 06:01
210

According to official docs to install node on Debian and Ubuntu based distributions:

node v12 (Old)

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

node v14 (For new users: install this one):

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

node v15 (Current version):

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

Other older versions: Just replace the desired version number in the link above.

Optional: install build tools

To compile and install native packages

sudo apt-get install -y build-essential

To update node to the latest version just:

sudo apt update
sudo apt upgrade

To keep npm updated

sudo npm i -g npm

To find out other versions try npm info npm and in versions find your desired version and replace [version-tag] with that version tag in npm i -g npm@[version-tag]

And I also recommend trying yarn instead of npm

Developia
  • 3,800
  • 1
  • 27
  • 41
28

Using Node Version Manager (NVM):

Install it:

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

Test your installation:

close your current terminal, open a new terminal, and run:

command -v nvm

Use it to install as many versions as u like:

nvm install 8              # Install nodejs 8
nvm install --lts          # Install latest LTS (Long Term Support) version

List installed versions:

nvm ls

Use a specific version:

nvm use 8                  # Use this version on this shell

Set defaults:

nvm alias default 8        # Default to nodejs 8 on this shell
nvm alias default node     # always use latest available as default nodejs for all shells
Ahmad Abdelghany
  • 9,749
  • 5
  • 38
  • 34
15

Use n module from npm in order to upgrade node

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

To upgrade to latest version (and not current stable) version, you can use

sudo n latest

Undo :

sudo apt-get install --reinstall nodejs-legacy # fix /usr/bin/node sudo n rm 6.0.0 # replace number with version of Node that was installed sudo npm uninstall -g n

Mahak Choudhary
  • 973
  • 14
  • 13
14

Use sudo apt-get install --only-upgrade nodejs to upgrade node (and only upgrade node) using the package manager.

The package name is nodejs, see https://stackoverflow.com/a/18130296/4578017 for details.

You can also use nvm to install and update node.

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash

Then restart the terminal, use nvm ls-remote to get latest version list of node, and use nvm install lts/* to install latest LTS version.

nvm is more recommended way to install or update node, even if you are not going to switch versions.

apaderno
  • 26,733
  • 16
  • 74
  • 87
DarkKnight
  • 5,317
  • 2
  • 25
  • 35
4

Difference: When I first installed node, it installed as 'nodejs'. When I upgraded it, it created 'node'. By executing node, we are actually executing nodejs. Node is just a reference to nodejs. From my experience, when I upgraded, it affected both the versions (as it is supposed to). When I do nodejs -v or node -v, I get the new version.

Upgrading: npm update is used to update the packages in the current directory. Check https://docs.npmjs.com/cli/update

To upgrade node version, based on the OS you are using, follow the commands here https://nodejs.org/en/download/package-manager/

Siva Kiran
  • 1,589
  • 1
  • 10
  • 8
4

Please refer nodejs official site for installation instructions at the following link

https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions

Anyway, please find the commands to install nodejs version 10 in ubuntu below.

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
Naresh
  • 67
  • 6
3
sudo npm install npm@latest -g
Pang
  • 9,073
  • 146
  • 84
  • 117
Rubel Hossain
  • 2,097
  • 1
  • 19
  • 22
3

Try this:

Edit or create the file :nodesource.list

sudo gedit /etc/apt/sources.list.d/nodesource.list

Insert this text:

deb https://deb.nodesource.com/node_10.x bionic main

deb-src https://deb.nodesource.com/node_10.x bionic main

Run these commands:

curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -


sudo sh -c "echo deb https://deb.nodesource.com/node_10.x cosmic main /etc/apt/sources.list.d/nodesource.list"

sudo apt-get update

sudo apt-get install nodejs
Guile Garcia
  • 115
  • 1
  • 9
3

Run these commands:

sudo apt-get update
sudo apt-get install build-essential libssl-dev
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
source ~/.profile
nvm ls-remote
nvm install v9.10.1
nvm use v9.10.1
node -v
leopal
  • 4,238
  • 1
  • 27
  • 33
Anandhu Raj
  • 127
  • 6
1

Update latest version Nodejs :

  1. sudo npm cache clean -f

  2. sudo npm install -g n

  3. sudo n stable

1

Node.js Current:

It's work for me..

Using Ubuntu

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

Using Debian, as root

curl -fsSL https://deb.nodesource.com/setup_current.x | bash -
apt-get install -y nodejs
Eahiya
  • 524
  • 3
  • 7
1

I am also facing problem while going to install react app, so i found the solution,

npx create-react-app shodkk 

First install the npm latest version using

sudo npm install -g npm@8.4.1

So to install the node 16.x you need to go terminal and type

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

Doing this you install the node LTS that is 16.14.o at the time of writing this post.

Try this 2-3 times, also do the

sudo apt-get update

Then Now install the package using

sudo apt-get install -y nodejs

At last this helps you remove any unwanted package, that remains after updating that is depreciated and need not be there, So use autoremove command. sudo apt autoremove

So if like the post, Upvote and motivate me to write more, thanks, givingBack to the Community.

1

I am using Ubuntu 20.04.4 LTS and got the issue during upgrade node js. The current LTS version is 16.14.2 According to the NodeSource Node.js Binary Distributions document

Node.js v16.x:

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

sudo apt-get install -y nodejs

if you still get the issue you can also try the following:

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
apt-get update
sudo npm cache clean -f
sudo apt-get install -y nodejs
Hasinur
  • 186
  • 1
  • 6
0

Use n module from npm in order to upgrade node sudo npm cache clean -f sudo npm install -g n sudo n stable To upgrade to latest version (and not current stable) version, you can use sudo n latest

To undo: sudo apt-get install --reinstall nodejs-legacy # fix /usr/bin/node sudo n rm 6.0.0 # replace number with version of Node that was installed sudo npm uninstall -g n