How to Install Node.js and npm on Ubuntu 20.04 | 18.04

This brief tutorial shows students and new users how to install Node.js and npm on Ubuntu 20.04 | 18.04.

Node.js is an opensource, cross-platform full-stack solution which is primarily used to build back-end server apps using JavaScript runtime, and built on Chrome’s V8 JavaScript engine.

Chrome V8 engine on the other hand is a Google’s open source high-performance JavaScript engine written in C++ and used in Google Chrome, the open source projects.

Npm is the default package manager for Node.js and the world’s largest software registry.

For more about Node.js, please visit the project’s homepage.

There are multiple ways to install Node.js on Ubuntu.

If you only need Node.js as a local runtime for your Node.js applications, then the simplest way to install it will be from the NodeSource repository. 

To get stated with install Node.js and npm on Ubuntu, follow the steps below:

Install Node.js and npm from NodeSource

NodeSource is the company’s own enterprise-grade Node repository that they maintain and contains the latest versions of Node.js.

You NodeSource repository if you want to install specific version of Node.js. At the time of this writing, the repository contains Node.js version 14, 14, 12, 11, 10, 8.

To install Node.js from NodeSource, simply run the commands below to add the specific version you want.

Then for the Latest release (version 14), add this PPA.

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

For (version 12), run the commands below:

curl -sL https://deb.nodesource.com/setup_12.x | bash -

To install the LTS release (version 10), use this PPA

curl -sL https://deb.nodesource.com/setup_10.x | bash -

After that, you can now install latest version of Node.js from the particular repository you choose… If you add both repositories, the latest version of Node.js will be installed and not the LTS.

Install Node.js and npm

To install, run the commands below

sudo apt install nodejs

After installing, both Node.js and npm modules should be installed and ready to use.

You can use the commands below to view the version number installed.

node --version
npm --version

The commands will list the current installed version:

v14.0.0
6.14.4

Install Node.js and npm via Snap

Another way to install Node.js is via Snap package management… this might be the easiest way to do it.

Snaps are containerized software packages that are simple to create and install.

They are applications packaged with all their dependencies to run on all popular Linux distributions from a single build which allow them to update automatically and roll back gracefully.

To install via Snap, run the commands below install Snap.

sudo apt update
sudo apt install snapd

Then run the commands below to get Node.js installed via Snap..

For the Latest release (version 14),

sudo snap install node --channel=14/stable --classic

For version 13, run this:

sudo snap install node --channel=13/stable --classic

For the LTS (version 10)

sudo snap install node --channel=10/stable --classic

To test whether the web server is properly installed, run the commands below to create a test file called http_server.js in your home folder.

cd ~/
nano http_server.js

Then copy and paste the content below into the file and save.

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 Worldn');
});

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

After that save the file and run the commands below to start the server.

node http_server.js

You should see an output that reads:

Server running at http://127.0.0.1:3000/

Now open your browser and browse to the server hostname or IP address followed by port 3000… and you should see a default page with Hello World

http://localhost:3000

Node.js ubuntu install

Conclusion:

This post showed you how to install Node.js and npm on Ubuntu 20.04 | 18.04. If you find any error above, please use the comment form below to report.

Thanks,

You may also like the post below:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.