Introduction
Managing multiple Node.js versions can be a hassle, especially when working on projects with varying requirements. Enter nvm (Node Version Manager) – a simple yet powerful tool that allows developers to seamlessly switch between Node.js versions. Managing Node.js versions is straightforward using nvm. Below is a step-by-step guide to install and use nvm for switching between Node.js versions.
Prerequisites
- A Webdock Ubuntu server
- SSH Access to the server
Installing Dependencies
First, switch to root with:
$ sudo su
Now, install dependencies.
Install curl
Most Linux distributions include curl
by default. If not installed, follow these steps:
For Ubuntu/Debian:
# apt update
# apt install curl
# curl --version # Verify installation
For other systems, refer to the official curl installation guide.
Installing NVM (Node Version Manager)
Let's start by downloading and installing NVM.
Download and Install nvm
Run this command to install nvm:
# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
Configure Your Shell
The above command automatically adds NVM to the $PATH. As the prompt on the screen says, you can either log-out and log-in to the shell for the changes to get applied, or just run the below source command.
Reload the configuration:
# source ~/.bashrc # For Bash
# source ~/.zshrc # For Zsh
What the above lines do is, the nvm path will be added to the $PATH variable for the respective shell. So when a new shell session is created and nvm is ran, the shell looks for the "nvm" binary location. This location is included in the PATH.
To verify the installation, run:
# nvm --version # Should display the installed version (e.g., 0.40.1)
Managing Node.js Versions
To manage the NodeJS version you can use the below commands – to install, switch, or uninstall.
To Install a Node.js version:
# nvm install 20 # Replace "20" with your desired version (e.g., 18, 16.14.0)
To switch between versions:
# nvm use 20 # Use Node.js v20
To uninstall a version
# nvm uninstall 20 # Remove Node.js v20
To set a default version (Optional):
# nvm alias default 20 # Use Node.js v20 by default
Conclusion
That's it. This short and sweet article showed you how to use NVM to manage NodeJS version on your Linux system.
Feel free to contact Webdock Support if you need assitance.
This article was written by Ahmad Adel . Ahmad is a freelance writer and also a backend developer.