How to Install and Manage Node.js via NVM

How to Install and Manage Node.js via NVM

Managing multiple Node.js versions on a single machine can be a real headache, especially when different projects demand different Node.js or npm environments. This is where NVM (Node Version Manager) comes to the rescue. NVM allows you to easily install, switch, and manage various Node.js versions without conflict.

This guide will walk you through installing NVM and using it to manage your Node.js installations on Linux or macOS.

Why Use NVM?

  • Flexibility: Work on projects requiring different Node.js versions simultaneously.
  • Stability: Test your applications with different Node.js versions without affecting your primary development environment.
  • Simplicity: Easily switch between versions with simple commands.

Step 1: Install NVM

First, you need to install NVM itself. The easiest way is to use the curl or wget command to download and run the installation script.

Using cURL:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Using Wget:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Reload system environment using this command. It will set the required environment variables to use nvm on the system.

source ~/.profile     ## Debian based systems 
source ~/.bashrc      ## CentOS/RHEL systems 

Step 2: Verify NVM Installation

To confirm NVM is installed correctly, run:

nvm --version

You should see the NVM version number. If you get a “command not found” error, ensure you have sourced your shell configuration file as mentioned above.

Step 3: Find Available Node.js Version

At this point, you have installed nvm on your system for the current user. Now find out the available version of Node.js to install. Use ls-remote option to list versions.

nvm ls-remote

You will see a long list of available versions.

...
...
        v24.8.0
        v24.9.0
       v24.10.0
       v24.11.0   (LTS: Krypton)
       v24.11.1   (LTS: Krypton)
       v24.12.0   (LTS: Krypton)
       v24.13.0   (Latest LTS: Krypton)
        v25.0.0
        v25.1.0
        v25.2.0
        v25.2.1
        v25.3.0

Step 4: Install Node.js Versions

Now that NVM is ready, you can start installing Node.js versions.

Install the latest stable version:

nvm install node

Install a specific version (e.g., Node.js 16):

nvm install 16

Install the latest LTS (Long Term Support) version:

nvm install --lts

You can install as many versions as you need.

Step 5: List Installed Node.js Versions

To see all the Node.js versions currently installed via NVM:

nvm ls

This command will show you a list of versions, with an arrow -> pointing to the currently active version.

Step 6: Switch Between Node.js Versions

To use a specific Node.js version, simply tell NVM which one you want:

nvm use 16

This will activate Node.js version 16 for your current terminal session. If you open a new terminal, it will revert to the default (or system-wide) Node.js version unless you set a default.

Step 7: Set a Default Node.js Version

To make a specific Node.js version the default whenever you open a new shell:

nvm alias default 16

Now, every new terminal session will automatically use Node.js version 16.

Step 8: Uninstall Node.js Versions

If you no longer need a specific Node.js version, you can uninstall it:

nvm uninstall 14

Note: You cannot uninstall the version currently in use. Switch to another version first if you want to remove the active one.

Conclusion

NVM simplifies Node.js version management, making your development workflow much smoother and more efficient. With these commands, you’re well-equipped to handle any project’s Node.js requirements.


Discover more from TCMHACK

Subscribe to get the latest posts sent to your email.

Tags:

Leave a Reply

Discover more from TCMHACK

Subscribe now to keep reading and get access to the full archive.

Continue reading