Friday, December 27, 2024
Google search engine
HomeUncategorisedHow To Install Node.js 16 on Ubuntu 22.04|20.04|18.04

How To Install Node.js 16 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}}

In this guide, we will walk through how to install Node.js 16 on Ubuntu 22.04/20.04/18/04. Node.js is a general-purpose platform with JavaScript runtime built on Chrome’s V8 JavaScript engine and is used to build scalable network applications. It is used on both the back-end and front-end as a full-stack making development more consistent.

npm is an abbreviation of Node Package Manager which is the largest and default package repository for Node.js. Node.js 16 has the following features:

  • V8 JavaScript Engine Version 9
  • Updated Platform support.
  • New Promises APIs
  • Async Local Storage APIs
  • N-API Version 8

Before we commence on Node.js installation, ensure that:

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

  • You have a user with sudo privileges.
  • Update your system

Step 1: Install Node.js using NodeSource PPA / NVM

You can install Node.js 16 on Ubuntu 22.04|20.04|18.04 from either:

  • NodeSource PPA Repository
  • Using NVM Manager

Method 1) Install Node.js 16 on Ubuntu from NodeSource PPA Repository

To install any version of Node.js you can use a PPA (personal package archive) which is maintained by Nodesource. These PPAs have a lot more versions of Nodejs as compared to the official Ubuntu repositories. First, we will need to install the PPA in order to install Node.js 16. From your home directory, use the cURL command. Install cURL in case you haven’t installed it using sudo apt install curl .

cd ~
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -

This command creates APT sources list for Nodesource Nodejs 16 repo which can be viewed as below.

# sample on Ubuntu 20.04
$ cat /etc/apt/sources.list.d/nodesource.list
deb https://deb.nodesource.com/node_16.x focal main
deb-src https://deb.nodesource.com/node_16.x focal main

With the repository added successfully, you can now install Node.js 16 on Ubuntu 22.04/20.04/18.04 with the command:

sudo apt -y install nodejs

Once installed, verify the version of Node.js.

$ node  -v
v16.14.2

Method 2) Install Node.js and npm using NVM

From NVM(Node Version Manager) you can install Node.js 16 using the following steps.

Install NVM tool

Download and install Node Version Manager as below.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

Sample Output:

 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13527  100 13527    0     0  23484      0 --:--:-- --:--:-- --:--:-- 23443
=> Downloading nvm as script to '/root/.nvm'

=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

From the above output, the path of nvm has been added to the current session. Now check the installed version of nvm.

$ source ~/.bashrc
$ nvm --version
0.39.1

Install Node.js 16 and npm

Now with NVM installed, check the available Node.js versions.

nvm list-remote

Sample Output:

....
        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   (LTS: Gallium)
       v16.13.2   (LTS: Gallium)
       v16.14.0   (LTS: Gallium)
       v16.14.1   (LTS: Gallium)
       v16.14.2   (Latest LTS: Gallium)

From the above output, nvm has many versions of Node.js. Proceed and install the Node.js 16.x version that you want on Ubuntu 22.04/20.04/18/04.

nvm install v16

Sample Output:

Downloading and installing node v16.14.2...
Downloading https://nodejs.org/dist/v16.14.2/node-v16.14.2-linux-x64.tar.xz...
############################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.14.2 (npm v8.5.0)
Creating default alias: default -> v16 (-> v16.14.2)

Another example on installation of version 16.14.1:

$ nvm install v16.14.1
Downloading and installing node v16.14.1...
Downloading https://nodejs.org/dist/v16.14.1/node-v16.14.1-linux-x64.tar.xz...
############################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.14.1 (npm v8.5.0)

Once complete, check the installed version of Node.js.

$ node --version
v16.14.2

You have Node.js 16 installed. In case you have multiple versions of Node.js installed list them with:

$ nvm ls
->     v16.14.1
       v16.14.2
         system
default -> v16 (-> v16.14.2)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.14.2) (default)
stable -> 16.14 (-> v16.14.2) (default)
lts/* -> lts/gallium (-> v16.14.2)
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.12 (-> N/A)
lts/fermium -> v14.19.1 (-> N/A)
lts/gallium -> v16.14.2

Set the default version of Node.js with this command. As seen the current version in use is v16.14.1 we now switch to version 16.

$ nvm use 16.14.2
Now using node v16.14.2 (npm v8.5.0)

Set it as the default.

$ nvm alias default 16.14.2
default -> 16.14.2 (-> v16.14.2)

Step 2: Install Node.js Development Tools

In order to use npm to build native tools, you require development tools and libraries that you install on Ubuntu 22.04/20.04/18/04 as below:

sudo apt -y install gcc g++ make

Install the Yarn package manager.

sudo apt install gnupg2
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn

Check the installed version of Yarn.

$ yarn -V

Sample Output.

Install Node.js 16 on Ubuntu 20.041804 Debian 11109 1

Step 3: Getting started with Node.js

Now that we have successfully installed Node.js 16 on our system, let us build a simple web server. First, create a file.

vim example.js

In the created file, add the below lines.

const http = require('http');

const hostname = '0.0.0.0';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World from Kenya');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Here, we have created a simple webserver to print “Hello World from Kenya

Start the webserver:

$ node example.js
Server running at http://0.0.0.0:3000/

The server is running on port 3000, so we allow it through the firewall.

# Firewalld
sudo firewall-cmd --zone=public --add-port=3000/tcp --permanent
sudo firewall-cmd --reload

# Ufw
sudo ufw allow 3000/tcp

Now access the web server using http://IP_Address:3000 on your browser. The page should be similar to this.

Install Node.js 16 on Ubuntu 20.041804 Debian 11109 3

That is it! We have triumphantly installed Node.js 16 on Ubuntu 22.04/20.04/18/04. I hope this article was helpful.

ode.JS Programming Udemy Video Courses

See more articles on our page:

.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