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.

11 Answers 11

up vote 915 down vote accepted

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

Found in David Walsh blog http://davidwalsh.name/upgrade-nodejs

  • 10
    this should be the correct answer. n is a node helper package that installs or update a given node.js version. – Martin Oct 2 '14 at 12:38
  • 35
    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 '15 at 22:30
  • 5
    I tried this... now when I run node I get this error bash: /usr/bin/node: No such file or directory what should I do ? – Yerko Palma Oct 11 '15 at 0:44
  • 5
    but still nodejs --version sow 0.10.25 – Mansuu.... Dec 29 '16 at 19:30
  • 8
    David Walsh says use NVM instead – Ads20000 Jan 12 '17 at 12:15

Complete installation instructions have since been uploaded here by Nodesource. It is copied below for your reference. Instructions are the same for updating to the latest version.

Node.js v10.x:

NOTE: If you are using Ubuntu Precise or Debian Wheezy, you might want to read about running Node.js >= 6.x on older distros

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_10.x | bash -
apt-get install -y nodejs

Node.js v9.x:

NOTE: If you are using Ubuntu Precise or Debian Wheezy, you might want to read about running Node.js >= 6.x on older distros

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_9.x | bash -
apt-get install -y nodejs

Node.js v8.x:

NOTE: If you are using Ubuntu Precise or Debian Wheezy, you might want to read about running Node.js >= 6.x on older distros

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_8.x | bash -
apt-get install -y nodejs

Node.js v7.x:

NOTE: Debian Wheezy and Ubuntu Precise packages are NOT available for this release. Please reference running Node.js >= 4.x on older distros

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_7.x | bash -
apt-get install -y nodejs

Node.js v6.x:

NOTE: If you are using Ubuntu Precise or Debian Wheezy, you might want to read about running Node.js >= 4.x on older distros.

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_6.x | bash -
apt-get install -y nodejs

Node.js v5.x:

NOTE: If you are using Ubuntu Precise or Debian Wheezy, you might want to read about running Node.js >= 4.x on older distros.

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_5.x | bash -
apt-get install -y nodejs

Node.js v4.x:

NOTE: If you are using Ubuntu Precise or Debian Wheezy, you might want to read about running Node.js >= 4.x on older distros.

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_4.x | bash -
apt-get install -y nodejs

Node.js v0.12:

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_0.12 | bash -
apt-get install -y nodejs

Node.js v0.10:

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_0.10 | bash -
apt-get install -y nodejs

io.js v3.x:

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_iojs_3.x | bash -
apt-get install -y iojs

io.js v2.x:

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_iojs_2.x | bash -
apt-get install -y iojs

io.js v1.x:

Note: this branch of io.js is not actively maintained and is not recommended for production use.

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_iojs_1.x | bash -
apt-get install -y iojs
  • 2
    upvote for the update, thanks! – Roy Ling May 14 '15 at 6:22
  • I'd advise either removing the outdated information, or moving them to the bottom of the question, so that the current info is read first. – k0pernikus Dec 3 '15 at 16:05
  • @k0pernikus How's that? :p – dayuloli Dec 4 '15 at 2:44
  • Awesome :) I installed node4 yesterday after reading. It's outdated already ^^ – k0pernikus Dec 4 '15 at 19:04
  • 2
    This is the answer that worked for me! – eri0o Mar 12 '16 at 4:16

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
  • 11
    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 '14 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 '14 at 11:46
  • @Capaj nope. that didn't help... – deostroll Dec 24 '14 at 0:47
  • 5
    Don't recommend using this ppa any more, it seems to be rather out of date. – Kzqai Dec 2 '15 at 12:12

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

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

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

to download and install nvm

nvm install node

should download and install the latest version of node.

To update node later on just do

nvm install node
nvm alias default node
  • 2
    This should be the only accepted answer - in case you want to have a sane and up to date Node setup in Ubuntu. – José L. Patiño Aug 20 at 13:35

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 ;)

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
  • 2
    Are you installing node from scratch? The question was about updating not installing – Green Feb 24 '17 at 5:56

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

see all available nodejs versions

https://nodejs.org/dist/

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 scripts 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 execute 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

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.

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

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.

protected by Community Jul 4 '17 at 21:42

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.