Introduction
NVM (Node Version Manager) allows you to install and manage multiple Node.js versions on a single Ubuntu machine. This is especially useful when working on multiple projects requiring different Node versions.
Step 1 — Update System Packages
sudo apt update && sudo apt upgrade -y
Step 2 — Install Required Dependencies
sudo apt install curl build-essential -y
Step 3 — Download and Install NVM
curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Step 4 — Load NVM into Your Shell
source ~/.bashrc
Or if using ZSH:
source ~/.zshrc
Step 5 — Verify NVM Installation
nvm --version
Step 6 — Install Latest Node.js Version
nvm install node
Step 7 — Install a Specific Node.js Version
nvm install 18
Step 8 — List Installed Node Versions
nvm ls
Step 9 — Switch Between Node Versions
nvm use 18
Step 10 — Set a Default Node Version
nvm alias default 18
Step 11 — Check Current Node & NPM Version
node -v
npm -v
Step 12 — Uninstall a Node Version (Optional)
nvm uninstall 16
Useful NVM Commands
| Command | Description |
|--------|------------|
| nvm install node | Install latest Node |
| nvm install 18 | Install Node 18 |
| nvm use 18 | Switch Node version |
| nvm ls | List installed versions |
| nvm alias default 18 | Set default Node |
| nvm uninstall 16 | Remove Node version |
Troubleshooting
If nvm command not found:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
Conclusion
NVM makes Node.js version management easy and flexible.
Author
Marquefactory DevOps Team
