How to Install Node.js and npm Using NVM

Node.js is a powerful JavaScript runtime that allows developers to build fast, scalable network applications. With Node.js, you can use JavaScript on both the front end and back end, making development more consistent and easier to integrate. In this guide, I'll walk you through the process of installing Node.js and npm using NVM (Node Version Manager), which is a flexible and efficient method for managing Node.js versions on your system.

Why Use NVM for Installing Node.js?

Using NVM to install Node.js and npm is the recommended approach, especially if you're working on multiple projects that require different versions of Node.js. NVM allows you to install and switch between different versions of Node.js without affecting your entire system. This flexibility is particularly useful when maintaining legacy applications or testing new Node.js features.

Step-by-Step Guide to Installing Node.js and npm Using NVM

NVM works on various operating systems, including Windows, Ubuntu, and Linux. Follow these steps to install Node.js and npm using NVM:

  1. Install NVM: First, you need to download the NVM installation script. You can do this using curl from the official NVM GitHub repository. Run the following command in your terminal:

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

    This script will install NVM into a subdirectory of your home directory (~/.nvm). It also adds the necessary configuration to your ~/.profile file to enable NVM.

  2. Activate NVM: After installation, you need to activate NVM in your current terminal session. You can do this by restarting your terminal or by running the following command:

    source ~/.profile
  3. Install Node.js Versions: With NVM installed, you can now list all available Node.js versions using:

    nvm ls-remote

    You'll see a list of available versions, including the latest LTS (Long-Term Support) versions. To install the latest LTS version, use the following command:

    nvm install 14.15.1
  4. Switch Between Node.js Versions: NVM will automatically switch to the most recently installed version. To manually switch between installed versions, use:

    nvm use 14.15.1
  5. Check Installed Versions: To verify the version of Node.js currently in use, type:

    node -v

    Similarly, to check the npm version, use:

    npm -v
  6. Manage Multiple Node.js Versions: If you have multiple Node.js versions installed, you can view them with:

    nvm ls

How to Uninstall Node.js Using NVM

If you need to uninstall a specific version of Node.js, NVM makes it easy. First, check which version is currently active:

nvm current

If the version you want to remove is not active, you can uninstall it with:

nvm uninstall <node_version>

To remove the currently active version, first deactivate NVM:

nvm deactivate

Then, proceed with the uninstall command as mentioned above.

Conclusion

There are quite a number of ways to get up and running with Node.js on your system. But in my opinion nvm is the easiest method to get node up and running easily. Using nvm, it offers additional flexibility.

💌 If you'd like to receive more tutorials in your inbox, you can sign up for the newsletter here.

Discussions

Up next