Skip to main content

Linux NPM Node Configuration

How To Install Node.js On Ubuntu 18.04

Introduction

Node.js is a powerful, open-source JavaScript runtime environment designed to enable the implementation of server-side JavaScript code. It's a cross-platform package that allows developers to run JavaScript code on a machine like a standalone application. The platform is primarily used to create robust back-end, server-side applications, but it's also useful as a front-end solution.
This tutorial will help you install Node.Js on Ubuntu 18.04 Bionic Beaver Linux via three options: from Ubuntu repository, from NodeSource repository, and using the Node Version Manager (NVM).

Part 1: Installing Node.js From Ubuntu 18.04 Repository

Node,js is a reliable package that is available from the repository of latest distribution of Ubuntu (18.04). In the first part of this tutorial will install Node.js and the Node.js Package Manager (NPM) from Ubuntu 18.04 repository.

Step one - Installing Node.js

First, update the packages index, then install the Node.js:
  1. $ sudo apt update
  2. $ sudo apt install nodejs
Next, execute the command below to verify this installation:
  1. $ nodejs --version
This should give you the following output:
  1. v8.10.0
The version of Node.js package in Ubuntu repository is referred to as nodejs and not node to avert conflict with another package in the repository.

Step 2 - Installing Node.js Package Manager (npm)

Installing Node.js Package Manager makes it easy to download npm packages. Run the command below to install npm:
  1. $ sudo apt install npm
Once the installation is done, execute the command below to verify it:
  1. $ npm --version
This will give you the following output:
  1. 3.5.2

Part two: Installing Node.js From NodeSource Repository

NodeSource is a popular company that offers enterprise-class Node support. The company maintains a reliable repository that features the most recent version of Node.js. Follow the steps below to install Node.js package and npm.

Step 1 - enabling NodeSource Repository

Execute the command below to activate the NodeSource repository:
  1. $ curl -sL https://deb.nodesource.com/setup_8.x | sudo bash -
The command above adds a signing key for NodeSource to your system, creates a source repository file for apt, installs all the required packages, and refreshes the apt cache. The setup_8.x is an LTS version of Node.js;the value can be changed to version 10.x with setup_10.x.

Step 2 - Installing Node.js as well as the npm

By now the NodeSource repository is activated, and the stage is set for Node.js installation. Run the command below to install Node.js and the npm:
  1. $ sudo apt install nodejs
To verify Node.js installation, execute the command below:
  1. $ node --version
This will give you the following results:
  1. V8.11.3
Next, run the command below to verify npm installation:
  1. $ npm --version
This will give you the output below:
  1. 5.6.0
That is it! You have successfully installed Node.js from the NodeSource repository.

Part 3: Installing Node,js using the Node Version Manager (NVM)

NVM is a helpful bash script that is phenomenal at managing numerous versions of active Node.js. With NVM, it's a breeze to install or uninstall a specific version of Node.js you want to use. In this part of our tutorial, we’ll utilize NVM to install Node.js on Ubuntu 18.04.

Step 1- Downloading NVM Script

Execute the command below to download the install script for NVM:
  1. $ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
The script will automatically clone Node Version Manager repository from Github to ~/.nvm directory. In addition, it will add an nvm path to the ZSH or Bash profile.
The output of the download-nvm-script will look like this:
  1. => Close and reopen your terminal to start using nvm or run the following to use it now:

  2. export NVM_DIR="$HOME/.nvm"
  3. [ -s"$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
  4. [ -s"$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion
The output provides precise instructions, and you should reopen and close your terminal to add the nvm script path to the current session. Alternatively, you can execute the commands to get the same results.
Once you add the script to your PATH, run the command below to verify nvm installation:
  1. $ nvm --version
This will give you the following output:
  1. 0.33.11

Step 2 - Using NVM To Install Node.js

The NVM is successfully installed which means, we can easily install Node.js. Run the command below to install Node.js:
  1. $ nvm install node
The installation should not take long. Once done, run the command below to verify Node.js installation:
  1. $ node --version
This will give you the output below:
  1. v10.6.0
We can proceed and leverage the power of NVM to install two more Node.js versions. We can now install version 4.9.1 and the most recent  LTS version. To accomplish this, run the command below:
  1. $ nvm install --lts
  2. $ nvm install 4.9.1
Next, execute the following command, to view a list of the installed Node.js:
  1. $ nvm ls
This will give you the output below:
how to install node.js on ubuntu 18.04
From the above output, you can clearly see that v4.9.1 is the Node.js version utilized in the current session. Besides, its clear v10.6.0 is the default version; this Node.js version will be active once a new shell is opened.
You can run the command below to switch the current active Node.js version:
  1. $ nvm use 8.11.3
Next, execute the command below to verify it:
  1. $ nvm current
This will give you the following output:
  1. V8.11.3
To make v8.11.3 your default Node.js version run the command below:
  1. $ nvm alias default 8.11.3

Part 4: Further Implementations

Installing Development Tools

If you want to compile or install native add-ons from the Node.js Package Manager, you must install the required development tools. Now, execute the command below to install the required packages:
  1. $ sudo apt install gcc g++ make

Uninstalling Node.js

Run the command below, to uninstall npm packages as well as Node.js:
  1. $ sudo apt remove nodejs npm

Installing The Yarn Package Manager

Yarn is a helpful JavaScript Package Manager that integrates seamlessly with npm. The package is developed to remedy a plethora of issues with npm such as reducing network connectivity errors and speeding up the installation process.
To install Yarn, we’ll use the apt commands:

Step 1- Adding Yarn Yum Repository

Execute the commands below to add a GPG key and a repository:
  1. $ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
  2. $echo"deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Step 2 - Installing Yarn

Since the repository has been added successfully, you can now run the following commands to install Yarn:
  1. $ sudo apt update
  2. $ sudo apt install yarn
Alternatively, if you are utilizing the Node.js Version Manager, use the commands below to skip:
  1. $ sudo apt update
  2. $ sudo apt install --no-install-recommends yarn
Next, run the commands below to verify the installation:
  1. $ yarn --version
The output should be similar to:
  1. 1.7.0

Conclusion

That's it! The tutorial has detailed the three different methods you can use to install Node.js and its package manager (nvm). Of the three ways, it’s highly recommended you use the Node.js Version Manager as it delivers unprecedented flexibility for installing various versions of Node.js.

Comments

Popular posts from this blog

C# IEnumerable and IQueryable

The first important point to remember is IQueryable interface inherits from IEnumerable, so whatever IEnumerable can do, IQueryable can also do.   There are many differences but let us discuss about the one big difference which makes the biggest difference. IEnumerable interface is useful when your collection is loaded using LINQ or Entity framework and you want to apply filter on the collection. Consider the below simple code which uses IEnumerable with entity framework. It’s using a Wherefilter to get records whose EmpId is 2. EmpEntities ent = new EmpEntities(); IEnumerable<Employee> emp = ent.Employees;  IEnumerable<Employee> temp = emp.Where(x => x.Empid == 2).ToList<Employee>(); This where filter is executed on the client side where the IEnumerable code is. In other words all the data is fetched from the database and then at the client its scans and gets the record with EmpId is 2.   But now see the below code we have...