Installing NVM and Node.js on Ubuntu
Install NVM (Node Version Manager)​
-
Update your package index:
sudo apt update
-
Install prerequisites:
sudo apt install curl build-essential -y
-
Download and run the NVM installation script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
-
Close and reopen your terminal, or run this to apply changes immediately:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" -
Verify NVM installation:
nvm --version
Install Node.js using NVM​
-
To install the latest LTS (Long Term Support) version:
nvm install --lts
Or install a specific version:
nvm install 18.18.0
-
Set your default Node.js version:
nvm alias default 'lts/*'
Or specify a version:
nvm alias default 18.18.0
-
Verify the installation:
node --version
npm --version
Managing Node.js Versions with NVM​
-
List installed versions:
nvm ls
-
List available versions to install:
nvm ls-remote
-
Switch between versions:
nvm use 16.20.0
NVM makes it easy to switch between different Node.js versions for different projects.