How to Install Node.JS on Ubuntu

Read it in 9 Mins

Last updated on
07th Jun, 2022
Published
15th Jun, 2021
Views
3,594
How to Install Node.JS on Ubuntu

Node.js is a general-purpose programming JavaScript platform that allows users to quickly build network applications. Node.js makes development more consistent and integrated by using JavaScript on both the front and backend, and allows you to write JavaScript on the server-side.

Don't forget to read about how to Install Jenkins on Ubuntu.

JavaScript, as you know, is a browser-based language. The browser's engine will take and compile JavaScript code into commands. The creator of Node.js took the engine of Chrome and set it to work on a server. The language can be interpreted in an environment.

In this article, we will read about Node.js installation using three methods:

  1. Installing the Stable Version
  2. Install Using a PPA
  3. Install Using NVM

Prerequisites

Hardware Requirements:

  • RAM: 4 GB
  • Storage: 256 GB of Hard Disk Space

Software Requirements:

  • Web Browser: Any browser such as Google Chrome, Mozilla Firefox, Microsoft Edge.
  • Operating System: An Ubuntu 18.04 server installed with a non-root sudo user and firewall.

Installation Procedure

1. Installing the Stable Version for Ubuntu

In its default repositories, Ubuntu 18.04 contains a version of Node.js that provides a consistent experience on a number of systems. The version of the repositories is 8.10.0 when it is written. This is not the latest version but should be stable and sufficient for quick language experiments.

Step 1: You can use the apt package manager to obtain this version. Refresh your index for your local package with:

$ sudo apt update

Step 2: Now, install Node.js from the repository:

$ sudo apt install nodejs

Step 3: This is all you need to do to get set up with Node.js if the package in the repositories fits your needs. In most cases, the package manager Node.js will also install npm. This can be done by:

$ sudo apt install npm

This allows the installation of Node.js modules and packages.

The executable from Ubuntu repositories is known as nodejs instead of node because of conflict with another package. Take this into account when you run the software.

Step 4: To check which Node.js version you installed, try this:

$ nodejs -v

You can decide if you want to work with different versions, package archives, and version managers, after you have established which version of Node.js you have installed from Ubuntu repositories. The following elements will be discussed with more flexible and robust installation methods.

2. Install Using a PPA

Step 1: First install curl on Ubuntu:

$ sudo apt install curl

Step 2: To access its contents, install the PPA first. Use curl to retrieve the installation script in your favourite version from your home directory so that your favourite version (if different) replaces 10.x:

$ cd ~
$ curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh

Step 3: nano (or your preferred text editor) can be used to inspect the contents of this script:

$ nano nodesource_setup.sh

Step 4: Now, run the script under sudo:

$ sudo bash nodesource_setup.sh

Step 5: You add the PPA to the settings and automatically update your local package cache. You can install the Node.js package in the same way that you did above after running the Nodesource setup script:

$ sudo apt install nodejs

Step 6: To check the version of Node.js, use:

$ nodejs -v

The nodejs package includes the nodejs binary and npm so that you don't have to install npm individually.

Step 7: Npm uses a setup file to keep track of updates in your home directory. The first time you run npm, it will be created. To check that npm has been installed and to create a settings file, execute this command:

$ npm -v

Step 8: You need to install the build-essential package to allow certain npm packages (for instance those that require compilation code from source) to work.

$ sudo apt install build-essential

You now have the tools to work with npm packages, which require source code compilation.

3. Install Using NVM

An alternative is to use a tool called nvm which stands for "Node.js Version Manager" when installing Node.js with apt.  

You can access the latest versions of Node.js and manage previous releases by controlling your environment with nvm. However, the Node.js versions that you handle are different from the apt versions.

Step 1: You can use curl to download the nvm installer from the GitHub project page. Note that the version number may differ from the above:

$ curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh -o install_nvm.sh

Step 2: Use nano to inspect the installation script:

$ nano install_nvm.sh

Step 3: Now, run the script with bash:

$ bash install_nvm.sh

It installs the software in your home ~/.nvm subdirectory. The lines needed to use the file will also be added into your ~/.profile.

Step 4: You will either need to log out and log in back in to get access to NVM functionality or to source the ~/.profile file so you know the changes in the current session:

$ source ~/.profile

Step 5: You can install isolated versions of Node.js with nvm installed. For information on the available Node.js versions, type:

$ nvm ls-remote

Step 6: As you can see, at the time of writing this blog, the current version of LTS was v12.18.3. You can install this with:

$ nvm install 12.18.3

Step 7: Normally, nvm will use the most recently installed version. You can instruct nvm to use the newly downloaded version by typing:

$ nvm use 12.18.3

Step 8: The executable is called node when you install Node.js with nvm. The current version of the shell can be seen by:

$ node -v

Step 9: You can see what is installed in your system if you have multiple Node.js versions:

$ nvm ls

Step 10: If you wish to default to any of the versions, use:

$ nvm alias default 12.18.3

Step 11: When a new session spawns, this version is automatically selected. You can also mention it through the alias as follows:

$ nvm use default

Each Node.js version will track and have npm available to manage its own packages.

Step 12: In the directory of Node.js project's /node modules you can also have npm install packages. Install the express module using the following syntax:

$ npm install express

Step 13: If you want the module to be installed globally by other projects with the same node.js version, you can add the flag -g:

$ npm install -g express

The package will install in:

~/.nvm/versions/node/12.18.3/lib/node_modules/express

Step 14: If you install the module globally, you can execute commands from the command line, but the package must be linked to your local area in order to access this from a program:

$ npm link express

Step 15: You can learn more about the nvm by using:

$ nvm help

Create Demo Web Server

Step 1: If your node is to be tested. Set up js. Let's build a "Hello World!" web server. You can use the following command to create a file app.js and open it in a text editor.

Step 2: Set up a JavaScript (js) file to test the node. Let's build a "Hello World!" web server. You can use the following command to create a file app.js and open it in a text editor.

$ gedit app.js

Step 3: Now, add the following content in the text editor and save it:

var express = require('express');
var app = express();app.get('/', function (req, res) {
  res.send('Hello World!');
});app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

Step 4: Start the node application using the following command:

$ node app.js

You will see the output as an example app running in port 3000!

Step 5: On port 3000 the webserver was launched. Here is a web browser access http://127.0.0.1:3000/ URL. Now you need to set up your app's front-end server.

You're done, that's it. You have created your first Node application successfully. Don't stop here, continue to explore the beautiful Node.js world, as it has more to offer.

How to Uninstall Node.js?

Depending on the version you want to target, you can uninstall Node.js with apt or nvm. You have to work with the apt utility at system level in order to remove the distro-stable version.

1. Using apt:

$ sudo apt remove nodejs

This command will remove the package and keep the setup files. This can be useful for you if the package is to be reinstalled later.

2. If the configuration files for future use are not to be saved, then run the following:

$ sudo apt purge nodejs

This deactivates the package and deletes its associated configuration files.

3. You can then delete any unused packages, which have been installed automatically with the deleted package:

$ sudo apt autoremove

Uninstall using NVM

1. To uninstall a Node.js version that you have enabled with nvm, first determine whether the version you want to remove is the current active version:

$ nvm current

2. If the current active version is the version you want to remove, you must first deactivate NVM in order to allow your changes:

$ nvm deactivate

You can uninstall the current version by using the command above to uninstall all the associated Node.js files with the exception of cached files which can be reinstalled.

Learn more about the core concepts of Node with REPL, Cluster, Routing, Express with Node.js Certification Course.

Conclusion:

On your Ubuntu 18.04 server, there are a few ways to run Node.js. You can decide which of the above-mentioned methods are best suited to your needs. The easiest way to use the packaged version in Ubuntu's repository is by adding flexibility using nvm.

Profile

Abhresh Sugandhi

Author

Abhresh is specialized as a corporate trainer, He has a decade of experience in technical training blended with virtual webinars and instructor-led session created courses, tutorials, and articles for organizations. He is also the founder of Nikasio.com, which offers multiple services in technical training, project consulting, content development, etc.