Ubuntek Banner

Install NVM (Node Version Manager) on Ubuntu

Programming & Development Beginner 10 minutes

Summary

NVM (Node Version Manager) is a tool that allows you to install and manage multiple versions of Node.js on your system. This tutorial will guide you through installing NVM and using it to manage Node.js versions.

Key Vocabulary

NVM: Node Version Manager - a tool for installing and managing multiple Node.js versions.
Node.js: A JavaScript runtime built on Chrome's V8 JavaScript engine.
LTS: Long Term Support - stable, recommended versions of Node.js for production use.
npm: Node Package Manager - the default package manager for Node.js.
Shell Profile: Configuration files that set up your shell environment when you log in.

Prerequisites

  • Ubuntu 18.04 or later
  • Internet connection
  • Basic command line knowledge

Step-by-Step Instructions

Step 1

Update Package List

Update your system's package list.

sudo apt update
Why this works: Ensures you get the latest package versions.
Step 2

Install curl

Install curl if it's not already available on your system.

sudo apt install curl
Why this works: curl is required to download the NVM installation script from GitHub.
Step 3

Download and Install NVM

Download and run the NVM installation script.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Why this works: This downloads the NVM installation script and runs it to install NVM on your system.
Step 4

Load NVM in Current Session

Load NVM into your current shell session.

source ~/.profile
Why this works: This loads NVM into your current shell session so you can use it immediately.
Step 5

Verify NVM Installation

Check that NVM is properly installed.

nvm --version
Why this works: This command displays the NVM version to confirm the installation was successful.
Step 6

Install Latest LTS Node.js

Install the latest Long Term Support version of Node.js.

nvm install --lts
Why this works: Installs the latest LTS version of Node.js, which is recommended for most users.
Step 7

Install Specific Node.js Version

Install a specific version of Node.js (version 20 in this example).

nvm install 20
Why this works: Installs Node.js version 20. You can replace '20' with any version number you need.
Step 8

List Installed Node.js Versions

View all Node.js versions installed on your system.

nvm ls
Why this works: Lists all Node.js versions installed via NVM, showing which one is currently active.
Step 9

Switch to LTS Version

Switch to using the LTS version of Node.js.

nvm use --lts
Why this works: Sets the LTS version as the active Node.js version for the current session.

Common Issues & Solutions

Problem: Command 'nvm' not found after installation
Solution: Run 'source ~/.profile' or restart your terminal. NVM needs to be loaded into your shell session.
Problem: Permission denied when installing NVM
Solution: The installation script should be run as your regular user, not with sudo. NVM installs to your home directory.
Problem: curl command not found
Solution: Install curl first with 'sudo apt install curl' before running the NVM installation.
Problem: NVM installation fails with network error
Solution: Check your internet connection and try again. The script downloads from GitHub.

Conclusion

Congratulations! You have successfully installed NVM and learned how to manage Node.js versions. NVM makes it easy to switch between different Node.js versions for different projects.

Next steps:

  • Install your preferred Node.js version with 'nvm install [version]'
  • Switch between versions with 'nvm use [version]'
  • Set a default version with 'nvm alias default [version]'
  • Check available versions with 'nvm ls-remote'

NVM will automatically load when you open a new terminal session, so you can start using it immediately!