This post shows students and new users the steps to install Node.js and npm on Ubuntu Linux to allow users and developers to run Node.js-based applications.

Node.js is an open source cross-platform solution primarily used for building back-end server applications using the JavaScript runtime, built on Chrome’s V8 JavaScript engine. The Chrome V8 engine, on the other hand, is Google’s high-performance open source 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.

This post will show you various ways to install these two popular packages when using Ubuntu Linux. The steps below will show you three ways to install Node.js and npm packages on Ubuntu Linux.

For more information on Node.js, visit the Homepage.

For instructions on installing Node.js and npm on Ubuntu Linux, follow the steps below:

How to install Node.js from Ubuntu repositories

By default, the Node.js and npm packages are available in the default Ubuntu repositories. However, the versions of the Ubuntu repositories may not necessarily be the most recent. Installation is pretty straightforward.

Run the following commands to update the package index and install Node.js and npm:

sudo apt update
sudo apt install nodejs npm

The above command will install a number of Node.js and npm packages, including the tools required to compile and install native npm plugins.

YOU CAN ALSO READ:   How to install Memcached on Ubuntu Linux

Once the installation is complete, run the following commands to verify the installed version.

nodejs --version

That should generate a line similar to the one shown below:

v10.19.0

As you can see, the latest version of Ubuntu is v10.19.0. However, the latest versions v11, v12, v13 and v14 have also been released.

How to install Node.js and npm from Snap

Another way to install Node.js is through Snap package management. This might be the easiest way to do it.

Snaps They are containerized software packages that are easy to create and install. They are apps packaged with all their dependencies to run on all popular Linux distributions from a single build, allowing them to automatically update and gracefully roll back.

To install via Snap, run the following commands to install Snap.

sudo apt update
sudo apt install snapd

Once Snap is installed, just run the following commands to install Node.js and npm through Snap. Choose the correct versions of Node.js that you want to install on Ubuntu.

For the latest version (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

How to install Node.js and npm from NodeSource

NodeSource is the company’s enterprise level Node repository maintained and contains the latest versions of Node.js.

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

YOU CAN ALSO READ:   How to install LEMP on Ubuntu Linux

To install Node.js from NodeSource, just run the following commands to add a specific repository for the version you want.

Then for him More recent liberationversion 14), add this PPA.

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

For (version 12), run the following commands:

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

To install the LTS liberationversion 10), use this PPA

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

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

Install Node.js and npm

Once the NodeSource repositories are added, just run the command to install Node.js from the added repository.

sudo apt install nodejs

After installation, the Node.js and npm modules should be installed and ready to use.

You can use the following commands to see the installed version number.

node --version
npm --version

The commands will list the current installed version:

v14.0.0
6.14.4

To test if the web server is installed correctly, run the following commands 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 it.

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 following commands to start the server.

node http_server.js

You should see a result that says:

Server running at http://127.0.0.1:3000/

Now open your browser and look for the server’s hostname or IP address followed by the port 3000. and you should see a default page with Hello World

http://localhost:3000
You should do that!

Conclusion:

In this tutorial we have seen how to install Node.js and npm on Ubuntu Linux. If you find any errors above or have something to add, please use the comment form below.

Write A Comment