198

I noticed over at the https://nodejs.org/ website that node is currently at v 0.12.0.

Can someone let me know how to install the latest version of node together with npm (terminal commands please)?

  • 1
    What's wrong with sudo apt-get install nodejs? – AlikElzin-kilaka Apr 4 '16 at 7:29
  • 20
    ^ it installs 1.10.0. – siliconrockstar May 7 '16 at 3:10
  • 6
    @siliconrockstar that actually installed v0.10.25 on my 14.04 LTS – Alessandro Cosentino Jan 3 '17 at 17:00
  • 1
    In ubuntu 16 use sudo apt-get install nodejs-legacy – briankip Apr 5 '17 at 9:45

19 Answers 19

254

Fresh installation

Use the NodeSource PPA. For details look at the installation instructions. First, choose the Node.js version you need and add the sources for it:

v=8   # set to 4, 5, 6, ... as needed
curl -sL https://deb.nodesource.com/setup_$v.x | sudo -E bash -

Then install the Node.js package.

sudo apt-get install -y nodejs

P.S.: curl package must be installed on server for these code lines.

Upgrading

If you have nodejs already installed and want to update, then first remove current instalation and install it again using scripts above.

sudo apt-get purge nodejs npm
  • 1
    NodeSource use two separate PPA: one for 0.10.X version and for 0.12.X. In addition, it contains last npm package. – 23W Jun 12 '15 at 8:46
  • 11
    didn't work for me. After running curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash - then installing nodejs I ended up with v0.10 – Daithí Jun 28 '15 at 11:42
  • 45
    Curling into root shell is so wrong in so many ways. – Draco Ater Apr 12 '16 at 12:36
  • 8
    Answer by Guss below should have been the accepted answer. This is not the recommended way to do it. – Patrick Fabrizius Aug 30 '16 at 22:38
  • 2
    @AlexanderMills the answer provided below (askubuntu.com/a/711976/389358) will get you the latest version of nodejs from NodeSource -- this package includes the latest version of npm as well. – Keefer Rourke May 5 '17 at 3:39
160

Generally speaking, loading arbitrary data from a URL into a root shell session is not a good idea and I wish people would stop peddling it as a solution for everything - "Please just run this script I'm sending you, and also while we're at it - I have a bridge you'd probably be interested in purchasing".

As an alternative, here's the "Ubuntu Way" of doing the same, where you can see how the system is being updated and know what repositories and what keys are added to your system configuration:

curl https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -
sudo apt-add-repository "deb https://deb.nodesource.com/node_7.x $(lsb_release -sc) main"
sudo apt-get update
sudo apt-get install nodejs

This is for the latest (at time of writing) Nodejs version 7. Other versions can also be gotten with a simple change to the repo URL - consult nodesource.com documentation for details.

  • 2
    I totally agree with your statement, but isn't adding a new repository to apt, the same as running some unknown script? (Though I understand the script in every other answer here, is actually adding that repo by itself) – Dan Dec 21 '15 at 13:59
  • 16
    The script is adding the repo by itself, and also doing god knows what. It may be completely benign now, but who knows what DNS hijackers or web site exploiters will do in the future? Adding a repo is not the same as running a script of the internet because by itself it does not download any executable code or runs it - it will only do so in response to a specific apt-get install, which will verify the package was signed with a the nodesource key - which is hopefully kept much more securely then the nodesource domain or website. APT security is excellent and has a good track record. – Guss Dec 21 '15 at 14:53
  • 1
    @Dan, It appears you are running Ubuntu 15.04 (utopic) which is not supported by Node Source, so you get 0.10 from utopic's repositories. Node Source only support LTS releases and the latest non-LTS. Because you are not on an LTS release you're expected to upgrade to the latest version when it came out last October. So either do that, or hack it by changing the $(lsb_release -c) release autodetection bit to one of the supported releases and deal with the consequences. – Guss Feb 8 '16 at 18:24
  • 2
    @PaulDraper: Ask nodesource, they're the ones selling bridges they don't own. My apt-get solution is the straight dope. – Guss Aug 14 '17 at 3:53
  • 1
    awesome answer ! in case anyone else is wondering, it just works for newer versions - e.g: just replace 7 with 9 – Mihai Rotaru Nov 26 '17 at 13:26
33

Node.js v4.x:

# 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

source: https://github.com/nodesource/distributions#debinstall

  • 13
    Curling into root shell is so wrong in so many ways. – Draco Ater Apr 12 '16 at 12:36
24

If you want to update inside npm, you can use the n command:

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

This installs the n package which you can use to switch NodeJS-Versions and uses it. Comparison to the alternative NVM and command options are at SO. There is also a blog post.

  • 1
    I've add a NVM step-by-step at: askubuntu.com/a/971612/52975 – Ciro Santilli 新疆改造中心996ICU六四事件 Nov 1 '17 at 9:45
  • 1
    I'm pretty new to npm and nvm; I'm trying to install npm inside a python virtualenv; does it matter whether I use npm or nvm? nvm install lts/carbon installs npm and node for me. How is this different from the above commands which you shared? – kRazzy R Nov 13 '17 at 19:00
  • @kRazzyR: you get to choose whether to use nvm or n to update NodeJS and npm. In both cases, you use npm to install node packages. – serv-inc Nov 14 '17 at 6:53
7
curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -
# Then install with:
sudo apt-get install -y nodejs

Here you can find more info: Node.js v0.12, io.js, and the NodeSource Linux Repositories

7

For version 5.x According to PPA:

apt-get remove --purge nodejs npm
curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
apt-get install nodejs
7

To install NPM,

sudo apt-get install npm

Then for Node,

sudo npm cache clean -f
sudo npm install -g n
sudo n 0.xx.x  // here is the version what you want.. 

This command will install node based on your version you want..

6

NVM (Node Version manager)

https://github.com/creationix/nvm

NVM installs both the latest stable node and npm for you

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, you will probably want to add the following to your .bashrc:

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

Tested in Ubuntu 17.10.

4

You can install latest version very easily using below instruction.

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

Vesion 7.x is the latest version of node.

sudo apt-get install nodejs

Above line will install nodejs.

sudo apt-get install build-essential

This will install essential modules for nodejs to run properly.

Now check whether nodejs installed correctly at your end

nodejs -v

This will return installed nodejs version.

npm -v

This will return installed npm version. Hope it helps....

Source : link will show you how to install nodejs using some other methods as well.

  • Why install build-essential? – David Foerster Nov 29 '16 at 14:30
  • The nodejs package contains the nodejs binary as well as npm. However, in order for some npm packages to work (such as those that require building from source), you will need to install the build-essentials package – Jitendra Pawar Nov 29 '16 at 17:35
4

Node.js is available as a snap package in all currently supported versions of Ubuntu. Specific to Node.js, developers can choose from one of the currently supported releases and get regular automatic updates directly from NodeSource. Node.js versions 6, 8, 9, 10, 11 and 12 are currently available, with the Snap Store being updated within hours, or minutes of a Node.js release.

Node can be installed with a single command, for example:

sudo snap install node --classic --channel 11/stable 

The node snap can be accessed by the command node, for example:

$ node -v  
v11.5.0

An up-to-date version of npm will installed as part of the node snap. npm should be run outside of the node repl, in your normal shell. After installing the node snap run the following command to enable npm update checking:

sudo chown -R $USER:$(id -gn $USER) /home/your-username/.config

Replace your-username in the above command with your own username. Then run npm -v to check if the version of npm is up-to-date. As an example I checked that npm was up-to-date, checked the version of an already installed package named yarn with the command npm list yarn and then updated the existing yarn package to the latest version with the command npm update yarn

Users can switch between versions of Node.js at any time without needing to involve additional tools like nvm (Node Version Manager), for example:

sudo snap refresh node --channel=11/stable

Users can test bleeding-edge versions of Node.js that can be installed from the latest edge channel which is currently tracking Node.js version 12 by switching with:

sudo snap switch node --edge

This approach is only recommended for those users who are willing to participate in testing and bug reporting upstream.

Node.js LTS Schedule

Release LTS Status  Codename    LTS Start       Maintenance Start Maintenance End
6.x     Active      Boron       2016-10-18      April 2018        April 2019
7.x     No LTS              
8.x     Active      Carbon      2017-10-31      April 2019        December 2019
9.x     No LTS              
10.x    Active      Dubnium     October 2018    April 2020        April 2021  
11.x    No LTS                                  2019-04-01        2019-06-30
12.x    No LTS                  2019-10-22      2021-04-01        2022-04-01
3

For Ubuntu 15.10 you can download the .deb package form packages.ubuntu.com

node --version
v4.2.3
3

Just a spin-off of the answer by @23W (accepted answer).

This is just the contents of the script, just to avoid doing curl to root shell.

curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -
echo "deb https://deb.nodesource.com/node_6.x $(lsb_release -c -s) main" | sudo tee /etc/apt/sources.list.d/nodesource.list
echo "deb-src https://deb.nodesource.com/node_6.x $(lsb_release -c -s) main" | sudo tee -a /etc/apt/sources.list.d/nodesource.list

sudo apt-get update
sudo apt-get install -y nodejs
3

Download nodejs from https://nodejs.org/

To install from a terminal:

cd /usr/local
tar --strip-components 1 -xJf ~/Downloads/node-v4.4.5-linux-x64.tar.xz
2
//Download
cd ~/Downloads
wget https://nodejs.org/dist/v4.4.5/node-v4.4.5-linux-x64.tar.xz

//Install
cd /usr/local
tar --strip-components 1 -xJf ~/Downloads/node-v4.4.5-linux-x64.tar.xz

//Verify
node -v
npm version

Source

2

My favorite way to install latest nodejs and npm is using the installation binaries archive provided on nodejs' website.

This might quickly become your new favorite way too. Works like a charm. It involves 1 download and 1 command. Done. NO errors [the multiple times I've done this]. Didn't need to uninstall existing stuff beforehand.

Installing nodejs via Binaries


At first remove the installed version: (if exists)

sudo apt-get remove nodejs

Download

https://nodejs.org/en/download/stable/

Download Linux Binaries.

  • I had to extract the tar.xz to a folder and then re-compress as tar.gz

Install

sudo tar -C /usr/local --strip-components 1 -xzf /path/to/downloaded/tar/node-vYOURVERSION-linux-x64.tar.gz

Verify

node -v
npm --version

Source

http://www.thegeekstuff.com/2015/10/install-nodejs-npm-linux/

  • 1
    Why re-compress as tar.gz? tar supports --xz - and -C /usr/local to let tar do the cd for you. – muru Apr 14 '16 at 22:28
2

Video Explanation


NVM is very simple to install and allows you to easily switch node versions. From the github repository:

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

downloads and runs the installation script

nvm ls-remote

to see the available versions. Say you want to install version 8.9.4

nvm install 8.9.4

and you're done.

To install another version and use it (say 9.6.1)

nvm install 9.6.1
nvm alias default 9.6.1
1

Update Package Manager

sudo apt-get update

Adding NodeJS PPAs

sudo apt-get install python-software-properties
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

Installing NodeJS and NPM

sudo apt-get install nodejs

Here, I Wrote a Complete Blog post about Installing NodeJS and NPM in Ubuntu. Click Here to Read

  • Curling into a root shell is a really bad thing to do in general do to the huge number of security issues this opens up, I would recommend that you update your blog post and your answer to not do this. – shuttle87 Jul 13 '18 at 11:35
1

This approach allows you easily delete Node.JS, keep different versions and provide the commands to all the system users.

Download the stable version of Node.JS, uncompress it, and move it into /opt.

In case you wish to install different versions, you can rename the folder according to its version code:

/opt/node/6.9.1

Since only root can change files in /opt/, if you don't wish to call invoke sudo every time you need to modify a file, change the group permission:

chgrp adm -R /opt/node/10.15.3

Then create file /etc/profile.d/node.sh if the following content:

export NODE_HOME=/opt/node/10.15.3

export CPLUS_INCLUDE_PATH=${NODE_HOME}/include
export C_INCLUDE_PATH=${NODE_HOME}/include
export LD_LIBRARY_PATH=${NODE_HOME}/lib
export MANPATH=${NODE_HOME}/share/man:${MANPATH}

export PATH=${NODE_HOME}/bin:$PATH

Logout and login and X-Window will reload all profile configurations.

If you had a previous versions installed, to avoid library conflicts run:

npm cache clean
-1

Updating Package Manager

sudo apt-get update

Adding PPA

sudo apt-get install python-software-properties

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

Installing NodeJS and NPM

sudo apt-get install nodejs

For Reference : Installing NodeJS in Ubuntu - WebCheerz

protected by Community Nov 29 '16 at 18:58

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.