How to use NVM to install NodeJS in Ubuntu 18.04

nvm nodejs npm

While direct installation can be done, it might be better to do it by using nvm to install nodejs.

If there are already any nodejs or npm version installed on your system and you want to remove it, you can do so by (not mandatory)

sudo apt remove nodejs
sudo apt remove npm

First let’s get started with installing Node Version Manager (nvm)

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

All the details of nvm can be found on it’s github page.
This will download the nvm package to our local, install it and set the path of nvm in bash.

The terminal will ask to restart to be able to load the new command.
Restart the terminal and type in

nvm --version

This will return the currently installed version. For me, the result was

0.33.11

Then we can start with the installation of node.
The next few commands can be run independently, so make sure that you run the one as per your requirement.

1. Installing the latest version of node

nvm install node

The version installed by the above command will be the default node version.
To verify if node actually got installed, you can type

node --version
or
node -v

For me it resulted in

v12.6.0

2. To install the long term release (lts) version (which you should be using for production level applications), type

nvm install --lts

3. To install any particular versions, type

npm install 4.9.1

To see the all the node versions installed/available, type

nvm ls

For me, this resulted in

        v10.16.0
 ->      v12.6.0
          system
 default -> node (-> v12.6.0)
 node -> stable (-> v12.6.0) (default)
 stable -> 12.6 (-> v12.6.0) (default)
 iojs -> N/A (default)
 lts/* -> lts/dubnium (-> v10.16.0)
 lts/argon -> v4.9.1 (-> N/A)
 lts/boron -> v6.17.1 (-> N/A)
 lts/carbon -> v8.16.0 (-> N/A)
 lts/dubnium -> v10.16.0

The one with the arrow mark, signifies the default node version.
We can change the current active version by typing in

nvm use 10.16.0

This results in

Now using node v10.16.0 (npm v6.9.0)

To change the default node version,

nvm alias default 10.16.0

Results in

default -> 10.16.0 (-> v10.16.0)

To uninstall any version, we can use

nvm uninstall 12.6.0
or
nvm uninstall node // This will remove the latest node version installed

Before firing the uninstall command, please make sure to change the active version to any other version (by using nvm use version_no, else it won’t uninstall, and would throw the error

nvm: Cannot uninstall currently-active node version, v12.6.0 (inferred from 12.6.0).

We can check for the changed status by typing nvm ls everytime you execute any command.

Using nvm to install nodejs will help in the long run when we want are working on different projects having different node versions.

Read More:
Solution – FTP Access not Showing Any Files/Directory

Leave a Reply