Master Guide: Installing Node.js on Ubuntu and CentOS

Hello friend! Welcome to my comprehensive 2800+ word guide on getting the latest Node.js JavaScript runtime installed smoothly on your Ubuntu or CentOS system.

Whether you‘re looking to learn Node development or deploy production microservices, having Node properly setup makes everything easier.

We‘ll cover multiple methods for installing Node v12.x to v16.x, troubleshooting gotchas, using version managers, and some pros/cons of each approach. Strap in!

What is Node.js Exactly?

In case you‘re brand new, Node.js allow building server applications using JavaScript. Here‘s a quick background:

  • Created in 2009 by Ryan Dahl
  • Built on Chrome‘s high performance V8 JavaScript engine
  • Uses an event-driven, non-blocking I/O model
  • Makes it easy to create fast, scalable network apps
  • Has over 1 million reusable packages available via npm
  • Used by companies like Netflix, Uber, eBay, LinkedIn

Some key stats on the impressive growth of Node adoption:

  • Over 3+ million websites built with Node as of 2022
  • 162 million+ downloads in 2021, up from 100M+ in 2018
  • Millions of JavaScript developers worldwide
  • 291,00+ Node GitHub Stars and 5500+ Contributors

As you can see, Node.js has become extremely popular for crafting web applications APIs, microservices and more. And with its vibrant open source community, the future looks bright for Node too!

What We‘ll Cover

The goal of this extensive guide is to provide you with various methods for getting the latest Node releases installed on Ubuntu or RHEL based systems like CentOS.

We‘ll be looking specifically at:

  • Installing Node v12.x to v16.x on Ubuntu 16.04+ and 18.04+
  • Installing on CentOS 7, 8 and Red Hat Enterprise Linux 8
  • Using pre-built binaries from NodeSource repositories
  • Building and compiling from source code
  • Configuring the nvm Node version manager
  • Troubleshooting any install issues you may run into
  • Best practices for keeping Node.js updated and applying security patches

With the background out of the way, let‘s jump right into installing Node on Ubuntu!

Installing the Latest Node.js on Ubuntu 16.04+ / 18.04+

When it comes to installing Node.js on Ubuntu or Debian systems, you generally have two options:

  1. Use a pre-built binary distribution via a repository like NodeSource
  2. Compile and install Node from source code

Binary distributions provide deb/apt packages making installation straightforward. But compiling form source allows more customization and control.

We‘ll cover both methods next.

Using NodeSource Binary Distributions

The easiest way to get recent Node releases on Ubuntu is tapping into NodeSource‘s binary distributions.

NodeSource offers deb/apt repositories that contain up-to-date Node.js builds ready for easy installation.

Let‘s go through setting this up and installing Node v16.x from NodeSource.

First we need to configure the NodeSource repositories:

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

This will add the required apt configuration and GPG keys to verify the Node packages.

With the repository configured, we can install Node.js + npm in one go:

sudo apt install nodejs

The nodejs package contains the latest Node v16 release. Check it completed properly:

node -v
# v16.14.0

Easy as that! The npm CLI should also be available out of the box:

npm -v 
# 8.3.1

Using the NodeSource binary distributions provides hassle-free installs and upgrades as new Node versions release every month.

Possible Issues When Using Apt

Of course software installation doesn‘t always go smoothly!

Here are some possible errors and how to resolve them:

If you see GPG key errors try re-installing packages:

sudo apt --reinstall install curl gnupg

For metadata problems do an apt update:

sudo apt update

And general installation failures can usually be fixed via:

sudo apt clean
sudo apt autoremove

This cleans caches and removes unnecessary packages and dependencies.

With that your Node should be installed perfectly from the NodeSource binaries.

Use nvm For Easy Version Management

An alternative to installing Node globally is to use a version manager like nvm.

This gives you full control for installing any specific Node versions you require, and easily switch between them.

First install nvm itself:

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

Now you can install multiple versions such as the latest Node 16 and 14 LTS:

nvm install 16 14

And manage which version to use:

nvm use 16
node -v 
# v16.14.0

nvm use 14  
node -v
# v14.21.1

No need to ever uninstall as you can switch versions anytime.

This wraps up setting Node.js using the apt packages. Next we‘ll cover building and compiling from source code.

Installing Node.js on Ubuntu from Source Code

In addition to binary distributions, installing Node…