Skip to main content
  1. Tutorials/

How To Install Node.js on Ubuntu 18.04

Tutorials DigitalOcean App Platform Node.js Ubuntu 18.04
Introduction>

Introduction #

Node.js is a JavaScript platform for general-purpose programming that allows users to build network applications quickly. By leveraging JavaScript on both the front and backend, Node.js makes development more consistent and integrated.
In this guide, you’ll learn about three different methods to install Node.js on an Ubuntu 18.04 server.

Prerequisites>

Prerequisites #

This guide assumes that you are using Ubuntu 18.04. Before you begin, you should have a non-root user account with sudo privileges set up on your system. You can learn how to do this by following the initial server setup tutorial for Ubuntu 18.04.

Installing Node.js from Default Repositories with Apt>

Installing Node.js from Default Repositories with Apt #

Ubuntu 18.04 contains a version of Node.js in its default repositories that can be used to provide a consistent experience across multiple systems. At the time of writing, the version in the repositories is 8.10.0. This will not be the latest version, but it should be stable and sufficient for quick experimentation with the language.
To get this version, you can use the apt package manager. Refresh your local package index:

sudo apt update

Now install Node.js:

sudo apt install nodejs

Verify you’ve installed Node.js successfully by querying node for its version number:

node -v

v8.10.0

If the package in the repositories suits your needs, this is all you need to do to get set up with Node.js. In most cases, you’ll also want to install npm, the Node.js package manager. You can install the npm package with apt:

sudo apt install npm

This will allow you to install modules and packages to use with Node.js.
You’ve now successfully installed Node.js and npm using apt and the default Ubuntu software repositories. However, you may prefer to work with different versions of Node.js, package archives, or version managers. The next steps will discuss these elements, along with more flexible and robust methods of installation.

Installing Node.js with Apt Using a NodeSource PPA>

Installing Node.js with Apt Using a NodeSource PPA #

To install a more recent version of Node.js you can add the PPA (personal package archive) maintained by NodeSource. This will have more up-to-date versions of Node.js than the official Ubuntu repositories and will allow you to choose between several available versions of the platform.
First, install the PPA in order to get access to its contents. From your home directory, use curl to retrieve the installation script for your preferred version, making sure to replace 17.x with your preferred version string (if different):

cd ~
curl -sL https://deb.nodesource.com/setup_17.x -o /tmp/nodesource_setup.sh

You can refer to the NodeSource documentation for more information on currently available versions.
If you’d like, you can inspect the contents of this script with nano (or your preferred text editor):

nano /tmp/nodesource_setup.sh

Once you’re satisfied the script is safe to run, exit the text editor. If you used nano, you can exit by pressing CTRL + X. Next, run the script with sudo:

sudo bash /tmp/nodesource_setup.sh

The PPA will be added to your configuration and your local package cache will be updated automatically. Now you can install the Node.js package as you did in the previous section:

sudo apt install nodejs

Verify you’ve installed the new version by running node with the -v flag:

node -v

v17.3.0

Unlike the one in the default Ubuntu package repositories, this nodejs package contains both node and npm, so you don’t need to install npm separately.
npm uses a configuration file in your home directory to keep track of updates. It will be created the first time you run npm. Run the following command to verify that npm is installed and to create the configuration file:

npm -v

8.3.0

In order for some npm packages to work (those that require compiling code from source, for example), you need to install the build-essential package:

sudo apt install build-essential

Now you have the necessary tools to work with npm packages that require compiling code from source.
In this section, you successfully installed Node.js and npm using apt and the NodeSource PPA. Next, you’ll use the Node Version Manager to install and manage multiple versions of Node.js.

Installing Node Using the Node Version Manager>

Installing Node Using the Node Version Manager #

An alternative for installing Node.js is to use a tool called nvm, the Node Version Manager (NVM). Rather than working at the operating system level, nvm works at the level of an independent directory within your home directory. This means that you can install multiple self-contained versions of Node.js without affecting the entire system.
Controlling your environment with nvm allows you to access the newest versions of Node.js and retain and manage previous releases. It is a different utility from apt, however, and the versions of Node.js that you manage with it are distinct from the versions you manage with apt.
To install NVM on your Ubuntu 18.04 machine, visit the project’s GitHub page. Copy the curl command from the README file that displays on the main page to get the most recent version of the installation script.
Before piping the command through to bash, it is always a good idea to audit the script to make sure it isn’t doing anything you don’t agree with. You can do that by removing the | bash segment at the end of the curl command:

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

Review the output and make sure you are comfortable with the changes it is making. Once you’re satisfied, run the same command with | bash appended at the end. The URL you use will change depending on the latest version of NVM, but as of right now, the script can be downloaded and executed by running the following:

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

This installs the nvm script to your user account. In order to use it, first source the .bashrc file:

source ~/.bashrc

With nvm installed, you can install isolated Node.js versions. First, ask nvm what versions of Node are available:

nvm ls-remote

...
        v14.18.2   (Latest LTS: Fermium)
        v15.0.0
        v15.0.1
        v15.1.0
        v15.2.0
        v15.2.1
        v15.3.0
        v15.4.0
        v15.5.0
        v15.5.1
        v15.6.0
        v15.7.0
        v15.8.0
        v15.9.0
       v15.10.0
       v15.11.0
       v15.12.0
       v15.13.0
       v15.14.0
        v16.0.0
        v16.1.0
        v16.2.0
        v16.3.0
        v16.4.0
        v16.4.1
        v16.4.2
        v16.5.0
        v16.6.0
        v16.6.1
        v16.6.2
        v16.7.0
        v16.8.0
        v16.9.0
        v16.9.1
       v16.10.0
       v16.11.0
       v16.11.1
       v16.12.0
       v16.13.0   (LTS: Gallium)
       v16.13.1   (Latest LTS: Gallium)
        v17.0.0
        v17.0.1
        v17.1.0
        v17.2.0
        v17.3.0

It’s a very long list, but you can install a version of Node by inputting any of the released versions listed. For example, to get version v16.13.1, run the following:

nvm install v16.13.1

Now using node v16.13.1 (npm v8.1.2)

Sometimes nvm will switch to use the most recently installed version. But you can tell nvm to use the version you just downloaded (if different):

nvm use v16.13.1

Check the version currently being used by running the following:

node -v

v16.13.1

If you have multiple Node versions installed, you can run ls to get a list of them:

nvm ls

->     v16.13.1
         system
default -> v16.13.1
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.13.1) (default)
stable -> 16.13 (-> v16.13.1) (default)
lts/* -> lts/gallium (-> v16.13.1)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.8 (-> N/A)
lts/fermium -> v14.18.2 (-> N/A)
lts/gallium -> v16.13.1

You can also default to one of the versions:

nvm alias default 16.13.1

default -> 16.13.1 (-> v16.13.1)

This version will be automatically selected when a new session spawns. You can also reference it by the alias like in the following command:

nvm use default

Now using node v16.13.1 (npm v8.1.2)

Each version of Node will keep track of its own packages and has npm available to manage these.
You can also have npm install packages to the Node.js project’s ./node_modules directory. Use the following syntax to install the express module:

npm install express

added 50 packages, and audited 51 packages in 4s

2 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 8.1.2 -> 8.3.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.3.0
npm notice Run npm install -g npm@8.3.0 to update!
npm notice

If you’d like to install the module globally, making it available to other projects using the same version of Node.js, you can add the -g flag:

npm install -g express

added 50 packages, and audited 51 packages in 1s

2 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

This will install the package in:

~/.nvm/versions/node/16.13.1/lib/node_modules/express

Installing the module globally will let you run commands from the command line, but you’ll have to link the package into your local sphere to require it from within a program:

npm link express

You can learn more about the options available to you with nvm by running the following:

nvm help

You’ve successfully installed Node by using the Node Version Manager, nvm, to install and manage various versions of Node.

Removing Node.js>

Removing Node.js #

You can uninstall Node.js using apt or nvm, depending on the version you want to target. To remove the default repository version, you will use apt at the system level. This command removes the package and retains the configuration files. This is useful if you plan to install the package again in the future:

sudo apt remove nodejs

If you don’t want to save the configuration files for later use, then run the following command to uninstall the package and remove the configuration files associated with it:

sudo apt purge nodejs

As a final step, you can remove any unused packages that were automatically installed with the removed package:

sudo apt autoremove

To uninstall a version of Node.js that you have enabled using nvm, first determine whether or not the version you would like to remove is the current active version:

nvm current

If the version you are targeting is not the current active version, you can run:

nvm uninstall node_version

Uninstalled node node_version

This command will uninstall the selected version of Node.js.
If the version you would like to remove is the current active version, you must first deactivate nvm to enable your changes:

nvm deactivate

Now you can uninstall the current version using the uninstall command used previously. This removes all files associated with the targeted version of Node.js except the cached files that can be used for reinstallment.

Conclusion>

Conclusion #

There are quite a few ways to get up and running with Node.js on your Ubuntu 18.04 server. Your circumstances will dictate which of the methods is best for your needs. While using the packaged version in Ubuntu’s repository is one method, using nvm or a NodeSource PPA offers additional flexibility.
For more information on programming with Node.js, please refer to our tutorial series How To Code in Node.js.