Tutorial

How To Install Node.js on Ubuntu 18.04

Updated on December 29, 2021
English
How To Install Node.js on Ubuntu 18.04
Not using Ubuntu 18.04?Choose a different version or distribution.
Ubuntu 18.04

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

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

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:

  1. sudo apt update

Now install Node.js:

  1. sudo apt install nodejs

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

  1. node -v
Output
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:

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

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

  1. cd ~
  2. 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):

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

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

  1. sudo apt install nodejs

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

  1. node -v
Output
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:

  1. npm -v
Output
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:

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

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:

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

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

  1. source ~/.bashrc

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

  1. nvm ls-remote
Output
... 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:

  1. nvm install v16.13.1
Output
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):

  1. nvm use v16.13.1

Check the version currently being used by running the following:

  1. node -v
Output
v16.13.1

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

  1. nvm ls
Output
-> 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:

  1. nvm alias default 16.13.1
Output
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:

  1. nvm use default
Output
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:

  1. npm install express
Output
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:

  1. npm install -g express
Output
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:

  1. ~/.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:

  1. npm link express

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

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

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:

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

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

  1. nvm current

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

  1. nvm uninstall node_version
Output
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:

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

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.

DigitalOcean provides multiple options for deploying Node.js applications, from our simple, affordable virtual machines to our fully-managed App Platform offering. Easily host your Node.js application on DigitalOcean in seconds.

Learn more here


Tutorial Series: How to Install Node.js and Create a Local Development Environment

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.

To get your development environment configured so you can start building Node.js applications. select the tutorial for your platform.

About the authors



Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
9 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

In the example command, you have used version 10 which is not supported anymore.

curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh

Please update it to version 16. I mistakenly copy pasted and installed v10 and had to update to 16 again.

For installing NPM recently, had to follow the following steps:

sudo apt-get install nodejs-dev node-gyp libssl1.0-dev

and

sudo apt-get install npm

Reference: https://askubuntu.com/a/1092849

Recently, I created my very first droplet and used this tutorial to install node on it. (I also did what is recommended in https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-18-04.)

After that all I had to do was the following:

npm install --save express npm install --save handlebars

My Node/Express app developed under Windows and uploaded to my droplet ran correctly. I am amazed that things were so easy.

Thanks for this tutorial.

You must change command for install nodeJS to…

‘sudo apt-get install -y nodejs’

hi, any update on this article?

very useful article, nodejs installed successfully. thank you

This article is awesome 🙌Y’all are great!

It would so useful as a quickstart, so I could quickly get my node installation up and running 🏃‍♀️😄

I had to use nvm install v8.11.1 instead of nvm install 8.11.1. I’m still new so don’t know if that’s just me or if that’s a typo. Great information though, just almost panicked when it didn’t load without the v before the version number.

Dear Kathleen - this was super useful. Thank you ! I had issues installing on my ubuntu laptop and these instructions really made it work for me - so THANKS a TON!! It’s kind and generous people like you who help more people like me overcome the fear of programming and give us the courage to try.

  • Joe

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more