10

Install Node.js 16 on Ubuntu 20.04/18.04 & Debian 11/10/9

 2 years ago
source link: https://computingforgeeks.com/how-to-install-node-js-on-ubuntu-debian/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Install Node.js 16 on Ubuntu 20.04/18.04 & Debian 11/10/9

In this guide, we will walk through how to install Node.js 16 on Ubuntu 20.04/18/04 & Debian 11/10/9. 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:

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

Step 1a: Install Node.js with Apt Using a NodeSource PPA.

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

#sample on Debian 11
$ cat /etc/apt/sources.list.d/nodesource.list
deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_16.x bullseye main
deb-src [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_16.x bullseye main

With the repository added successfully, you can now install Node.js 16 on Ubuntu 20.04/18/04 & Debian 11/10/9 with the command:

sudo apt -y install nodejs

Once installed, verify the version of Node.js.

$ node  -v
v16.6.2

Step 1b: Install Nodejs and npm using NVM.

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

1. Install NVM

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

2. Install Node.js 16 and npm

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

nvm list-remote

Sample Output:

 ...........      
       v14.15.1   (LTS: Fermium)
       v14.15.2   (LTS: Fermium)
       v14.15.3   (LTS: Fermium)
       v14.15.4   (LTS: Fermium)
       v14.15.5   (LTS: Fermium)
       v14.16.0   (LTS: Fermium)
       v14.16.1   (LTS: Fermium)
       v14.17.0   (LTS: Fermium)
       v14.17.1   (LTS: Fermium)
       v14.17.2   (LTS: Fermium)
       v14.17.3   (LTS: Fermium)
       v14.17.4   (LTS: Fermium)
       v14.17.5   (Latest LTS: Fermium)
.........
       v15.14.0
        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

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 20.04/18/04 & Debian 11/10/9. I will go for v16.7.0

nvm install v16.7.0

Sample Output:

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

Another example on installation of version 16.6.2:

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

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

$ node --version
v16.7.0
v16.6.2

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

$ nvm ls
->      v16.6.2
        v16.7.0
default -> v16.7.0
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.7.0) (default)
stable -> 16.7 (-> v16.7.0) (default)
lts/* -> lts/fermium (-> N/A)
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.5 (-> N/A)
lts/fermium -> v14.17.5 (-> N/A)

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

$ nvm use 16.6.2
Now using node v16.6.2 (npm v7.20.3)

Set it as the default.

$ nvm alias default 16.6.2
default -> 16.6.2 (-> v16.6.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 20.04/18/04 & Debian 11/10/9 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.

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.

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

ode.JS Programming Udemy Video Courses

See more articles on our page:

Install Node.js 14 on Ubuntu 20.04/18.04 & Debian 11/10/9

How To Install OpenNMS on Debian 11 / Debian 10

Install Debian 11 Bullseye – Step by Step With Screenshots


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK