How to install Node.js on Ubuntu (A step-by-step guide)

The JavaScript runtime environment Node.js has been a fundamental tool for developers for years. Countless websites and applications rely on the platform, the latest versions of which have been downloaded and used millions of times. We explain step by step how to install Node.js on Ubuntu.

What are the requirements for installing Node.js on Ubuntu?

Before proceeding with the installation, it’s important that your system meets certain requirements to ensure successful installation and smooth operation of the platform. Since Node.js executes JavaScript code, having prior experience with the scripting language is definitely a plus. Being familiar with JavaScript’s basic syntax and functions as well as the typical use cases that it’s used for will make it easier for you to use Node.js in a more targeted manner. Similarly, knowledge of object-oriented programming (OOP) and software design principles puts you at an advantage when working with the runtime environment.

Hardware

To install Node.js on Ubuntu, you’ll need suitable hardware. The platform isn’t particularly demanding though and functions on most modern computers. All you need is a memory of at least four gigabytes and a minimum of 256 gigabytes of available storage space on your hard disk. It’s also important to have a stable internet connection.

Software

Your operating system and the programs required for installing and running Node.js should be up to date. This helps to ensure complications don’t arise when installing Node.js on Ubuntu. You should have an Ubuntu server installed and configured as well as a non-root user set up. It’s also a good idea to set up a firewall. You’ll need a browser to use Node.js. The runtime environment works with all popular providers.

VPS Hosting
Fully virtualised servers with root access
  • Unlimited traffic and up to 1 Gbit/s bandwidth
  • Fast SSD NVMe storage
  • Free Plesk Web Host Edition

Step-by-step instructions for installing Node.js on Ubuntu

There are different ways you can install Node.js on Ubuntu. In the following sections, we’ll discuss three different methods.

Direct installation

To install the stable version of Node.js on Ubuntu, you should first update the terminal. To do this, follow the steps below:

  1. Check for an update for the terminal and install it if necessary. To do this, use the following command:
$ sudo apt update
bash
  1. Now install the runtime environment using the following code:
$ sudo apt install nodejs
bash
  1. Node.js uses the package manager npm. You can install it with this code:
$ sudo apt install npm
bash
  1. Finally, check whether your version of Node.js is now up to date:
$ node -v && npm --version
bash

Personal package archive (PPA)

Alternatively, you can carry out the installation with a personal package archive (PPA). To use this method, you’ll need cURL, a tool that you can use to transfer data to and from a server. If you can’t find cURL on your system, you can start by installing it:

  1. Here’s the command for installing cURL:
$ sudo apt install curl
bash
  1. Now add the official Node.js setup page to cURL:
$ curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
bash

If you receive an error message such as ‘the following signatures couldn’t be verified because the public key is not available’, retrieve your public key and then copy and paste it into the following code:

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key <INSERT YOUR KEY HERE>
bash
  1. Then install Node.js:
$ sudo apt install -y nodejs
bash
  1. Finally, check that the latest version of Node.js has been installed on your Ubuntu system:
node -v && npm --version
bash

Node Version Manager

If you want to be able to choose from different versions of the runtime environment, it’s best to install Node.js with the Node Version Manager (NVM). Here’s how to do it:

  1. Install the command line:
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
bash
  1. Now use NVM to install Node.js under Ubuntu:
$ nvm install node
bash
  1. Ensure you’ve installed the correct version:
$ node -v
bash

Check whether NVM is up to date:

$ nvm --version
bash

How to test whether the platform works

Before launching projects with Node.js, test the program. To do this, create a simple application. Here’s how:

  1. Create a new folder:
mkdir new-project
bash
  1. Open the folder:
cd new-project
bash
  1. Launch a new project in Node.js using the following command.
npm init -y
bash

This creates a new file called package.json, which contains the metadata and links of your project.

  1. Create a new file. You can use a text editor such as nano for this:
sudo nano app.js
bash
  1. Enter the following command:
console.log("Here is your sample text.");
bash
  1. Save the file.

  2. Open the terminal and enter the following command:

node app.js
bash
  1. Check that your sample text has been saved. If it has, that means you’ve successfully installed Node.js on your Ubuntu system and can now work with the platform. If you’d like to learn how to use the platform, we recommend checking out our tutorial on Introduction to Node.js.
Tip

Explore more useful information about Node.js, Ubuntu and JavaScript in our Digital Guide. To get started, check out our summary of the differences between Java and JavaScript.

Was this article helpful?
Page top