How to Install Node.js on Ubuntu 22.04

Login to the Ubuntu 22.04 server.

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

apt install nodejs

Output:

root@crown:~# apt install nodejs
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  apache2-data apache2-utils
Use 'apt autoremove' to remove them.
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

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

node --version

Output:

root@crown:~# node --version
v14.1.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@crown:~# 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.

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 the latest version.

nvm ls-remote

Output:

root@vps:~# nvm ls-remote
        v0.1.14
        v0.1.15
        v0.1.16
        ....
        v13.12.0
        v13.13.0
        v13.14.0
        v14.0.0
        v14.1.0

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

eg: Here v14.1.0

nvm install 14.1.0

Output:

root@crown:~# nvm install 14.1.0
Downloading and installing node v14.1.0...
Downloading https://nodejs.org/dist/v14.1.0/node-v14.1.0-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.1.0 (npm v6.14.4)
Creating default alias: default -> 14.1.0 (-> v14.1.0)

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

node -v

Output:

root@crown:~# node -v
v14.1.0

Done!