Cyberithub

How to Install NVM for Node.js on Ubuntu 18.04

Advertisements

In this tutorial, I will take you through the steps to install NVM for Node.js on Ubuntu 18.04. Node Version Manager(NVM) is the most widely used tool for node.js version management. This tool makes developers life little easy by providing the features of using and managing different node versions for multiple projects in a single system. This tool allows him to switch his node version back and forth with the use of a simple command.

What is NVM ?

NVM is known as Node Version Manager. It is used to install and manage various different versions of Node.js in a System. It is mostly required to test your code and libraries for backward compatibility.

How to Install NVM for Node.js on Ubuntu 18.04 1

Install NVM for Node.js

Before you go through the steps to install nvm for node.js in your system, it is very important to fulfill prerequisites criteria first.

Also Read: An Introduction to Javascript

Step 1: Prerequisites

a)You need to have a running Ubuntu 18.04 System.

b)You must logged in as root or any other user having sudo access to run all commands. In this tutorial, I am logged in as test user which has sudo access. Go to How to add User into Sudoers on Ubuntu 18.04 article to check how to provide sudo access to user.

Step 2: Update Your System

Before installing NVM in your server, you need to upgrade all the installed packages in your System using sudo apt-get upgrade command.

test@localhost:~$ sudo apt-get update
Hit:1 http://in.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 https://artifacts.elastic.co/packages/7.x/apt stable InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu bionic-backports InRelease
Get:5 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Fetched 88.7 kB in 1s (64.4 kB/s)
Reading package lists... Done

Step 3: Download and install NVM

Now you need to download and install NVM using below command. I have installed the latest Node Version Manager(NVM) from GITHUB Page. At the time of writing this tutorial, v0.35.2 is the latest one. You can go to GITHUB Page and find the latest one to install.

test@localhost:~$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash
=> 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

Step 4: Restart Terminal

You can restart terminal by below 3 ways. You can use any of them as per your convenience.

a)Start another bash prompt 

You can start a new bash prompt from current prompt by running exec bash command.

test@localhost:~$ exec bash

NOTE:

Please ensure test user has access to exec bash command otherwise it will throw an error "bash: exec: No such file or directory".

b)Login bash prompt using bash --login

You can also login to bash prompt using bash --login command.

test@localhost:~$ bash --login

c)Close and Open Terminal Again

Step 5: Check NVM Version

Once you successfully installed NVM in your system, you can check the installed nvm version by using nvm --version command.

test@localhost:~$ nvm --version
0.35.2

Step 6: Install Node.js through NVM

To install node.js in your system through nvm, you need to use nvm install node command. This will install the currently available latest version from repo.

test@localhost:~$ nvm install node
Downloading and installing node v13.6.0...
Downloading https://nodejs.org/dist/v13.6.0/node-v13.6.0-linux-x64.tar.xz...
################################################################################################################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v13.6.0 (npm v6.13.4)
Creating default alias: default -> node (-> v13.6.0)

Step 7: Check Node Version

If you want to check the installed node version, you can check by running node --version command.

test@localhost:~$ node --version
v13.6.0

Step 8: Install Latest LTS Version using --lts Option

You can also install the latest Node version by using nvm install --lts command as shown below.

test@localhost:~$ nvm install --lts
Installing latest LTS version.
Downloading and installing node v12.14.1...
Downloading https://nodejs.org/dist/v12.14.1/node-v12.14.1-linux-x64.tar.xz...
################################################################################################################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.14.1 (npm v6.13.4)

Step 9: Install Node.js Version 10.5

If you want to install a particular version say v10.5, you can do that by running nvm install 10.5 command.

test@localhost:~$ nvm install 10.5
Downloading and installing node v10.5.0...
Downloading https://nodejs.org/dist/v10.5.0/node-v10.5.0-linux-x64.tar.xz...
################################################################################################################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v10.5.0 (npm v6.1.0)

Step 10: Check all the Installed Version

To check all the currently installed versions of node, you can run nvm ls command. Notice from the output that currently you have v8.9.4, v10.5.0, v12.14.1 and v13.6.0 installed in your system. There is an arrow(->) showing in front of v10.5.0 which means currently this version is in use.

test@localhost:~$ nvm ls
v8.9.4
-> v10.5.0
v12.14.1
v13.6.0
default -> node (-> v13.6.0)
node -> stable (-> v13.6.0) (default)
stable -> 13.6 (-> v13.6.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/erbium (-> v12.14.1)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.18.1 (-> N/A)
lts/erbium -> v12.14.1

Step 11: Switch to Version 13.6.0

If you want to switch to Version 13.6.0, then you need to run nvm use v13.6.0 command to switch your node version.

test@localhost:~$ nvm use v13.6.0
Now using node v13.6.0 (npm v6.13.4)

Step 12: Uninstall Version 8.9 using NVM

If you want to uninstall any particular version let's say version 8.9, then you need to run nvm uninstall 8.9 command.

test@localhost:~$ nvm uninstall 8.9
Uninstalled node v8.9.4

Step 13: Update node through NVM

If you want to update your node to the latest stable version, then you need to use nvm install stable command. In our case, since the latest version 13.6.0 is already installed in our system so it will show the latest version is already installed and hence no update is currently available to install.

test@localhost:~$ nvm install stable
v13.6.0 is already installed.
Now using node v13.6.0 (npm v6.13.6)

 

Also Read: Steps to repair filesystem in rescue mode in RedHat/CentOS 7

 

Leave a Comment