How To Install NVM on Ubuntu 22.04

Here is a step-by-step guide to install NVM (Node Version Manager) on Ubuntu 22.04:

Step 1 : Install the required packages:

sudo apt-get update
sudo apt-get install build-essential libssl-dev curl

Step 2 : Download the NVM installation script:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

To determine the latest version of NVM, you can go to the NVM repository on Github: https://github.com/nvm-sh/nvm.

Step 3 : Load the NVM environment variables:

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 

Step 4 : Verify the NVM installation:

nvm --version

Step 5 : Use NVM to install the latest version of Node.js:

nvm install node

Step 6 : Use NVM to install a specific version of Node.js:

nvm install <version>

Replace <version> with the desired version number (e.g. 16).

Step 7 : To list all installed versions of Node.js:

nvm ls

Step 8 : Use the following command to switch to the desired version:

nvm use <version>

Step 9 : Verify the currently active version of Node.js:

node -v

Step 10 : To set a default version of Node.js:

nvm alias default <version>

Replace <version> with the desired version number or node for the latest version.