How to Install Node.js on Ubuntu 21.10

Login to the Ubuntu 21.04 server.

Install stable NodeJS from Ubuntu repository by running the following command.

apt install nodejs

Output:

root@crowncloud:~# apt install nodejs
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
  npm
The following NEW packages will be installed:
  nodejs
0 upgraded, 1 newly installed, 0 to rem

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

node --version

Output:

root@crowncloud:~# node --version
v12.22.5

Installing a different version of Node.js from the source.

Install NVM manager by using wget.

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

Output:

root@crowncloud:~#        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 102 (delta 24), pack-reused 0
Receiving objects: 100% (264/264), 116.38 KiB | 3.88 MiB/s, done.
Resolving deltas: 100% (31/31), done.
Note: switching to '7ad6d98cedde01809e32d56ab8ced064f6f28175'.

Update your shell ~/.profile by running the following command

  source ~/.profile

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

nvm --version

Output:

root@crowncloud:~# nvm --version
0.33.8

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

  nvm ls-remote

Output:

root@crowncloud:~# nvm ls-remote
    v0.1.14
    v0.1.15
    v0.1.16
    v0.1.17
    v0.1.18
    v0.1.19
    v0.1.20
    v0.1.21
    v0.1.22
    v0.1.23
    v0.1.24
    v0.1.25

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

In this example, we will install version 16.

  nvm install v16.0.0

Output:

root@crowncloud:~# 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.23.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v7.23.0

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

  node -v

Output:

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

Done.