DEV Community

Cover image for How to install and use NVM
Megan Lee for LogRocket

Posted on • Originally published at blog.logrocket.com

3

How to install and use NVM

Written by Carlos Mucuho✏️

NVM, or Node Version Manager, is a command-line tool that simplifies the process of managing multiple active Node.js versions. It lets you install, switch between, and manage different Node.js installations without conflicts.

Managing Node.js versions can be challenging, especially when different projects require different Node.js versions. Directly installing Node.js globally can lead to conflicts and broken projects. NVM solves this by providing isolated Node.js environments for each project.

Installing NVM

Before installing NVM, it's recommended that any existing Node.js or npm installations be removed to avoid potential conflicts.

Installing NVM on macOS and Linux

The NVM install process on Unix-based systems is straightforward: 1. Open your terminal and run the following command:

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

(Replace v0.40.1 with the latest version number if needed. Check the NVM GitHub page for the latest version.) 2. After the NVM install completes, either restart your terminal or run these commands:

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
Enter fullscreen mode Exit fullscreen mode

Installing NVM on Windows

NVM also supports Windows, but it’s typically through WSL (Windows Subsystem for Linux), depending on the version of WSL. If that doesn’t work, you can try a separate project called NVM for Windows (nvm-windows):

  1. Download the latest NVM for Windows installer from the official GitHub repository — NVM Windows releases
  2. Run the installer and follow the on-screen instructions. The setup wizard will guide you through the process, allowing you to choose the installation directory

Using NVM

Now that NVM is installed, let's explore how to use it to manage your Node.js versions:

Listing available Node.js versions

Before installing any Node.js versions, it's helpful to see what's available. You can do this by using the following command:

nvm ls-remote  # On Unix systems
nvm list available  # On Windows ( nvm-windows ) 
Enter fullscreen mode Exit fullscreen mode

The command will return a list of all the Node.js versions that NVM can install, including stable releases, LTS versions, and even older, less common versions. If you are using nvm-windows make sure you run all nvm-windows commands in an Admin shell.

Installing Node.js versions

NVM makes installing Node.js versions a breeze. You have several options, depending on your needs.

Installing the latest version

To install the absolute latest version of Node.js, use the following command:

nvm install node  # Unix
nvm install latest  # Windows ( nvm-windows )
Enter fullscreen mode Exit fullscreen mode

At the time of writing, the latest version of Node.js is 23.7.0.

Installing the latest LTS version

For production environments, stability is key. Long Term Support (LTS) versions of Node.js are recommended for their extended support and reliability. To install the latest LTS version, use :

nvm install lts/*  # Unix
nvm install --lts  # Unix  
nvm install lts  # Windows ( nvm-windows )
Enter fullscreen mode Exit fullscreen mode

At the time of writing, the latest LTS version of Node.js is 22.14.0 .

Installing a specific version

If your project requires a very specific Node.js version, NVM allows you to install any available version by specifying the version number. For example, to install version 18.12.0, you would use the following command :

nvm install 18.12.0 # Works on both nvm and nvm-windows
Enter fullscreen mode Exit fullscreen mode

Listing installed versions

To see which versions you have installed, run the following command:

nvm list # Works on both nvm and nvm-windows
Enter fullscreen mode Exit fullscreen mode

The command will return the installed versions, such as:

       v18.12.0
       v22.14.0
->      v23.7.0
default -> lts/* (-> v22.14.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v23.7.0) (default)
stable -> 23.7 (-> v23.7.0) (default)
lts/* -> lts/jod (-> v22.14.0)
...
Enter fullscreen mode Exit fullscreen mode

The output above shows that the following versions of node were installed: 18.12.0, 22.14.0, 23.7.0 . It also shows that the current version of Node.js being used in this shell is v23.7.0, and the default version is v23.7.0. The nvm and nvm-windows output should be somewhat similar.

Switching to a specific version

To switch to a specific installed version, use the nvm use <version> command, replacing <version> with the desired version number. For instance, to switch to version 18.12.0, you would run the following command:

nvm use 18.12.0 # Works on both nvm and nvm-windows
Enter fullscreen mode Exit fullscreen mode

To switch to the LTS version, you would run the following command instead:

nvm use lts/*  # Unix
nvm use lts  # Windows ( nvm-windows )
Enter fullscreen mode Exit fullscreen mode

Advanced NVM usage

Beyond the basic installation and usage, NVM offers advanced features that streamline your Node.js version management even further. These features allow you to fine-tune your NVM setup and customize it to fit your development workflow perfectly.

Let's explore some of these more advanced capabilities:

Setting a default Node.js version

Beyond simply switching between versions, you can configure NVM to automatically use a specific Node.js version as the default for all new terminal sessions. This is particularly useful for streamlining your workflow and ensuring that your projects always use the correct Node.js version.

To achieve this, use the nvm alias default <version> command. Replace <version> with the version number or alias (like lts/* for the latest LTS) you want to use. For example:

nvm alias default 18.12.0
Enter fullscreen mode Exit fullscreen mode

Or, for the latest LTS version:

nvm alias default lts/*
Enter fullscreen mode Exit fullscreen mode

After setting the default, new terminal windows or tabs will automatically use the specified Node.js version. Existing terminals might need to be restarted or sourced again for the change to take effect.

Please note that the nvm alias command isn’t available in nvm-windows.

Uninstalling Node.js versions

If you need to remove a Node.js version that you no longer use, NVM makes this easy.

Use the nvm uninstall <version> command, replacing <version> with the version number you wish to remove. For example, to uninstall the Node.js version 23.7.0 using both NVM and nvm-windows you would run:

nvm uninstall 23.7.0 # Works on both nvm and nvm-windows
Enter fullscreen mode Exit fullscreen mode

Project-specific versions

Managing Node.js versions on a per-project basis is crucial for ensuring compatibility and avoiding conflicts. NVM simplifies this with the .nvmrc file. By placing a .nvmrc file in the root directory of your project, you can specify the required Node.js version for that project.

To use this feature, simply create a file named .nvmrc in your project's root directory. Inside this file, put the Node.js version you need. For example, if your project requires Node.js 18.12.0, your .nvmrc file would contain:

18.12.0
Enter fullscreen mode Exit fullscreen mode

Or, if you prefer to use the latest LTS version:

lts/*
Enter fullscreen mode Exit fullscreen mode

When you navigate into a directory containing a .nvmrc file, you can then run nvm use. NVM will read the .nvmrc file and automatically switch to the specified Node.js version. This makes collaboration easier, as anyone cloning the project can simply run nvm useto use the correct Node.js version.

Please note that this feature isn’t available in nvm-windows.

Custom download sources

In situations where the default Node.js download server is slow, unavailable, or you have specific mirroring requirements, the NVM_NODE_MIRROR environment variable comes in handy. This variable allows you to specify an alternative URL from which NVM will download Node.js releases.

To use this, set the NVM_NODEJS_ORG_MIRROR environment variable before installing Node.js versions. For example, in your shell configuration file (e.g., .bashrc, .zshrc), you might add:

export NVM_NODE_MIRROR=https://your-mirror.example.com/node/
Enter fullscreen mode Exit fullscreen mode

Replace https://your-mirror.example.com/node/ with the actual URL of your preferred mirror. After setting this variable, NVM will use the specified mirror when downloading Node.js versions.

If you are using nvm-windows run the following command instead:

nvm node_mirror https://your-mirror.example.com/node/
Enter fullscreen mode Exit fullscreen mode

Running commands in a specific context

Sometimes, you might need to run a single command in the context of a specific Node.js version without changing the default version for your current shell. The nvm exec command is designed for this purpose. The syntax is as follows:

nvm exec <version> -- <command>
Enter fullscreen mode Exit fullscreen mode

Replace <version> with the Node.js version you want to use, and <command> with the command you want to execute. For example, to install project dependencies using npm with Node.js version 23.7.0, you could use:

nvm exec v23.7.0 -- npm install
Enter fullscreen mode Exit fullscreen mode

This will run npm install using Node.js 23.7.0, without affecting the Node.js version used by your current shell for other commands. This is very useful for running specific scripts or tools that require a different Node.js version than your project's primary one.

Please note that the nvm exec command isn’t available in nvm-windows.

Troubleshooting

While NVM generally works smoothly, you might occasionally encounter some issues. This section covers common problems and provides solutions to help you get NVM working correctly.

Conflicts with existing installations

Before installing NVM, it's crucial to remove any existing Node.js and npm installations. These pre-existing installations can conflict with NVM, leading to unexpected behavior and errors.

On Unix systems, this might involve using your distribution's package manager (e.g., apt, yum, pacman, Homebrew) to remove Node.js and npm.

On Windows, uninstall via the Control Panel.

NVM not recognized

If the nvm command isn't recognized after installation, it usually means that NVM's directory isn't in your system's PATH. To fix that:

  • For Linux/macOS — First, restart the terminal and then try running the nvm command again. If that does not work, run the following commands for the different shells on the command line: source ~/.bashrc (bash), source ~/.zshrc(zhs). If the issue persists on macOS, check this link
  • For Windows — The NVM for Windows installer should handle adding NVM to the PATH. If it's still not recognized, you may need to add it manually via the System Properties

Permission issues

Occasionally, you might encounter permission issues during NVM installation or when installing Node.js versions:

  • Linux — Here the issue might lie with how you installed curl. If you installed curl using snap, you will have to uninstall it and install it using apt
  • Windows Ensure you're running the installer with administrator privileges

Version not found

If you try to install or use a version of Node.js that NVM can't find, it means that version isn't available in the NVM repository.

Use nvm ls-remote to see the complete list of available versions. Double-check the version number for typos.

Best practices for using NVM

To ensure a smooth and efficient experience with NVM, it's helpful to follow some best practices. These guidelines will help you avoid potential problems and make the most of NVM's features:

Install NVM per-user

It's strongly recommended that NVM be installed on a per-user basis, rather than globally on a shared system. This helps to isolate Node.js environments for different users and avoid conflicts.

Avoid shared scenarios

Avoid using NVM in shared environments or on build servers where multiple users or processes might be using the same NVM installation. This can lead to issues with symbolic links and unpredictable behavior.

Consider using containerization technologies like Docker to manage Node.js versions in shared or automated environments.

Keep NVM updated

Keeping NVM updated is important. New versions often include bug fixes, performance improvements, and support for the latest Node.js releases.

You can typically update NVM itself using a similar process to the initial installation. Consult the NVM GitHub page for update instructions.

Use LTS versions

For production environments, it's generally best to use LTS versions of Node.js. LTS versions are supported for an extended period and receive critical bug fixes and security updates, ensuring stability for your applications.

Use nvm install lts/* to install the latest LTS.

Conclusion

NVM simplifies Node.js version management, allowing you to switch between different versions and avoid conflicts easily. By following this tutorial, you can effectively manage your Node.js environments and ensure your projects run smoothly.

Explore the NVM git repository for more advanced features and options.


200’s only ✔️ Monitor failed and slow network requests in production

Deploying a Node-based web app or website is the easy part. Making sure your Node instance continues to serve resources to your app is where things get tougher. If you’re interested in ensuring requests to the backend or third party services are successful, try LogRocket.

LogRocket Network Request Monitoring

LogRocket is like a DVR for web apps, recording literally everything that happens on your site. Instead of guessing why problems happen, you can aggregate and report on problematic network requests to quickly understand the root cause.

LogRocket instruments your app to record baseline performance timings such as page load time, time to first byte, slow network requests, and also logs Redux, NgRx, and Vuex actions/state. Start monitoring for free.

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay