Installing Node in Ubuntu

NVM

It is much easier to install Node and even maintain multiple versions of it using a script called NVM (a.k.a)Node Version Manager. It is a bash script that helps installing Node.

Downloading and running the NVM script

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

This command will clone the NVM repository from Github and add it to the ~/.nvm directory. The output of the command will be like below

=> 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

Once the we get the output as above, we can check the version of nvm by doing

nvm --version

This will give an output like

0.33.11

Installing the latest version of Node

To install the latest version simply execute

nvm install node

After executing the recently installed version becomes the active. We can check that by

aravamudhan@amudhan-device:~/development/amudhanbala$ node --version
v13.2.0

We can list the available versions and the active version by executing the following command

aravamudhan@amudhan-device:~/development/amudhanbala$ nvm ls
       v10.14.2
       v10.15.3
       v11.11.0
->      v13.2.0
default -> 10.15.3 (-> v10.15.3)
node -> stable (-> v13.2.0) (default)
stable -> 13.2 (-> v13.2.0) (default)
iojs -> N/A (default)
lts/* -> lts/erbium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.2 (-> N/A)
lts/dubnium -> v10.17.0 (-> N/A)
lts/erbium -> v12.13.1 (-> N/A)

The ->(arrow) points to the currently active version in this session. The default version is the one that will be active when a new session(by opening a new terminal) is created. To change the default version nvm alias default 10.15.3. To activate any version nvm use 11.11.0. Simply mentioning the version number along with the nvm use command switches the version of node.

Installing an LTS version of Node

To install the latest LTS version simply do

nvm install --lts

NPM

npm comes with node. No need to install npm separately. One can check by the command

aravamudhan@amudhan-device:~/development/amudhanbala$ npm --version
6.13.1