221

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

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

19 Answers 19

268

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
16
  • 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, 2015 at 11:42
  • 48
    Curling into root shell is so wrong in so many ways.
    – Draco Ater
    Apr 12, 2016 at 12:36
  • 8
    Answer by Guss below should have been the accepted answer. This is not the recommended way to do it. Aug 30, 2016 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. May 5, 2017 at 3:39
  • 2
    It is a little bit irritating to me to download a bash script from some website that might be hacked and run it with root permissions May 17, 2017 at 9:52
174

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.

10
  • 3
    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, 2015 at 13:59
  • 19
    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, 2015 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, 2016 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, 2017 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 Nov 26, 2017 at 13:26
36

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

1
  • 13
    Curling into root shell is so wrong in so many ways.
    – Draco Ater
    Apr 12, 2016 at 12:36
28

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
sudo npm install npm@latest

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.

3
  • 1
    I've add a NVM step-by-step at: askubuntu.com/a/971612/52975 Nov 1, 2017 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, 2017 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, 2017 at 6:53
10

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

8
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

8

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

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

2
  • Why install build-essential? Nov 29, 2016 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 Nov 29, 2016 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, 13, 14, 15, 16, 17 and 18 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 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 Status Codename Initial release LTS Start Maintenance Start Maintenance End
6.x EOL Boron 2016-04-26 2016-10-18 2018-04-30 2019-04-30
7.x EOL 2017-05-30 2017-06-30
8.x EOL Carbon 2016-10-25 2017-10-31 2019-01-01 2019-12-31
9.x EOL 2017-10-01 2018-06-30
10.x EOL Dubnium 2018-04-24 2018-10-30 2020-05-19 2021-04-30
11.x EOL 2018-10-23 2019-06-01
12.x Maintenance LTS Erbium 2019-04-23 2019-10-21 2020-11-301 2022-04-30
13.x EOL 2019-10-22 2020-06-01
14.x Maintenance LTS Fermium 2020-04-21 2020-10-27 2021-10-30 2023-04-30
16.x Active LTS Gallium 2021-04-20 2021-10-26 2022-10-18 2024-04-30
17.x Current 2021-10-19 2022-04-01 2022-06-01
18.x Current 2022-04-19 2022-10-25 2023-10-18 2025-04-30
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
3

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
3
# 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
  • For me, in Ubuntu 18.04, cd /usr worked instead of cd /usr/local.
    – Miron
    Dec 25, 2019 at 2:58
  • @Miron /usr would work but you should install software manually to /usr/local for maintainability. See /usr/bin vs /usr/local/bin on Linux for details.
    – Melebius
    Sep 30, 2020 at 10:16
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
  • 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, 2016 at 22:28
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

1
  • 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, 2018 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

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