In this guide, we will introduce how to install Node.js 16 on Ubuntu 22.04/20.04/18/04. Node.js is a V8 JavaScript engine based on Chrome, a common platform of JavaScript runtime, used to build scalable web applications. It is used as a full stack on both the back end and the front end, making development more consistent.
npm is short for Node Package Manager, and it is the largest default package repository for Node.js. Node.js 16 has the following features:
- V8 JavaScript engine version 9
- Updated platform support.
- New promise API
- Asynchronous local storage API
- N-API version 8
Before we start installing Node.js, please make sure:
- You have a user sudo privilege.
- 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 in any of the following ways:
- NodeSource PPA repository
- Use NVM Manager
Method 1) Install Node.js 16 on Ubuntu from the NodeSource PPA repository
To install any version of Node.js, you can use the PPA (Personal Package Archive) maintained by Nodesource. Compared to the official Ubuntu repositories, these PPAs have more versions of Nodejs. First, we need to install PPA to install Node.js 16. In your home directory, use the cURL command.Install cURL in case you haven’t used it yet sudo apt install curl
.
cd ~
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
This command creates a list of APT sources for the Nodesource Nodejs 16 repository as shown 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
After successfully adding the repository, you can now install Node.js 16 on Ubuntu 22.04/20.04/18.04 using the following command:
sudo apt -y install nodejs
After installation, verify the version of Node.js.
$ node -v
v16.6.2
Method 2) Use NVM to install Node.js and npm
From NVM (Node Version Manager), you can install Node.js 16 using the following steps.
Install NVM tools
Download and install the node version manager as shown 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 nvm version.
$ source ~/.bashrc
$ nvm --version
0.38.0
Install Node.js 16 and npm
Now install NVM and check the available Node.js version.
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
Judging from the above output, nvm has many versions of Node.js. Go ahead and install the required Node.js 16.x version on Ubuntu 22.04/20.04/18/04. I will go to v16.7.0
nvm install v16
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)
other example About the installed 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)
When finished, check the installed version of Node.js.
$ node --version
v16.7.0
v16.6.2
You have installed Node.js 16. If you have installed multiple versions of Node.js, please list them:
$ 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)
Use this command to set the default version of Node.js. As you can see, the currently used version is v14.15.1, and we are now switching to version 16.
$ nvm use 16.6.2
Now using node v16.6.2 (npm v7.20.3)
Set it to the default value.
$
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 need the development tools and libraries installed on Ubuntu 22.04/20.04/18/04 as follows:
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: Start using Node.js
Now that we have successfully installed Node.js 16 on our system, let’s build a simple web server. First, create a file.
vim example.js
In the created file, add the following 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 https://${hostname}:${port}/`);
});
Here, we created a simple web server to print” Hello World from Kenya “
Start the web server:
$ node example.js
Server running at https://0.0.0.0:3000/
The server is running on port 3000, so we allow it to pass 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 use https://IP_Address:3000 on the browser to access the Web server. The page should look similar to this.
now it’s right! We have successfully installed Node.js 16 on Ubuntu 22.04/20.04/18/04. I hope this article is helpful.
ode.JS programming Udemy video course
- The complete Node.js developer course (version 3)
- NodeJS-complete guide (MVC, REST API, GraphQL, Deno)
- Microservices using Node JS and React
- Node JS: Advanced concepts
- Learn and understand NodeJS
- Angular and NodeJS – MEAN stack guide
Check out more articles on our page:
- Install Node.js on Ubuntu and Debian 14
- How to install OpenNMS on Debian
- Install Debian 11 Bullseye-step by step screenshots