How to install NodeJs on Ubuntu 23.04

Let us see 3 easy methods of installing NodeJS on Ubuntu while also covering the steps for removing NodeJS from the Ubuntu machine.
This post tries to capture the steps that we need to set up Node.JS on our machine running Ubuntu 23.04. The same set of steps can be used to install NodeJS on most of the recent versions of Ubuntu.
3 popular ways of installing Node.JS on Ubuntu
1. Using apt to install from the default repository:
sudo apt update
sudo apt install nodejs
node -v
sudo apt install npm
npm -v
2. Using a NodeSource PPA with apt:
cd ~
curl -sL https://deb.nodesource.com/setup_18.x -o /tmp/nodesource_setup.sh
cat /tmp/nodesource_setup.sh
sudo bash /tmp/nodesource_setup.sh
sudo apt install nodejs
node -v
npm -v
3. Using Node Version Manager (NVM):
This is popular among the three as it is quite flexible in terms of use and allows you to easily install multiple versions of node and switch between them. Practically very handy if you have multiple projects and some of them depend on a particular version of node. For more, visit its GitHub page.
Download and run the install script using cURL:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
Update bashrc to use nvm command (required only if bash fails to recognize nvm):
source ~/.bashrc
Get the list of node versions available:
nvm list-remote
Install the latest LTS (long-term support) release:
nvm install --lts
Verify the installation:
node -v
npm -v
Uninstall Node.JS on Ubuntu
Pick a method depending on what was used to install Node.Js - apt or NVM:
1. If apt was used to install NodeJS
sudo apt remove nodejs
sudo apt purge nodejs
2. If NVM was used to install NodeJS Check the currently active version on Node:
nvm current
If the target is not the active version:
nvm uninstall node_version
If the target is active version:
nvm deactivate
nvm uninstall node_version
replace node_version
with the actual version number that you want to uninstall