231

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

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
  • 207,228
23W
  • 2,990
180

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,775
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
  • 954
29

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,159
  • 1
  • 26
  • 32
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
  • 784
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
  • 122,292
  • 133
  • 301
  • 332
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
  • 10,070
3

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

node --version
v4.2.3
Jakuje
  • 6,793
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,750
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
  • 207,228
amurrell
  • 221
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
  • 337
-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
  • 337