UGC bg

How to install Node.js on Ubuntu 22.04


Install Node.js on Ubuntu 22.04

Objective

Node.js is one of the most famous asynchronous event-driven JavaScript runtimes. Its wide adoption over the past years makes it an unavoidable platform in the development world. To learn more about the capabilities of the Node.js platform refer to the official documentation.

In this tutorial, you will learn how to install a Node.js platform on an Ubuntu 22.04 Linux distribution.

 

Requirements

This tutorial assumes that you have an OVHcloud Public Cloud Compute Instance, VPS, or bare metal server running Ubuntu 22.04 and basic knowledge using the command line. In this tutorial, we've used a Public Cloud Compute instance. If you need help setting up a Public Cloud instance with Ubuntu 22.04, follow this guide: Creating and connecting to your first Public Cloud instance.

To install the Node.js manager you need to install the make tool.

 

Instructions

In this tutorial, you will install a Node.js platform, use it, and learn how to switch between several installed versions.

At the time of creating this tutorial, the latest release of Node.js was 16.15.x and the latest GA release was 18.1.x.

 

A node manager to rule them all

Before installing Node.js you have to install a tool to manage multiple Node.js installations. The two most used tools are nvm and n. In this example, we use n.

To install n on Ubuntu, use the following bash script:

curl -L https://bit.ly/n-install | bash

Output:

$ curl -L https://bit.ly/n-install | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   161  100   161    0     0    718      0 --:--:-- --:--:-- --:--:--   718
100 43367  100 43367    0     0  83076      0 --:--:-- --:--:-- --:--:--  225k
===
You are ABOUT TO INSTALL n, the Node.js VERSION MANAGER, in:

  /home/ubuntu/n

Afterwards, THE FOLLOWING Node.js VERSION(S) WILL BE INSTALLED,
and the first one listed will be made active; 
  'lts' refers to the LTS (long-term support) version, 
  'latest' to the latest available version.
  '-' means that *no* version will be installed:

  lts
 
If your shell is bash, bsh, zsh, fish, or pwsh (PowerShell), the relevant 
initialization file will be modified in order to:
 - export environment variable $N_PREFIX.
 - ensure that $N_PREFIX/bin is in the $PATH

Your shell, bash, IS supported, and the following initialization
file will be updated:

   /home/ubuntu/.bashrc

For more information, see https://bit.ly/n-install-repo
===
CONTINUE (y/N)? y
-- Cloning https://github.com/tj/n to '/home/ubuntu/n/n/.repo'...
-- Running local n installation to '/home/ubuntu/n/bin'...
-- Shell initialization file '/home/ubuntu/.bashrc' updated.
-- Installing helper scripts in '/home/ubuntu/n/bin'...
-- Installing the requested Node.js version(s)...
   1 of 1: lts...
  installing : node-v16.15.0
       mkdir : /home/ubuntu/n/n/versions/node/16.15.0
       fetch : https://nodejs.org/dist/v16.15.0/node-v16.15.0-linux-x64.tar.xz
     copying : node/16.15.0
   installed : v16.15.0 (with npm 8.5.5)
=== n successfully installed.
  The active Node.js version is: v16.15.0

  Run `n -h` for help.
  To update n later, run `n-update`.
  To uninstall, run `n-uninstall`.

  IMPORTANT: OPEN A NEW TERMINAL TAB/WINDOW or run `. /home/ubuntu/.bashrc`
             before using n and Node.js.
===

Verify the installation:

n --version

Output:

$ n --version
v8.2.0

 

Installation of Node.js with n

To install the LTS version of Node.js, type in the following command:

n lts

Output:

$ n lts
   copying : node/16.15.0
   installed : v16.15.0 (with npm 8.5.5)

Verify the new installation:

npm --version node --version

Output:

$ npm --version
8.5.5

$ node --version
v16.15.0

To install the latest GA version of Node.js:

n current

Output:

$ n current
  installing : node-v18.1.0
  mkdir : /home/ubuntu/n/n/versions/node/18.1.0
  fetch : https://nodejs.org/dist/v18.1.0/node-v18.1.0-linux-x64.tar.xz
  copying : node/18.1.0
  installed : v18.1.0 (with npm 8.8.0)

 

Test the Node.js installation

To test your Node.js installation, write a Hello World application. Create a HelloWorld.js file and paste the following code into the file:

const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });

Save and run it.

node HelloWorld.js

Output:

$ node HelloWorld.js
Server running at http://127.0.0.1:3000/

To test your sample, run the following command:

curl http://127.0.0.1:3000/

Output:

$ curl http://127.0.0.1:3000/
👋 Hello World

That’s it, you have successfully installed and configured Node.js on Ubuntu 22.04.