Upgrading or Downgrading NodeJS Versions with NVM

Last updated: January 28th 2025

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.39.7/install.sh | bash

Configure Your Shell

Add nvm to your shell’s configuration file.

For Bash:

# nano ~/.bashrc

For Zsh (Oh My Zsh):

# nano ~/.zshrc

Paste these lines at the end of the file:

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"  # Load nvm

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.39.7)

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 AdelAhmad is a freelance writer and also a backend developer.

Related articles