sodcalculator

10 Tech How-To

Install NVM on Ubuntu: Step-by-Step Guide

Guide 10-5382 · Updated · Step by step

To install NVM (Node Version Manager) on Ubuntu, you run a single curl command from the official GitHub repository, then source your shell configuration. This guide walks you through the exact steps, explains how NVM works, and covers common issues you might hit.

What Is NVM and Why Use It?

NVM is a shell script that lets you install, switch, and manage multiple versions of Node.js on a single machine. Instead of installing one global Node.js via apt or a binary tarball, NVM keeps each version in your home directory under ~/.nvm. You can then switch between versions with a single command, which is essential when you work on multiple projects that require different Node.js releases.

For example, an older Laravel project might need Node 14, while a new React app requires Node 20. With NVM, you can have both installed and switch instantly without uninstalling anything. It also lets you test your code against multiple Node versions, which is useful for open-source maintainers and CI environments.

Prerequisites

Before you begin, ensure your Ubuntu system has the following:

  • A user account with sudo privileges (you’ll need it to install dependencies).
  • An internet connection.
  • Basic familiarity with the terminal.

You also need curl or wget. Most Ubuntu installations have curl already, but if not, install it with:

sudo apt update && sudo apt install curl

Step-by-Step Installation

  1. Download the install script from the official NVM GitHub repository. As of this writing, the latest version is v0.39.7. Run:
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

    This downloads the script and executes it in your current shell. The script clones the NVM repository into ~/.nvm and adds the necessary lines to your shell profile.

  2. Verify the installation by opening a new terminal or sourcing your profile:
    source ~/.bashrc

    Then check the version:

    nvm --version

    If you see 0.39.7 (or similar), NVM is installed.

  3. Install a Node.js version with:
    nvm install --lts

    This installs the latest Long-Term Support version. To install a specific version, use nvm install 18.20.4.

  4. Set a default Node version (optional) so that new shells use it automatically:
    nvm alias default 18.20.4

How NVM Works

NVM is not a separate binary; it’s a shell function. When you run nvm use 18, it modifies your PATH environment variable to point to the directory containing that Node version’s binaries. This is why you must source your shell profile after installation—the function definitions need to be loaded into your current shell session.

The install script adds the following block to your ~/.bashrc (or ~/.zshrc if you use Zsh):

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

Each Node version is stored in ~/.nvm/versions/node/. When you switch, NVM updates the PATH variable to point to the selected version’s bin directory. The node, npm, and npx commands then resolve to that version.

Common Problems and Fixes

“nvm: command not found” after installation is the most frequent issue. This usually happens because the shell hasn’t been reloaded. Run source ~/.bashrc or open a new terminal. If it persists, check that the NVM lines were added to your profile file. For Zsh users, the script adds to ~/.zshrc—make sure you’re sourcing the correct file.

Permission errors when installing Node versions. NVM installs to your home directory, so you shouldn’t need sudo. If you see permission issues, check the ownership of ~/.nvm with ls -la ~/.nvm. If it’s owned by root, change it back to your user: sudo chown -R $(whoami) ~/.nvm.

NVM not persisting after reboot. This is normal if you’re using a non-login shell. Ensure the NVM lines are in ~/.bashrc (for Bash) or ~/.zshrc (for Zsh). If you use a custom shell like Fish, you’ll need to install the Fish port of NVM separately.

NVM vs. Other Node Installers

On Ubuntu, you have several ways to install Node.js:

  • apt (sudo apt install nodejs) gives you an older version that may not be ideal for modern development. It’s simple but often outdated.
  • NodeSource PPA provides newer versions but you have to manually switch if you need multiple versions.
  • nvm is the only one that allows easy version switching without affecting system packages. It’s the recommended approach for developers.

One downside of NVM is that every new shell session requires the function to load, which adds a small delay. Also, since NVM installs to your home directory, it isn’t available to other system users unless they install it themselves.

Frequently Asked Questions

How do I uninstall NVM?

To completely remove NVM, delete the ~/.nvm directory and remove the lines that were added to your shell profile. Run rm -rf ~/.nvm and then edit ~/.bashrc (or ~/.zshrc) to delete the NVM export and sourcing lines. After that, reload your shell.

Can I use NVM with Zsh or Fish?

Yes. The official install script automatically detects Zsh and adds the necessary lines to ~/.zshrc. For Fish, you need to install a separate port called nvm-fish or use fisher to install it.

What is the difference between nvm and npm?

NVM manages Node.js versions, while npm is the package manager that comes with Node. NVM installs Node, and npm is included with it. You use npm to install packages like Express or React, not to manage Node versions.

How do I switch between Node versions?

Use nvm use <version> to switch for the current session. To set a default for new shells, use nvm alias default <version>. You can also create project-specific .nvmrc files and run nvm use to auto-switch when you cd into a directory.

Why is my installed Node version not showing up?

This usually happens if you installed Node via apt before installing NVM. The system’s /usr/bin/node might be taking precedence. Run nvm use <version> to override it, or uninstall the apt version with sudo apt remove nodejs.

Keeping NVM Updated

To update NVM itself, you can re-run the install script with the latest version number. Alternatively, you can use the built-in updater:

nvm upgrade

This command checks for the latest release and updates your installation. It’s a good idea to update NVM periodically to get bug fixes and support for newer Node versions.

Now that you have NVM installed, you can start using multiple Node.js versions effortlessly. Whether you’re a beginner or a seasoned developer, NVM is a must-have tool for any Ubuntu development environment.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →