Back

How to Switch Node.js Versions on Linux using NVM (Step-by-Step Guide)

How to Switch Node.js Versions on Linux using NVM (Step-by-Step Guide)

Managing different Node.js versions can become messy, especially if you work on multiple projects that each require their own Node.js setup. Node Version Manager (NVM) makes switching between Node.js versions on Linux easy and clean. In this guide, you’ll learn exactly how to install NVM, switch Node versions, set defaults, and quickly resolve common issues.

Key Takeaways

  • NVM helps manage multiple Node.js versions without conflicts.
  • Always run NVM and Node.js commands without sudo to avoid permission issues.

Step 1: Install NVM on Linux

Run this command in your terminal:

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

This script downloads and installs NVM automatically.

Verify Installation

Reload your shell:

source ~/.bashrc  # For Bash users
source ~/.zshrc   # For Zsh users

Then verify NVM installation:

nvm --version

You should see the version number.

Troubleshooting installation

If you encounter nvm: command not found:

  • Check your ~/.bashrc or ~/.zshrc has these lines:
export NVM_DIR=""$HOME/.nvm""
[ -s ""$NVM_DIR/nvm.sh"" ] && . ""$NVM_DIR/nvm.sh""
  • After adding these lines, reload your shell again.

Step 2: Install and Manage Node.js Versions

Install the Latest Node.js Version

Install the latest Node.js:

nvm install node  # latest release
nvm install --lts # latest Long Term Support (LTS) version

Install a Specific Node.js Version

Find and install a specific Node.js version (e.g., 20.11.0):

nvm ls-remote
nvm install 20.11.0

List Installed Versions

Check installed Node versions:

nvm ls

Step 3: Switch Between Node.js Versions

Switch Node.js versions instantly:

nvm use 20.11.0

Verify the active version:

node -v

Set a Default Node.js Version

Set default Node.js for new terminals:

nvm alias default 20.11.0

Step 4: Using .nvmrc for Project-Specific Versions

Create .nvmrc file in your project folder with Node version inside (e.g., 20.11.0). Then use:

nvm use

NVM automatically picks the version from the .nvmrc file.

Common Issues and Quick Solutions

Node Version Not Changing

  • Ensure you’re not using a system-installed Node.js by mistake.
  • Run which node to confirm NVM-managed Node is active.
  • Never use sudo for NVM commands or installing npm packages.

Missing Global npm Packages After Switching Versions

Each Node.js version manages its own global npm packages. To migrate global packages:

nvm install 20 --reinstall-packages-from=18

Replace ""18"" with the Node version you want to migrate from.

Updating NVM

Run the install script again to update NVM seamlessly:

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

This updates NVM without affecting installed Node versions.

Conclusion

Using NVM simplifies managing multiple Node.js environments significantly. It enables quick switching, avoids version conflicts, and keeps your Linux development workflow smooth.

FAQs

No. NVM installs Node.js versions in your home directory, so root permissions (`sudo`) aren't needed and shouldn't be used.

NVM works perfectly on macOS. Windows users should use NVM for Windows, a similar but separate tool.

Ensure the NVM setup lines are in your shell profile (`~/.bashrc`, `~/.zshrc`), then reload the shell or restart your terminal.

Each terminal session can have a different active Node.js version managed by NVM, but a single session can only run one version at a time.

Listen to your bugs 🧘, with OpenReplay

See how users use your app and resolve issues fast.
Loved by thousands of developers