799

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.

  • 4
    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. – Mina Michael Nov 17 '18 at 16:11

14 Answers 14

1348

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

| improve this answer | |
  • 57
    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
  • 7
    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
  • 15
    but still nodejs --version sow 0.10.25 – Mansuu.... Dec 29 '16 at 19:30
  • 12
    David Walsh says use NVM instead – Ads20000 Jan 12 '17 at 12:15
  • 21
    Not working - after following the steps, node -v still gave me the 6 version, not 10. – Tomáš Zato - Reinstate Monica Jun 8 '18 at 13:17
475

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.

If you don't like curl ... | sudo bash - then you can follow the manual instructions, which tells you how to add the Node.js repository to /etc/apt/sources.list.d/ yourself.

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

Node.js v13.x:

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

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

Node.js v12.x:

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

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

Node.js v11.x:

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

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

Node.js v10.x:

# 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:

# 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:

# 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
| improve this answer | |
  • For me node --version still said "0.12.0". I had to rm /usr/local/bin/node, then I was able to use the newly installed version of nodejs. – Chris Clark Jul 17 '16 at 16:21
  • 1
    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 '19 at 11:13
  • 1
    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 '19 at 11:19
  • 4
    curl {anything} | sudo bash - is a terrible security practice – krubo Jun 26 '19 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 '19 at 19:18
37

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
| improve this answer | |
  • 8
    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 '18 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 '19 at 5:26
  • 2
    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 '19 at 21:42
  • @d4nyll good remark, but it is mostly applied to multiuser systems. – Gryu Apr 2 at 14:17
  • 1
    While nvm can be pretty handy, be cautious that it increases bash startup time dramatically. – user12164 Jun 28 at 14:54
27

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
| improve this answer | |
  • 15
    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
  • 8
    Don't recommend using this ppa any more, it seems to be rather out of date. – Kzqai Dec 2 '15 at 12:12
21

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

| improve this answer | |
7

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 | sh
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.

| improve this answer | |
  • 1
    its worked for me – matinict Sep 18 at 3:59
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 ;)

| improve this answer | |
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
| improve this answer | |
  • 2
    Are you installing node from scratch? The question was about updating not installing – Green Feb 24 '17 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.

| improve this answer | |
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.

| improve this answer | |
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
| improve this answer | |
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
| improve this answer | |
0

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.

| improve this answer | |
0

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
| improve this answer | |

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