1115

I have installed nodeJS on Ubuntu with following code

sudo apt-get install nodejs

Since I am a new user for ubuntu I also ran this code too

sudo apt-get install npm

Now when I type

 nodejs --version

It shows

v0.6.19

I checked and saw latest nodeJS version is 0.10.26

How can I update my version of nodeJS to 0.10.26?

I tried with

 sudo apt-get install <packagename>
 sudo apt-get install --only-upgrade <packagename>

but no luck.

1
  • 6
    It's better to use nvm to install nodejs. It stands for "Node Version Manager". You can very easily install multiple versions of nodejs, switch between them and set a default.
    – 842Mono
    Nov 17, 2018 at 16:11

20 Answers 20

1848

CAUTION: This method of upgrading node is now unstable and should not be used. The best way to manage Node.js versions is to use NVM: Node Version Management.! Refer: David Walsh's post on NVM

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
  • Fix PATH:

      sudo apt-get install --reinstall nodejs-legacy     # fix /usr/bin/node
    
  • To undo:

      sudo n rm 6.0.0     # replace number with version of Node that was installed
      sudo npm uninstall -g n
    

You may need to restart your terminal to see the updated node version.

Found in David Walsh blog

42
  • 78
    I would advise strongly against doing this on a production instance. It can seriously mess stuff up with your global npm packages and your ability to install new ones.
    – Loourr
    Feb 19, 2015 at 22:30
  • 22
    but still nodejs --version sow 0.10.25
    – Mansuu....
    Dec 29, 2016 at 19:30
  • 24
    David Walsh says use NVM instead
    – Ads20000
    Jan 12, 2017 at 12:15
  • 30
    Not working - after following the steps, node -v still gave me the 6 version, not 10. Jun 8, 2018 at 13:17
  • 42
    npm WARN using --force I sure hope you know what you are doing. I don't know what I am doing... Mar 12, 2019 at 4:28
659

NodeSource provides binary distributions of Node.js; complete installation instructions can be found here. The instructions have been copied below for your reference. Instructions are the same for updating to the latest version.

Run once:

sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

Run whenever you want to change the major version of Node.js:

NODE_MAJOR=20
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
sudo apt-get update
sudo apt-get install nodejs -y

Prior to August 2023: Previously versions of this answer involved the use of a series of setup_XX.x scripts that you'd run to install/update Node.js:

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

But as @eis pointed out, these scripts are no longer supported. To see the previous answers, please look at the edit history for this answer.

20
  • 7
    This is installation instructions not upgrade instructions. Installation implies that it's never been installed before. If you already have a version installed as the question implies, then what happens if you install a different version? Do you get version conflicts? Does the installation delete the old one?
    – icc97
    Jan 23, 2019 at 11:13
  • 3
    For all confused idiots like me, it seems that running the installation will effectively replace what ever you had before. So I think you can perform either an upgrade or a downgrade with this process. I upgraded from v8 to v10 and I spotted this message in the install logs: Unpacking nodejs (10.15.0-1nodesource1) over (8.12.0-1nodesource1) ..., running node -v after the installation happily displays v10.15.0 which is what I want.
    – icc97
    Jan 23, 2019 at 11:19
  • 5
    curl {anything} | sudo bash - is a terrible security practice
    – krubo
    Jun 26, 2019 at 11:26
  • 1
    @krubo I agree. The user should examine the script before running it. However, it is from a reputable source (nodesource.com) and the connection is secured by HTTPS, so there is an adequate amount of trust you can place in those scripts. But yes, in general, check the scripts before you pipe it to sudo bash
    – dayuloli
    Jun 26, 2019 at 19:18
  • 1
    "deb.nodesource.com" says it the script is deprecated so do not use this.
    – eis
    Nov 4, 2023 at 13:22
99

I also recommend using nvm instead, and also removing the already installed version to avoid conflicts in the terminal

sudo apt purge nodejs npm

then install nvm and use it

Video Explanation

to download and install nvm:

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

to download and install the latest version of node:

nvm install node

To update node later on, just do:

nvm install node
nvm alias default node
8
  • 15
    This should be the only accepted answer - in case you want to have a sane and up to date Node setup in Ubuntu. Aug 20, 2018 at 13:35
  • 1
    This also works the best for me (have pretty ancient nodejs on our EC2 and tried lots of suggestions and only this one made it work). Also 100% agree with the comment above!
    – Shan Dou
    Jun 12, 2019 at 5:26
  • 3
    nvm installs Node.js on a per-user basis. Using nvm will not give you a system-wide node command. So I would advice against removing the global/system node so scripts running as another user can still run node.
    – d4nyll
    Jul 25, 2019 at 21:42
  • 1
    While nvm can be pretty handy, be cautious that it increases bash startup time dramatically.
    – extraymond
    Jun 28, 2020 at 14:54
  • 1
    It is better to use nvm install --lts to get the long-term support version. nvm install node willl install the latest Current version. ` Jun 11, 2021 at 22:25
27

I use NVM to handle my Node versions. Very simple to set up and easy to use.

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

To install NVM globally instead, use the following curl command instead of the one above (and presumably don't use the second command but do use the third one)

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | NVM_DIR=/usr/local/nvm bash

Then use nvm install stable (or insert a version number instead of stable) to get the latest/a specific version of Node. Use nvm use stable (or a specific version number) to use that Node version. Use nvm ls to see what Node versions you have installed and nvm uninstall stable(or a specific version number) to remove a specific version of Node.

Sources: Install, usage

Note

If you are struggling with updating npm to the latest stable version because you are stuck at a specific version of npm and every time you update it that doesn't work then you can use this method to update npm as well. As soon as you install the latest stable version of node, npm will automatically be updated to its latest stable version.

4
27

Note: This PPA is out of date and not maintained anymore; you should consider other answers instead of this one.

You can install the latest version from PPA:

sudo add-apt-repository ppa:chris-lea/node.js  
sudo apt-get update  
sudo apt-get install nodejs
4
  • 17
    I think this would be a better answer if you (also) provided a non-ppa way of doing this, ppas are notoriously known for becoming outdated and unmaintained, essentially making the answer useless.
    – pzkpfw
    Feb 27, 2014 at 16:21
  • @bigbadonk420 yeah, if you just googled it, probably better to skip the first line and do just update and install-worked for me with node 0.10.34
    – Capaj
    Dec 22, 2014 at 11:46
  • @Capaj nope. that didn't help...
    – deostroll
    Dec 24, 2014 at 0:47
  • 8
    Don't recommend using this ppa any more, it seems to be rather out of date.
    – Kzqai
    Dec 2, 2015 at 12:12
14

NVM (Node Version manager) with --lts

NVM was mentioned at: https://askubuntu.com/a/441527/52975 but here goes a more complete usage example, including the sane --lts version, which installs the latest Long Term Support version of Node, which is likely the one you want if you don't have more specific requirement.

Install NVM and the latest Node LTS version:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.nvm/nvm.sh
nvm install --lts
nvm use --lts
npm --version

Now test it out with a dummy package:

npm install --global vaca
vaca

Since the sourcing has to be done for every new shell, the install script hacks adds some auto sourcing to the end of your .barshrc. That works, but I prefer to remove the auto-added one and add my own:

f="$HOME/.nvm/nvm.sh"
if [ -r "$f" ]; then
  . "$f" &>'/dev/null'
  nvm use --lts &>'/dev/null'
fi

Advantages:

  • allows you to use multiple versions of Node and without sudo

  • is analogous to Ruby RVM and Python Virtualenv, widely considered best practice in Ruby and Python communities

  • downloads a pre-compiled binary where possible, and if not it downloads the source and compiles one for you

We can easily switch node versions with:

nvm install 0.9.0
nvm install 0.9.9
nvm use 0.9.0
node --version
#v0.9.0
nvm use 0.9.9
node --version
#v0.9.9

You can then use a git tracked .nvmrc file to indicate the node version required for a given project: https://stackoverflow.com/questions/24869959/how-do-i-specify-a-local-version-of-node-for-a-project/54503474#54503474

With this setup, you get for example:

which node

gives:

/home/ciro/.nvm/versions/node/v0.9.0/bin/node

and:

which vaca

gives:

/home/ciro/.nvm/versions/node/v0.9.0/bin/vaca

and if we want to use the globally installed module:

npm link vaca
node -e 'console.log(require.resolve("vaca"))'

gives:

/home/ciro/.nvm/versions/node/v0.9.0/lib/node_modules/vaca/index.js

so we see that everything is completely contained inside the specific node version.

Tested in Ubuntu 17.10.

2
  • 1
    its worked for me
    – matinict
    Sep 18, 2020 at 3:59
  • 1
    Thank you! Worked for me May 18, 2021 at 6:17
6

CLEAN SIMPLE STEPS (which I use) :

  1. Uninstall previous version of node -
    sudo apt remove node && sudo apt autoremove

  2. Go to https://nodejs.org/en/ and download the latest version of node.

  3. Now open the terminal and change the directory where node is downloaded and then run-
    tar -xvf node-v12.16.2-linux-x64.tar.xz

  4. now run this in terminal -
    nano ~/.bashrc

  5. and append it with the following –
    export PATH=/path/to/node-js/bin:$PATH
    Where, /path/to/node-js/ is to be replaced where you extracted node in step 3.`

  6. run a last command source ~/.bashrc

That's it.
Now you can check your node version by node -v

4

I tried the same list commands on my ubuntu 14.04 but it was still throwing an error.

Commands I executed were:

sudo add-apt-repository ppa:chris-lea/node.js  
sudo apt-get update  
sudo apt-get install nodejs

and the error i was getting:

Invalid version 0.12.2
Line 299: curl not found in /bin/n

What I figured out was curl utility was not installed on my os.

I executed command:

apt-get install curl

(use sudo as prefix if you are not su)

and then repeated the steps suggest in answer and it work ;)

2

This one installs the latest node v0.12.* from nodesource.

sudo apt-get install -y curl
curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -
sudo apt-get install -y nodejs

For node v4.x

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs
1
  • 2
    Are you installing node from scratch? The question was about updating not installing
    – Green
    Feb 24, 2017 at 5:56
2

If using n does not work, you can install the latest version of nodejs (i. e. version 8) running the following commands:

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

sudo apt-get install -y nodejs

Further instructions are here.

2

Using nvm is preferred method. First install nvm:

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

Then install nodejs:

nvm install node

Now on, you can easy switch versions of node.

2

I tried to install via nvm, as described above, but faced some error in Ubuntu 18.04. Then I went through this process:

sudo apt purge nodejs npm

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

nvm install node

After running this command, you might get this error:

Command 'nvm' not found, did you mean:

In that case you can run this command:

source ~/.nvm/nvm.sh

OR you can put it in the /.bashrc or ~/.profile file to automatically load it.

After that you can run the following commands:

nvm install node
nvm alias default node
1

The selected answer did not work for me, I tried restarting terminal and PC but no success.

I have installed (as recommended in the docs) npm and node with node version manager. In this case, one can update node and npm simply with

nvm install stable

I explained here how to switch to reinstall with node-verion-manager if you have installed npm/node with Node installer.

1
  • the only answer that worked for me
    – Cris
    Jun 5, 2023 at 21:51
1

I suggest you first remove all nodejs installs then execute the below script once you edit with the desired nodejs version and its install parent directory

see all available nodejs versions

https://nodejs.org/dist/

The below script will allow you to install any of those nodejs versions on linux or OSX

#!/bin/bash

#     usage :
#
#     edit two vars in below :  NODE_VER  and  CODE_PARENT_DIR
#
# ... execute this script as yourself unless you choose a root-owned value for var CODE_PARENT_DIR
#     whichever id you execute this as determines the id you will issue npm commands as:  npm install -g foo-bar
#
#     NOTE - nodejs comes bundled with npm ... so no need to do separate npm install
#            This script runs fine on Linux or OSX

# ...  copy all the lines starting here  .. top of copy  ....   and ending ... end of copy  ...
#      and paste into your ~/.bashrc file so proper env vars get set 

# ............... top of copy  ........................  install_node.sh

# export NODE_VER=v7.2.0  # see available versions at https://nodejs.org/dist/
# export NODE_VER=v8.5.0  # edit this line next time you need to update nodejs  
export NODE_VER=v9.3.0  # edit this line next time you need to update nodejs

# ...  pick parent dir of nodejs install  ... comment out or remove ONE of below
# export CODE_PARENT_DIR=/opt/code  # root-owned dir ... requires you to sudo prior to npm install going forward
export CODE_PARENT_DIR=${HOME}    # RECOMMENDED executing as yourself including npm install

# ......... following env vars are OK no edits needed ... only ever need to edit above vars

curr_OS=$( uname )

echo curr_OS $curr_OS

if [[ "${curr_OS}" == "Darwin" ]]; then

    OS_ARCH=darwin-x64

elif [[ "${curr_OS}" == "Linux" ]]; then

    OS_ARCH=linux-x64
else
    echo "ERROR - failed to recognize OS $curr_OS"
    exit 5
fi

if [[ -z ${CODE_PARENT_DIR} ]]; then

    echo "ERROR - failed to see env var CODE_PARENT_DIR"
    exit 5
fi

export NODE_CODEDIR=${CODE_PARENT_DIR}/nodejs
export COMSUFFIX=tar.gz
export NODE_NAME=node-${NODE_VER}
export NODE_PARENT=${NODE_CODEDIR}/${NODE_NAME}-${OS_ARCH} 

export PATH=${NODE_PARENT}/bin:${PATH}
export NODE_PATH=${NODE_PARENT}/lib/node_modules

# ............... end of copy  ........................  install_node.sh

# copy and paste above from ... top of copy ... to here into your file ~/.bashrc   

echo
echo "NODE_CODEDIR $NODE_CODEDIR<--"
echo

echo "mkdir -p ${NODE_CODEDIR}"
echo
      mkdir -p ${NODE_CODEDIR}
echo

echo "cd ${NODE_CODEDIR}"
      cd ${NODE_CODEDIR}
echo

# this is compiled code NOT source

[ -f ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} ] && rm ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} # if file exists remove

echo "wget -q --show-progress https://nodejs.org/download/release/${NODE_VER}/${NODE_NAME}-${OS_ARCH}.${COMSUFFIX}"
      wget -q --show-progress https://nodejs.org/download/release/${NODE_VER}/${NODE_NAME}-${OS_ARCH}.${COMSUFFIX}
echo

echo "tar -C ${NODE_CODEDIR} -xf ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX}"
      tar -C ${NODE_CODEDIR} -xf ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX}
echo

[ -f ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} ] && rm ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} # if file exists remove

# ...........  done ........... #

which node

node --version

# ....  bottom of file   install_node.sh
1

Type following command:

sudo npm install n -g
sudo n stable
1

If you use Snap, this does the trick, quick and trouble free in my experience

sudo snap install node --classic
0

If you are behind a proxy, maybe you could get this error when you run 'sudo n stable':

Error: invalid version

You have to set the env variables like this:

export HTTP_PROXY=http://your-proxy-url:port
export HTTPS_PROXY=http://your-proxy-url:port

And then run the command passing these variables to the root user:

sudo -E n stable
1
0

Try this:

nvm install --lts

Installing latest LTS version.
Downloading and installing node v18.16.1...
Downloading https://nodejs.org/dist/v18.16.1/node-v18.16.1-linux-x64.tar.xz...
####################################################################### 100,0%
Computing checksum with sha256sum
Checksums matched!
Now using node v18.16.1 (npm v9.5.1)
Creating default alias: default -> lts/* (-> v18.16.1)

nvm alias default PUT_VERSION_HERE

e.g.:

nvm alias default v18.16.1

default -> v18.16.1

Confirm:

node --version
3
  • How did you install nvm or did you use the method in sayalok's answer?
    – karel
    Jul 5, 2023 at 9:40
  • This is not part of OP's question. It was already installed.
    – Black
    Jul 5, 2023 at 9:47
  • I know it's not part of the OP's question, but how else can I check your answer?
    – karel
    Jul 5, 2023 at 9:48
0

NVS (Node Version Switcher)

I like NVS because has feature for migrate current version to newer, include bringing globally installed packages to target update version

Install on Linux

export NVS_HOME="$HOME/.nvs"
git clone https://github.com/jasongin/nvs "$NVS_HOME"
. "$NVS_HOME/nvs.sh" install

More info:

0

NVM (Node Version Manager)

I found NVM is the simplest way to install and manage multiple versions of Node.js on Ubuntu. The command nvm list-remote lists all the available versions. You can install the required version by specifying the version details in a user-friendly way.

nvm install v18.19.1 --> this installs a specific version

nvm install lts/iron --> This installs the latest LTS in the Iron series

Source: How To Install Node.js on Ubuntu 22.04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .