Wednesday, January 8, 2025
Google search engine
HomeGuest BlogsInstall Node.js 20 LTS on Ubuntu 22.04|20.04|18.04

Install Node.js 20 LTS on Ubuntu 22.04|20.04|18.04

.tdi_3.td-a-rec{text-align:center}.tdi_3 .td-element-style{z-index:-1}.tdi_3.td-a-rec-img{text-align:left}.tdi_3.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_3.td-a-rec-img{text-align:center}}

Before the birth of of Node.js programming / scripting language, JavaScript was natively used to develop client-side websites. With release of Node.js it was possible to extend the use of JavaScript to develop server-side applications. This gave powers to developers where they could build full-stack applications without learning a new programming language.

Node.js is a cross-platform and free to use server-side JavaScript runtime environment created from Google’s V8 JavaScript engine used in Google Chrome browser.

In this tutorial we won’t dive much into the features and improvements in Node.js 20. For comprehensive coverage visit official Node.js 20 release notes page to get deeper insights on this version.

.tdi_2.td-a-rec{text-align:center}.tdi_2 .td-element-style{z-index:-1}.tdi_2.td-a-rec-img{text-align:left}.tdi_2.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_2.td-a-rec-img{text-align:center}}

Here are some highlights:

  • Official support for ARM64 Windows
  • Progress on Web Assembly System Interface (WASI)
  • Web Crypto API
  • Preparing single executable apps now requires injecting a Blob
  • Performance improvements
  • Test Runner module now stable
  • New permissions and abilities in Permission Model

Install Node.js 20 LTS on Ubuntu 22.04|20.04|18.04

Node.js offers different packages for Linux, Windows, and macOS. You can visit the official Node.js website and select the download link that corresponds to your operating system.

For reference and simplicity, we shall provide installation process from a DEB repository.

Step 1: Add Node.js 20 APT repository

Update your packages index.

sudo apt update

Run the script that will configure APT repository for Node.js 20 packages.

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

Sample output:

## Installing the NodeSource Node.js 20.x repo...


## Populating apt-get cache...

+ apt-get update
Hit:1 https://mirror.hetzner.com/ubuntu/packages jammy InRelease
Hit:2 https://mirror.hetzner.com/ubuntu/packages jammy-updates InRelease
Hit:3 https://mirror.hetzner.com/ubuntu/packages jammy-backports InRelease
Hit:4 https://mirror.hetzner.com/ubuntu/security jammy-security InRelease
Reading package lists... Done

## Confirming "jammy" is supported...

+ curl -sLf -o /dev/null 'https://deb.nodesource.com/node_20.x/dists/jammy/Release'

## Adding the NodeSource signing key to your keyring...

+ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | tee /usr/share/keyrings/nodesource.gpg >/dev/null

## Creating apt sources list file for the NodeSource Node.js 20.x repo...

+ echo 'deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x jammy main' > /etc/apt/sources.list.d/nodesource.list
+ echo 'deb-src [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x jammy main' >> /etc/apt/sources.list.d/nodesource.list

## Running `apt-get update` for you...

+ apt-get update
Hit:1 https://mirror.hetzner.com/ubuntu/packages jammy InRelease
Hit:2 https://mirror.hetzner.com/ubuntu/packages jammy-updates InRelease
Hit:3 https://mirror.hetzner.com/ubuntu/packages jammy-backports InRelease
Hit:4 https://mirror.hetzner.com/ubuntu/security jammy-security InRelease
Get:5 https://deb.nodesource.com/node_20.x jammy InRelease [4,563 B]
Get:6 https://deb.nodesource.com/node_20.x jammy/main amd64 Packages [776 B]
Fetched 5,339 B in 2s (3,411 B/s)
Reading package lists... Done
...

Step 2: Install Node.js, npm and yarn on Ubuntu

The repository we just configured contains Node.js binary distributions via .deb. We can install Node.js 20.x and npm by running the following commands:

sudo apt update && sudo apt install -y nodejs

You can also install development tools used in building native addons:

sudo apt install gcc g++ make -y

If you want to install Yarn package manager, run the commands given below:

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt-get install yarn -y

Verify install

$ node -v
v20.1.0

$ npm -v
9.6.4

$ yarn -v
1.22.19

Step 3: Using Node.js 20 on Ubuntu

Node.js enables you to create server-side applications using JavaScript. Let’s create our development environment to demonstrate how to create and run Node.js web application.

Create a new directory for the application.

cd ~/
mkdir mynode-app && cd mynode-app

Initialize the project by creating a package.json file which contains the metadata about the application and its dependencies required to run.

npm init

Follow the prompts to fill in the required information for the application. Confirm the file is created.

$ ls
package.json

In our application we’ll use Express framework. Let’s install this dependency – the --save option is for adding the dependency to package.json file.

$ npm install express --save

added 57 packages, and audited 58 packages in 3s

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

found 0 vulnerabilities

Next we create a new file called app.js in the same project directory which contain the code for the web application.

vim app.js

Add the following code to create a basic Express application:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, Node World!');
});

app.listen(3000, () => {
  console.log('App listening on port 3000!');
});

The code above creates an Express application listening for requests on port 3000. When a request is made to the root URL (/), the application sends a “Hello, Node World!” message to the client.

Run the following command to run the application.

node app.js

On a separate terminal curl IP address and port 3000 to confirm the app works.

$ curl 127.0.0.1:3000
Hello, Node World!

You should see the “Hello, Node World!” message displayed in your browser.

Conclusion

This guide has demonstrated how you can start the installation of Node.js 20 LTS on Ubuntu Linux system. As seen in this article, the process is straightforward and doesn’t required any special Linux skills. By following and customizing on the steps provided, you should be able to develop more complex applications by adding extra functionality and features. We hope this article was of great help. Follow us on our social media platforms for more juicy updates.

.tdi_4.td-a-rec{text-align:center}.tdi_4 .td-element-style{z-index:-1}.tdi_4.td-a-rec-img{text-align:left}.tdi_4.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_4.td-a-rec-img{text-align:center}}

RELATED ARTICLES

Most Popular

Recent Comments