Nvm
Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions
Install / Use
/learn @nvm-sh/NvmREADME
Node Version Manager [
][3] [
][4] 
<!-- To update this table of contents, ensure you have run `npm install` then `npm run doctoc` -->
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
Table of Contents
- Intro
- About
- Installing and Updating
- Usage
- Running Tests
- Environment variables
- Bash Completion
- Compatibility Issues
- Installing nvm on Alpine Linux
- Uninstalling / Removal
- Docker For Development Environment
- Problems
- macOS Troubleshooting
- WSL Troubleshooting
- Maintainers
- Project Support
- Enterprise Support
- License
- Copyright notice
Intro
nvm allows you to quickly install and use different versions of node via the command line.
Example:
$ nvm use 16
Now using node v16.9.1 (npm v7.21.1)
$ node -v
v16.9.1
$ nvm use 14
Now using node v14.18.0 (npm v6.14.15)
$ node -v
v14.18.0
$ nvm install 12
Now using node v12.22.6 (npm v6.14.5)
$ node -v
v12.22.6
Simple as that!
About
nvm is a version manager for node.js, designed to be installed per-user, and invoked per-shell. nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.
<a id="installation-and-update"></a> <a id="install-script"></a>
Installing and Updating
Install & Update Script
To install or update nvm, you should run the [install script][2]. 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.40.4/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/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 (~/.bashrc, ~/.bash_profile, ~/.zshrc, or ~/.profile). If you find the install script is updating the wrong profile file, set the $PROFILE env var to the profile file’s path, and then rerun the installation script.
<a id="profile_snippet"></a>
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
Additional Notes
-
If the environment variable
$XDG_CONFIG_HOMEis present, it will place thenvmfiles there.</sub> -
You can add
--no-useto the end of the above script to postpone usingnvmuntil you manuallyuseit:
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" --no-use # This loads nvm, without auto-using the default version
-
You can customize the install source, directory, profile, and version using the
NVM_SOURCE,NVM_DIR,PROFILE, andNODE_VERSIONvariables. Eg:curl ... | NVM_DIR="path/to/nvm". Ensure that theNVM_DIRdoes not contain a trailing slash. -
The installer can use
git,curl, orwgetto downloadnvm, whichever is available. -
You can instruct the installer to not edit your shell config (for example if you already get completions via a zsh nvm plugin) by setting
PROFILE=/dev/nullbefore running theinstall.shscript. Here's an example one-line command to do that:PROFILE=/dev/null bash -c 'curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash'
Installing in Docker
When invoking bash as a non-interactive shell, like in a Docker container, none of the regular profile files are sourced. In order to use nvm, node, and npm like normal, you can instead specify the special BASH_ENV variable, which bash sources when invoked non-interactively.
# Use bash for the shell
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# Create a script file sourced by both interactive and non-interactive bash shells
ENV BASH_ENV /home/user/.bash_env
RUN touch "${BASH_ENV}"
RUN echo '. "${BASH_ENV}"' >> ~/.bashrc
# Download and install nvm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | PROFILE="${BASH_ENV}" bash
RUN echo node > .nvmrc
RUN nvm install
Installing in Docker for CICD-Jobs
More robust, works in CI/CD-Jobs. Can be run in interactive and non-interactive containers. See https://github.com/nvm-sh/nvm/issues/3531.
FROM ubuntu:latest
ARG NODE_VERSION=20
# install curl
RUN apt update && apt install curl -y
# install nvm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash
# set env
ENV NVM_DIR=/root/.nvm
# install node
RUN bash -c "source $NVM_DIR/nvm.sh && nvm install $NODE_VERSION"
# set ENTRYPOINT for reloading nvm-environment
ENTRYPOINT ["bash", "-c", "source $NVM_DIR/nvm.sh && exec \"$@\"", "--"]
# set cmd to bash
CMD ["/bin/bash"]
This example defaults to installation of nodejs version 20.x.y. Optionally you can easily override the version with docker build args like:
docker build -t nvmimage --build-arg NODE_VERSION=19 .
After creation of the image you can start container interactively and run commands, for example:
docker run --rm -it nvmimage
root@0a6b5a237c14:/# nvm -v
0.40.4
root@0a6b5a237c14:/# node -v
v19.9.0
root@0a6b5a237c14:/# npm -v
9.6.3
Noninteractive example:
user@host:/tmp/test $ docker run --rm -it nvmimage node -v
v19.9.0
user@host:/tmp/test $ docker run --rm -it nvmimage npm -v
9.6.3
Troubleshooting on Linux
On Linux, after running the install script, if you get nvm: command not found or see no feedback from your terminal after you type command -v nvm, simply close your current terminal, open a new terminal, and try verifying again.
Alternatively, you can run the following commands for the different shells on the command line:
bash: source ~/.bashrc
zsh: source ~/.zshrc
ksh: . ~/.profile
These should pick up the nvm command.
Troubleshooting on macOS
Since OS X 10.9, /usr/bin/git has been preset by Xcode command line tools, which means we can't properly detect if Git is installed or not. You need to manually install the Xcode command line tools before running the install script, otherwise, it'll fail. (see #1782)
If you get nvm: command not found after running the install script, one of the following might be the reason:
-
Since macOS 10.15, the default shell is
zshand nvm will look for.zshrcto update, none is installed by default. Create one withtouch ~/.zshrcand run the install script again. -
If you use bash, the previous default shell, your system may not have
.bash_profileor.bashrcfiles where the command is set up. Create one of them withtouch ~/.bash_profileor `touch ~/.
