230

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

tonyf
  • 2,627
  • 5
  • 17
  • 19

19 Answers19

269

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
muru
  • 197,895
  • 55
  • 485
  • 740
23W
  • 2,980
  • 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 08:46
  • Appreciate the response 23W so how would I install latest stable 0.12.4 using NodeSource? – tonyf Jun 13 '15 at 15:54
  • @tonsils I have added the example code. – 23W Jun 15 '15 at 10:35
  • Worked perfectly. – tonyf Jun 22 '15 at 09:50
  • 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
  • ^ I resinstalled using nodesource and it worked! (apt-get remove --purge nodejs npm then redo the above) – Daithí Jun 28 '15 at 11:53
  • @Coombesy Try reading script at that link and adding links to /etc/apt/sources.list.d/nodesource.list file manually. – Ivan Anishchuk Sep 07 '15 at 12:38
  • Did something happen to nodesource? The website / github repo seems fine, but the curl command gives W: Failed to fetch https://deb.nodesource.com/setup_5.x/dists/wily/main/binary-amd64/Packages HttpError404 – Jorn Mar 22 '16 at 17:30
  • 49
    Curling into root shell is so wrong in so many ways. – Draco Ater Apr 12 '16 at 12:36
  • 1
    This installs it as nodejs instead of node. You can symlink it to node by running: sudo ln -s "$(which nodejs)" /usr/sbin/node – Eugene Kulabuhov Aug 24 '16 at 22:21
  • 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
  • Another formal source for instructions: https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions – AlikElzin-kilaka Jan 15 '17 at 16:08
  • This is such a dangerous idea. There are so many ways this could go wrong; you should never curl arbitrary data into a root shell. You could so easily compromise your system. Guss' answer should be accepted. – Keefer Rourke Apr 11 '17 at 23:50
  • 1
    ok, but what about the latest version of npm? – Alexander Mills May 03 '17 at 21:32
  • 2
    @AlexanderMills the answer provided below (https://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 05 '17 at 03: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 – Bastian Voigt May 17 '17 at 09:52
179

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

And here's the "post deprecation of apt-key way" of doing the same thing:

curl https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/nodesource.gpg
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_7.x $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/nodejs.list
sudo apt update
sudo apt 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.

Guss
  • 3,535
  • 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 '15 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 '15 at 14:53
  • The commands need sudo, and even then, I get node 0.10.25 installed, not 5.5.0, the current version. Failed to fetch https://deb.nodesource.com/node_5.x/dists/utopic/main/binary-amd64/Packages HttpError404 – Dan Dascalescu Feb 08 '16 at 17:54
  • 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 08 '16 at 18:24
  • This is also helpful when trying to ansibilize your dev box. I'm not going to curl a bash script to get the appropriate ppa for that... Thanks so much! – Josh Nov 26 '16 at 21:44
  • Good answer, but....where's the bridge? – Paul Draper Aug 14 '17 at 00:41
  • 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 03: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
  • also consider changing npm's folder, to avoid having to sudo when installing global packages: https://docs.npmjs.com/getting-started/fixing-npm-permissions#option-2-change-npms-default-directory-to-another-directory – Mihai Rotaru Nov 26 '17 at 13:37
  • Ooh, thank You so much! I've got an error, when try to install nodejs into root shell. And only Your way worked for me! Thanks! ;) – Victor Aug 16 '18 at 10:37
37

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

PowerKiKi
  • 814
Yosvel Quintero
  • 479
  • 1
  • 8
  • 8
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.

serv-inc
  • 3,059
  • 1
    I've add a NVM step-by-step at: https://askubuntu.com/a/971612/52975 – Ciro Santilli OurBigBook.com Nov 01 '17 at 09: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 06:53
11

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

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

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
GuySoft
  • 774
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
karel
  • 114,770
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.

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
842Mono
  • 9,790
  • 28
  • 90
  • 153
3

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

node --version
v4.2.3
Jakuje
  • 6,605
  • 7
  • 30
  • 37
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

Melebius
  • 11,431
  • 9
  • 52
  • 78
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
ATOzTOA
  • 330
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

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/

muru
  • 197,895
  • 55
  • 485
  • 740
amurrell
  • 211
  • 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
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

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

MC Naveen
  • 317
  • 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

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

MC Naveen
  • 317