The Nodesource Blog

How to Install Node.js on Ubuntu: A Tutorial

Installing Node.js is always going to be the first step to getting up and running with building applications with Node.js.

In this tutorial, I'm going to show you how to get Node.js installed quickly on Ubuntu. Once completed, you'll have Node.js installed and be ready to start building!

This guide covers installing Node on both the Ubutntu Desktop and Ubuntu Server distributions of Ubuntu, for any of the following versions: Precise Pangolin (12.04), Trusty Tahr (14.04), Xenial Xerus (16.04), Yakkety Yak (16.10)

Step 0: TL;DR - Quick and Instant Install

If you just want to get Node.js installed on Ubuntu (specifically the Precise, Trusty, or Xenial distributions), and don’t need anything else other than the quick how, here are the two commands you need to run.

# Adding the NodeSource APT repository for Debian-based distributions repository AND the PGP key for verifying packages
$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

# Install Node.js from the Debian-based distributions repository
$ sudo apt-get install -y nodejs

Step 1: Environment Setup - Ensure your system meets the needs of the install

To install Node.js on Ubuntu, you will need to ensure that your system meets following requirements:

Your environment is on a supported architecture:

  • i386 (32-bit)
  • amd64 (64-bit)
  • armhf (ARM 32-bit hard-float, ARMv7 and up: arm-linux-gnueabihf)

Your environment is using a supported Ubuntu LTS distribution:

  • Ubuntu 12.04 LTS (Precise Pangolin)
  • Ubuntu 14.04 LTS (Trusty Tahr)
  • Ubuntu 16.04 LTS (Xenial Xerus)
  • Ubuntu 16.10 (Yakkety Yak)

Step 2: Downloading - Setting Up the Correct APT Repository

The first part of downloading the correct Node.js binary is to add the APT repository for Node.js Debian-based and add the associated PGP key to the APT keychain.

This step is necessary, as the default APT repository that gets called when you run apt-get install nodejs is severely outdated - it defaults to a Node.js v4 release line that is missing several large security updates.

To set up the current APT repository and add the PGP key to the system’s APT keychain, you need to run this command:

# Download and setup the APT repository add the PGP key to the system’s APT keychain,
$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

This script completes both the addition of the apt-get repository and adds the PGP key. The Node.js Debian-based distributions are hosted and signed by NodeSource, and this script - including its current state, its commit history, and the companion scripts for previous versions of Node’s Debian installers - are all publicly available on GitHub. If you want to take a deep dive into understanding how it’s accomplished, go check them out!

Step 3: Running apt-get to Install Node.js

Once we have the APT repository set-up, as instructed in Step 2, we can finally install Node with apt-get. To do so, you will need to run the following command:

$ sudo apt-get install -y nodejs

Once this install is run to completion, should have been successfully installed on the system!

Step 4: Confirm Node.js was Successfully Installed

To confirm that Node.js was successfully installed on your system, you can run the following command:

$ node -v

If Node is installed, this command should print out something close to (but probably not exactly) this:

$ node -v // The command we’re running
v6.9.1

Step 5: Finally, Update Your Version of npm

One last step that’s a good practice is to update npm. There’s a default version that came with the version of Node you just installed, but that version is commonly slightly behind the latest and greatest version of npm (because of the different release cycles of the two projects).

To get the most up-to-date npm, you can run the command:

$ sudo npm install npm --global

Step 6: Start using Node!

Now it’s time to go explore Node.js! We’ve got a suite of guides to get you up and running with some different aspects of working with and deploying Node.js, including a set of awesome ES6 features in Node.js, a guide on how to lint your Node.js apps with ESLint, a two-part tutorial on running Node.js with systemd, and plenty more!

If you want to keep up to date with what’s going on in Node.js, you should follow @NodeSource on Twitter! We’ll keep you up-to-date on the latest and greatest Node.js updates, and are always updating you with the best Node.js tutorials, guides, and tools!