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 in the past years makes it an unavoidable platform in the development world. To have more information and the capabilities of the Node.js platform see the official documentation.
In this tutorial, you will learn how to install a Node.js platform on the Ubuntu 22.04 Linux distribution.
Requirements
This tutorial assumes that you have an Ubuntu 22.04, running in an OVHcloud Compute Instance for example, and some basic knowledge of how to operate it. If you don’t have a running Ubuntu 22.04, follow the guide to use an OVHcloud Compute Instance.
To install the Node.js manager you need to install make tool.
Instructions
In this tutorial, you will, first, install a Node.js platform, then, you will use it, and to finish you learn how to switch between different installed versions.
At the time of writing this tutorial, the last LTS release of Node.js is 16.15.x and the last GA release is 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 tools the most used are nvm and n. In this tutorial you will use n.
To install n on Ubuntu, the easiest way is to use the given bash script:
curl -L https://bit.ly/n-install | bash
Output should be like this:
$ 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. ===
Next, you can test your fresh installation:
n --version
Output should be like this:
$ 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 should be like this:
$ n lts copying : node/16.15.0 installed : v16.15.0 (with npm 8.5.5)
Next, you can test your fresh installation:
npm --version node --version
Output should be like this:
$ npm --version 8.5.5 $ node --version v16.15.0
If you want to install the latest GA version of Node.js:
n current
Output should be like this:
$ 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, you can write an Hello World application. Create a HelloWorld.js file and past the following code:
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 should be like this:
$ 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 should be like this:
$ curl http://127.0.0.1:3000/ 👋 Hello World
That’s it, you have successfully installed and configured Node.js on Ubuntu 22.04.
Go further
Check the offers of public cloud instances on OVHcloud.