Multiple Node.js versions can coexist on a single Linux system using NVM. This guide walks you through exactly how to install NVM on Linux step by step. You’ll have a flexible Node.js environment ready in minutes.
NVM stands for Node Version Manager. It lets you switch between different Node.js versions easily. This is super useful when working on multiple projects that require different Node versions.
What Is NVM And Why Use It
NVM is a command-line tool that manages multiple Node.js installations. Instead of installing one global Node version, you can have several versions side by side. You switch between them with a single command.
Developers love NVM because it solves version conflicts. One project might need Node 14, while another requires Node 18. Without NVM, you’d have to uninstall and reinstall each time. With NVM, you just change the active version.
Key Benefits Of Using NVM
- Install multiple Node versions without conflicts
- Switch between versions instantly
- Set default Node version per project
- Test your code across different Node environments
- Uninstall old versions cleanly
Prerequisites For Installing NVM On Linux
Before you start, make sure your system meets these requirements. You need a Linux distribution with standard tools installed.
- A Linux system (Ubuntu, Debian, CentOS, Fedora, or similar)
- Internet connection for downloading packages
- Basic terminal knowledge
- curl or wget installed (most systems have these)
- Git (optional but recommended)
Check if curl is available by running curl --version in your terminal. If not, install it with your package manager. For Ubuntu or Debian, use sudo apt install curl.
How To Install Nvm On Linux
Now we get to the main part. Follow these steps carefully to install NVM on your Linux system. The process is straightforward and takes about five minutes.
Step 1: Download The NVM Installation Script
Open your terminal. You’ll use either curl or wget to download the script. The official NVM repository provides a simple installation command.
Using curl:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Using wget:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Both commands do the same thing. They download the installation script and run it immediately. The script clones the NVM repository to your home directory under ~/.nvm.
Step 2: Configure Your Shell Profile
After the script runs, it adds NVM configuration to your shell profile. This is usually ~/.bashrc, ~/.zshrc, or ~/.profile. The script automatically detects your shell and updates the right file.
If you use Bash, check your ~/.bashrc file. You should see lines like these:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
If the script didn’t add these lines automatically, add them manually. This ensures NVM loads every time you open a terminal.
Step 3: Reload Your Shell Configuration
To apply the changes without closing your terminal, reload the configuration file. Run one of these commands depending on your shell.
For Bash:
source ~/.bashrc
For Zsh:
source ~/.zshrc
Alternatively, you can simply close and reopen your terminal. This also loads the new configuration.
Step 4: Verify The Installation
Check if NVM installed correctly by running:
nvm --version
You should see the version number printed, like 0.39.7. If you get a “command not found” error, something went wrong. Double-check your shell configuration file.
Installing Node.js With NVM
Now that NVM is installed, you can install Node.js versions. NVM makes this incredibly simple.
Install The Latest LTS Version
Long-term support (LTS) versions are recommended for most projects. Install the latest LTS with:
nvm install --lts
This downloads and compiles Node.js. The process takes a minute or two depending on your internet speed.
Install A Specific Version
You can install any Node version by specifying the version number. For example:
nvm install 18.17.0
Or install the latest version in a major release line:
nvm install 20
This installs the latest Node 20.x version available.
List Available Versions
See all Node versions you can install:
nvm ls-remote
This shows a long list. Use nvm ls-remote --lts to see only LTS versions.
Managing Node.js Versions With NVM
Once you have multiple versions installed, you need to manage them. Here are the essential commands.
Switch Between Versions
To use a specific version:
nvm use 18.17.0
To use the latest LTS:
nvm use --lts
To use the system-installed Node (if any):
nvm use system
Set A Default Version
Set a default Node version that loads automatically in new terminals:
nvm alias default 18.17.0
Now every time you open a terminal, Node 18.17.0 will be active.
List Installed Versions
See which versions you have installed:
nvm ls
The output shows all installed versions. The current active version has an arrow next to it.
Uninstall A Version
Remove a version you no longer need:
nvm uninstall 14.21.3
This cleans up the Node binary and associated files.
Using NVM With Project-Specific Node Versions
NVM can automatically switch Node versions when you enter a project directory. This is a game-changer for team projects.
Create A .Nvmrc File
In your project root, create a file named .nvmrc. Add the Node version you want, like:
18.17.0
Or use a version range:
lts/hydrogen
Auto-Switch With Shell Integration
NVM can read the .nvmrc file automatically. Add this to your shell configuration file (~/.bashrc or ~/.zshrc):
cd() {
builtin cd "$@"
if [[ -f .nvmrc ]]; then
nvm use
fi
}
Now when you cd into a project with an .nvmrc file, NVM switches to the specified version.
Troubleshooting Common NVM Installation Issues
Sometimes things don’t go smoothly. Here are solutions to common problems.
Command Not Found After Installation
If nvm isn’t recognized, your shell configuration might not be loaded. Try:
source ~/.bashrc
If that doesn’t work, check if the NVM directory exists:
ls -la ~/.nvm
If the directory is missing, re-run the installation script.
Permission Denied Errors
NVM installs in your home directory, so you shouldn’t need sudo. If you get permission errors, check file ownership:
ls -la ~/.nvm
Make sure your user owns the directory. If not, change ownership with:
sudo chown -R $USER:$USER ~/.nvm
Proxy Or Network Issues
If you’re behind a corporate proxy, NVM might fail to download. Set your proxy environment variables:
export HTTP_PROXY=http://proxy.example.com:8080
export HTTPS_PROXY=http://proxy.example.com:8080
Then run the installation again.
Advanced NVM Configuration
Once you’re comfortable with basic NVM usage, explore these advanced features.
Install Node From Source
NVM can compile Node from source instead of downloading binaries:
nvm install -s 18.17.0
This takes longer but gives you a custom build.
Use Different Architecture
On 64-bit systems, you can install 32-bit Node:
nvm install 18.17.0 32
Migrate Global Packages
When switching Node versions, you might want to reinstall global packages. NVM can help:
nvm reinstall-packages 16.20.0
This reinstalls all global packages from the specified version to the current one.
Uninstalling NVM From Linux
If you ever need to remove NVM completely, follow these steps.
Remove The NVM Directory
rm -rf ~/.nvm
Clean Up Shell Configuration
Edit your ~/.bashrc or ~/.zshrc file. Remove the lines that reference NVM. These are the export and source lines added during installation.
Remove NPM Cache
rm -rf ~/.npm
This clears any leftover npm data.
Frequently Asked Questions
What Is The Difference Between NVM And N?
NVM and n are both Node version managers. NVM is more popular and works on Linux and macOS. n is simpler but has fewer features. NVM supports per-project version switching with .nvmrc files, which n doesn’t do natively.
Can I Use NVM With Fish Shell?
Yes, but you need additional configuration. Fish shell uses a different syntax. Install NVM first, then use a plugin like bass to make it work. Alternatively, use nvm.fish which is a fish-compatible wrapper.
Does NVM Work On Windows?
NVM is designed for Linux and macOS. For Windows, use nvm-windows, which is a separate project. The commands are similar but not identical. The official NVM doesn’t support Windows natively.
How Do I Update NVM Itself?
Update NVM by running the installation script again. It overwrites the existing installation. You can also use nvm upgrade if you have a recent version. Check the official repository for the latest version number.
Why Should I Use NVM Instead Of A Package Manager?
Package managers like apt or yum install one global Node version. NVM gives you multiple versions with easy switching. This is essential for testing across Node versions or working on legacy projects. Package managers also lag behind in updates, while NVM gets the latest Node releases quickly.
Final Tips For Using NVM Effectively
You now know how to install NVM on Linux and use it productively. Here are some final recommendations.
Always use LTS versions for production projects. They receive security updates and are stable. For development, feel free to try the latest features with current versions.
Create .nvmrc files in all your projects. This helps team members use the correct Node version automatically. It also documents the required version for future reference.
Keep your NVM installation updated. New versions bring bug fixes and support for newer Node releases. Check the official repository periodically for updates.
If you encounter issues, check the NVM GitHub repository. The community is active and helpful. Most common problems have documented solutions.
With NVM installed, you have full control over your Node.js environment. You can switch between versions, test compatibility, and work on any project without version conflicts. This makes you a more efficient and flexible developer.