NVM (Node Version Manager) is a powerful tool for managing Node.js versions on a Linux machine. It allows developers to easily install, switch, and maintain multiple Node.js environments. This guide will walk you through the steps to install and use NVM to manage Node.js on Debian 11.
Prerequisites
- A server running Debian 11.
- Root access to the server.
Installing NVM
Installing NVM is a straightforward process using the CURL command. Follow these steps:
Step 1: Install CURL and Gnupg2
First, ensure that CURL and Gnupg2 are installed with the following command:
apt-get install curl gnupg2 -y
Step 2: Install NVM
Run the following command to download and execute the NVM installation script:
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
This script will install NVM and update the necessary environment settings in your .bashrc file.
Step 3: Activate NVM
Activate the NVM settings by running:
source ~/.bashrc
Step 4: Verify NVM Installation
Verify that NVM is installed by checking its version:
nvm --version
Expected output:
0.38.0
Installing Node.js with NVM
Now that NVM is set up, you can install any version of Node.js.
Install the Latest Node.js Version
To install the latest version, run:
nvm install node
Expected output:
Downloading and installing node v16.9.0... ...
Verify Node.js Installation
Check the installed Node.js version:
node --version
Expected output:
v16.9.0
Install a Specific Node.js Version
To install a specific version, such as 12.17.0, use:
nvm install 12.17.0
Expected output:
Downloading and installing node v12.17.0... ...
Managing Node.js Versions with NVM
List Installed Node.js Versions
List all Node.js versions installed on your system:
nvm ls
Available Node.js Versions
Find all available versions:
nvm ls-remote
Switch Node.js Versions
Switch to a specific version, for example, 12.17.0:
nvm use 12.17.0
Default Version for Current User
Get the default Node.js version for the current user:
nvm run default --version
Run Node Application with Specific Version
Execute a Node application with a particular Node.js version:
nvm run v12.17.0 app.js
Uninstall a Node.js Version
Remove a specific Node.js version, for example, 12.17.0:
nvm uninstall 12.17.0
Conclusion
This guide provided a step-by-step process for installing NVM and using it to manage Node.js versions on Debian 11. With NVM, you can easily switch between Node.js versions to suit different application requirements.
FAQ
What is NVM?
NVM stands for Node Version Manager. It is a command-line tool used to manage multiple Node.js versions on a single machine.
Can I use NVM to install Node.js on other Linux distributions?
Yes, NVM can be used on various Linux distributions, not just Debian. The installation process is similar across different distributions.
Is it possible to use NVM with other shell environments?
Yes, you can configure NVM to work with other shell environments like zsh or fish. Refer to the NVM documentation for detailed instructions.
How do I update NVM?
To update NVM, simply re-run the installation script using CURL, and then source your .bashrc file again.