Introduction
Node Version Manager (NVM) is a powerful tool that allows you to easily install and manage multiple active Node.js versions on a single system. This is incredibly useful for developers working on projects with varying Node.js version requirements, or for testing applications across different Node.js releases. This tutorial will guide you through the installation and setup process of NVM, ensuring you can seamlessly switch between Node.js versions.
Installation
The primary method for installing NVM is using a script downloaded directly from the NVM GitHub repository. Open your terminal and execute the following command:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
This command downloads the installation script and executes it using bash
. It will automatically download and install NVM into your user directory (typically ~/.nvm
).
Important: If you encounter issues with curl
, ensure it is installed on your system. You may need to use your distribution’s package manager (e.g., apt-get install curl
on Debian/Ubuntu, yum install curl
on CentOS/RHEL, or brew install curl
on macOS).
Setting Up Your Shell
After the installation script completes, it will provide instructions on how to activate NVM in your current shell. However, you also need to configure your shell to load NVM automatically every time you open a new terminal window. This involves adding a line to your shell’s configuration file.
The specific configuration file depends on the shell you are using. Common shells and their respective configuration files include:
- Bash:
~/.bashrc
or~/.bash_profile
- Zsh:
~/.zshrc
- Fish:
~/.config/fish/config.fish
Open the appropriate configuration file in a text editor. Add the following line to the end of the file:
source ~/.nvm/nvm.sh
This line tells the shell to execute the nvm.sh
script whenever a new terminal session is started, effectively loading NVM into your environment.
Restarting Your Terminal or Sourcing the Configuration File
After adding the line to your configuration file, you have two options:
-
Restart your terminal: Close and reopen your terminal window. This will ensure that the changes to your configuration file are loaded.
-
Source the configuration file: Execute the following command in your current terminal:
source ~/.bashrc # For Bash source ~/.zshrc # For Zsh
This will immediately load the changes into your current shell session without requiring a restart.
Verifying the Installation
To verify that NVM has been installed correctly, run the following command:
nvm --version
If NVM is installed correctly, this command will display the version number of NVM. If you receive an error message like "command not found", double-check that you have correctly added the source ~/.nvm/nvm.sh
line to your shell configuration file and that you have either restarted your terminal or sourced the file.
Installing Node.js Versions
Now that NVM is installed, you can easily install and manage multiple Node.js versions.
To install a specific Node.js version, use the following command:
nvm install <version>
Replace <version>
with the desired Node.js version number. For example, to install Node.js version 16.13.0, you would run:
nvm install 16.13.0
Switching Between Node.js Versions
After installing multiple Node.js versions, you can easily switch between them using the nvm use
command:
nvm use <version>
Replace <version>
with the desired Node.js version number. For example, to switch to Node.js version 16.13.0, you would run:
nvm use 16.13.0
Listing Installed Node.js Versions
To see a list of all Node.js versions installed on your system, use the following command:
nvm ls
This will display a list of installed versions, with the currently active version indicated by an arrow (->
).
Setting a Default Node.js Version
You can set a default Node.js version that will be used automatically whenever you open a new terminal session:
nvm alias default <version>
Replace <version>
with the desired Node.js version number.