How to Install Node.js on Ubuntu 23.04

Login to the Ubuntu 23.04 server.

Install stable Node.js from the Ubuntu repository by running the following command.

apt install nodejs

Output:

root@ubuntu:~# apt install nodejs
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libc-ares2 libnode108 node-acorn node-busboy node-cjs-module-lexer
  node-undici node-xtend nodejs-doc
Suggested packages:
  npm
The following NEW packages will be installed:
  libc-ares2 libnode108 node-acorn node-busboy node-cjs-module-lexer
  node-undici node-xtend nodejs nodejs-doc
0 upgraded, 9 newly installed, 0 to remove and 2 not upgraded.

Then check the version of the Node.js once the installation is done.

node --version

Output:

root@ubuntu:~# node --version
v18.13.0

Installing different version of Node.js from source

Install NVM manager by using "wget".

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

Output:

root@ubuntu:~# wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.39.3/install.sh | bash
=> Downloading nvm from git to '/root/.nvm'
=> Cloning into '/root/.nvm'...
remote: Enumerating objects: 359, done.
remote: Counting objects: 100% (359/359), done.
remote: Compressing objects: 100% (305/305), done.
remote: Total 359 (delta 40), reused 169 (delta 28), pack-reused 0
Receiving objects: 100% (359/359), 219.46 KiB | 1.73 MiB/s, done.
Resolving deltas: 100% (40/40), done.
* (HEAD detached at FETCH_HEAD)
  master
=> Compressing and cleaning up git repository

=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> 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

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@ubuntu:~# nvm --version
0.39.3

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@ubuntu:~# nvm ls-remote
    v0.1.14
    v0.1.15
    v0.1.16
    v0.1.17
    v0.1.18

v18.7.0
    v18.8.0
    v18.9.0
    v18.9.1
   v18.10.0
   v18.11.0
   v18.12.0   (LTS: Hydrogen)
   v18.12.1   (LTS: Hydrogen)
   v18.13.0   (LTS: Hydrogen)
   v18.14.0   (LTS: Hydrogen)
   v18.14.1   (LTS: Hydrogen)
   v18.14.2   (LTS: Hydrogen)
   v18.15.0   (LTS: Hydrogen)
   v18.16.0   (Latest LTS: Hydrogen)
    v19.0.0
    v19.0.1
    v19.1.0
    v19.2.0
    v19.3.0
    v19.4.0
    v19.5.0
    v19.6.0
    v19.6.1
    v19.7.0
    v19.8.0
    v19.8.1
    v19.9.0
    v20.0.0

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

eg: Here v20.0.0

nvm install 20.0.0

Output:

root@ubuntu:~# nvm install 20.0.0
Downloading and installing node v20.0.0...
Downloading https://nodejs.org/dist/v20.0.0/node-v20.0.0-linux-x64.tar.xz...
###################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v20.0.0 (npm v9.6.4)
Creating default alias: default -> 20.0.0 (-> v20.0.0)
root@ubuntu:~# node -v
v20.0.0

And check the version of the Node.js once the installation is done.

node -v

Output:

root@ubuntu:~# node -v
v20.0.0

Done!