Node Version Manager: Nvm Install Guide

In this article, I’ll explain how you can install and use NVM on a Mac.

What is NVM?#

Node Version Manager (NVM) is a tool for managing Node versions on your machine.

NVM allows you to easily install and manage different versions of Node and switch between them on a per-shell basis. This post describes how to install NVM, and how to use it to install and run different versions of Node.

Why the need for NVM#

Different projects on your machine may be using different versions of Node. Using only one version (the one installed by npm) for these different projects may not give you unexpected results. If you have and older version of Node compared to the version that your project uses, you’ll likely get a warning that says:

This project requires Node version X

Instead of using npm to install and uninstall Node versions for your different projects, you can use nvm, which helps you switch your node versions for each project via the command line.

Before proceeding, I recommend that you uninstall Node.js if you have it installed already so that you do not have any conflicts with Node.js and nvm.

How to install#

It’s best to check the instructions in the NVM repo to make sure you install the latest version. At the time of writing these are the instructions.

Install#

To install or update nvm, you should run the install script. To do that, you may either download and run the script manually, or use the following cURL or Wget command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
  # or 
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

Running either of the above commands downloads a script and runs it. The script clones the nvm repository to ~/.nvm, and attempts to add the source lines from the snippet below to the correct profile file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

  export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
  [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Verify installation#

With the above command executed, nvm is should be ready for you to use. Restart your terminal session then you can confirm that nvm is installed correctly by running:

  nvm -v

Use NVM to Install Node#

NVM’s install command downloads, compiles, and installs the specified version of Node. You can install as many versions of Node as you want.

To install the latest version of Node#

Run the following:

nvm install node

To install a specific version of Node#

Run the following to get a long list of available versions:

nvm ls-remote

and, install a specific version run:

nvm install 16.17.1 # Specific minor release
nvm install 17 # Specify major release only

To review all installed versions of Node#

run the ls command

nvm ls

To select a different version of Node#

, use the nvm use command. Specify either the version number of Node (major or minor release) or an alias such as node.

nvm use node

or

nvm use 16

NVM confirms it is now using the new version.

Now using node v16.17.1

You can also confirm the current version of Node with nvm current.

nvm current

NVM again returns the current version number.

v16.17.1

Other resources#

  • If you are having issues, check the troubleshooting guide in the NVM repo
  • A lengthy guide to in how to install and use NVM can be found at Linode.com

comments powered by Disqus