How to Install Node.js on Ubuntu 21.04 LTS.

Login to the Ubuntu 21.04 server.

Install stable nodejs from Ubuntu respository by running the following command.

apt install nodejs

Output:

root@vps:~# apt install nodejs
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libc-ares2 libjs-highlight.js libnode72 nodejs-doc
Suggested packages:
  npm
The following NEW packages will be installed:
  libc-ares2 libjs-highlight.js libnode72 nodejs nodejs-doc
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 11.5 MB of archives.
After this operation, 51.3 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Then check the version of the nodejs once installation is done.

node --version

Output:

root@vps:~# node --version
v12.21.0

Installing different version of Node.js from source.

Install NVM manager by using wget.

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash

Output:

root@vps:~# wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
=> Downloading nvm from git to '/root/.nvm'
=> Cloning into '/root/.nvm'...
remote: Enumerating objects: 264, done.
remote: Counting objects: 100% (264/264), done.
remote: Compressing objects: 100% (230/230), done.
remote: Total 264 (delta 31), reused 100 (delta 24), pack-reused 0
Receiving objects: 100% (264/264), 116.38 KiB | 5.29 MiB/s, done.
Resolving deltas: 100% (31/31), done.

Update your shell ~/.profile by running following command

source ~/.profile

Next, Check the if NVM installed or not by checking for its version.

nvm --version

Output:

root@vps:~# nvm --version
0.33.8

List out the all available Node.js versions. you can see all Node.js versions from the first version to latest version.

nvm ls-remote

Output:

root@vps:~# nvm ls-remote
    v0.1.14
    v0.1.15
    v0.1.16
    ....
        ....
        ....
        ....
    v14.16.0   (LTS: Fermium)
        v14.16.1   (Latest LTS: Fermium)
        v15.0.0
        v15.0.1
        v15.1.0
        v15.2.0
        v15.2.1
        v15.3.0
        v15.4.0
        v15.5.0
        v15.5.1
        v15.6.0
        v15.7.0
        v15.8.0
        v15.9.0
       v15.10.0
       v15.11.0
       v15.12.0
       v15.13.0
       v15.14.0
        v16.0.0

Now install Node.js with the version you want by running nvm install command.

In this example, we will install version 16.

nvm install v16.0.0

Output:

root@vps:~# nvm install v16.0.0
Downloading and installing node v16.0.0...
Downloading https://nodejs.org/dist/v16.0.0/node-v16.0.0-linux-x64.tar.xz...
########################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
npm notice
npm notice New minor version of npm available! 7.10.0 -> 7.11.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v7.11.2
npm notice Run npm install -g npm@7.11.2 to update!
npm notice
Now using node v16.0.0 (npm v7.10.0)
Creating default alias: default -> v16.0.0

And check the version of the nodejs once installation is done.

node -v

Output:

root@vps:~# node -v
v16.0.0

Done!