How to install and use nvm?

Author: Chidume Nnamdi

introduction

Nodejs became a game changer in the world of software development after its initial release in May 2009. Since then, software development has not remained the same. Most of the big companies turned to Nodejs and many JavaScript frameworks were born. Eventually, JavaScript switched from a browser-only language to an all-platform language. We can now build mobile apps and desktop apps with JavaScript.

All you have to do to get Nodejs running is to install the executable and you’re good to go!
Now, Nodejs has gotten better and better with each new version released, but sometimes these versions can be a problem, and we can find ourselves running incompatible Nodejs in our projects. So we may just have to install the correct Nodejs versions, but do we have to install and reinstall Nodejs versions all the time?

The answer is no because a brilliant one does all the versioning work for us. The tool is called nvm, the node version manager.

The steps we will cover:

what is nvm,

nvm as we learned is a node version manager. It allows us to install and switch between different versions of Nodejs.

This is for us behind the hood instead of manually downloading and uninstalling the version of nodejs nvm. It’s just that it doesn’t uninstall any versions. It just manages them and switches to any one at will.

Let’s say you are trying to run a node project and the project works correctly with (for example) node version 14 but your node version is 10. You are likely to get this warning:

This project requires Node version 14
enter fullscreen mode

exit fullscreen mode

I encountered that on a project I was part of. I am running node version 16.17.0 but the tests in the project were written in node version 14.18.1. I got frequent failures regarding V8 buffer overflow and most of the tests were failing because of that. Not long after I realized that the tests were initially written in a node 14.18.1 environment. So I had to use nvm and switch from 16 to 14 and all tests passed.

One of the fastest and most powerful commands of nvm is use command. This command switches the node version.

nvm use 14.18.0
enter fullscreen mode

exit fullscreen mode

This command will switch your current node environment version from its current version to version 14.18.0.

Let’s say you are on node 16.0.

node --v
16.0.1
enter fullscreen mode

exit fullscreen mode

Now, to switch to node version 14.18.0, we run the previous command:

nvm use 14.18.0
enter fullscreen mode

exit fullscreen mode

Let’s see if the node version is switched correctly:

node -v
14.18.0
enter fullscreen mode

exit fullscreen mode

Yes, it is now on node version 14.18.0.

nvm. establishment

The easiest way to install nvm in Linux or MacOS is to use curl or the wget tool. You run any of the commands below:

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

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
enter fullscreen mode

exit fullscreen mode

download these commands install.sh script from https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/ and run the script.

This script has instructions on where to fetch the nvm repository and install it on the executing machine. This nvm repository will be cloned .nvm directory in your machine, and then add the below snippet to any profile configuration file: ~/.bash_profile, ~/.zshrc, ~/.profileeither ~/.bashrc,

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" # This loads nvm
enter fullscreen mode

exit fullscreen mode

Installation on a Windows machine is different from above. In Windows, you would use the nvm-windows tool. This nvm for windows is the Node.js version manager recommended by microsoft/npm/google for windows.
NB it is not the same as nvm.

You can download the latest release of NVM Windows from here. To see all releases of NVM Windows, you can view it here. For more instructions on how to install, upgrade, and more NVM Windows, visit their Github repo page.

Now, when nvm is done with the installation we can verify if the installation is successful and nvm is globally available on our machine, we will run the command below:

nvm -v
enter fullscreen mode

exit fullscreen mode

This will print the version of nvm currently on your machine.

Node Version Manager (v0.35.3)

Note: <version> refers to any version-like string nvm understands. This includes:
  - full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
  - default (built-in) aliases: node, stable, unstable, iojs, system
  - custom aliases you define with `nvm alias foo`

 Any options that produce colorized output should respect the `--no-colors` option.
enter fullscreen mode

exit fullscreen mode

See mine above, as you can see I have nvm version 0.35.3 on my machine.


Is your CRUD app full of technical debt?

Meet the headless, React-based solution to make sleek CRUD Application. With refinements, you can rest assured that your codebase will always be clean and boilerplate-free.

Try Refine to Build Your Next Faster CRUD Project, whether it is an admin panel, dashboard, internal tools or storefront.


install node version

Can we install a particular nodejs version using nvm, This is done by running the command:

nvm install 10.1.0
enter fullscreen mode

exit fullscreen mode

This will install Nodejs v10.1.0 on your machine. Now, if your machine has Nodejs v14, it will not be removed. Instead nvm will install Nodejs v10.1.0 in my machine to sit with v14. Now, the current version of node that node Will use the version that was installed directly from main, not from nvm.

You can install as many Nodejs versions as you want:

nvm install 11.2.1
enter fullscreen mode

exit fullscreen mode

,


nvm install 16.2.3

To install the latest Node version, we will run the below command:


nvm install node

This will install the latest version of Nodejs on your machine.

If we run the install command with only major version, then nvm will install latest version of Nodejs in that major version.

For example, this:


nvm install 10

will install the latest version of Nodejs v10. Let’s say the latest version of Nodejs in major version 10 is 10.19.0. It will be installed.

Switching environments

We can switch Node environments by using the nvm use command.


nvm use 14

This switches to Nodejs version 14. We must have the Nodejs 14 already installed on our machine before we can switch to it.


nvm use 12.0.1

This will switch to Nodejs v12.0.1.

There is something that we need to understand. If we only state the major version in the use The command, nvm will switch to use the major version of Nodejs regardless of minor and patch versions.

For example, this:


nvm use 14

This is just 14, the major version. There is no minor or patch version. Now, this will switch to using the latest Node version in the machine with a major version of 14.
If there are Node versions: 14.17.1, 14.18.1And 14.18.0, nvm will choose latest 14.18.1 and switch on it.

discord banner

Useful nvm commands

Let’s look at some other useful nvm commands:

list

This command lists/displays all the Nodejs versions we have on our machine.


nvm list

Let’s see the output:


v14.17.1
v14.18.1
v16.9.0
-> v16.17 system
default -> node (-> v16.17.0)
node -> stable (-> v16.17.0) (default)
stable -> 16.17 (-> v16.17.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/gallium (-> v16.17.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.20.0 (-> N/A)
lts/gallium -> v16.17.0

See that it displays all the Nodejs versions in my machine and LTS(long-term support) versions. See that the arrowed item in the list is our current Nodejs version.

uninstall

it is the opposite verb install, This command will uninstall a node version from the machine:


nvm uninstall 13.10.1

This will remove the installed Nodejs version 13.10.1 from your machine.

run

This command lets you run a script with Nodejs version without changing the current version.


nvm run 6.10.3 app.js

This runs app.js using node 6.10.3. You don’t have to switch the current version to 6.10.3 using the use command before running the script. So this run The command performs the switching before the script is run and switches back to the current version after executing the script.

conclusion

We learned a lot from this article. We started by introducing Nodejs and then nvm. Next, we learned about nvm and what it does for the Node versions in our machine.

Next, we learned how nvm switches between node versions in our machine, and the commands we can use to do so. We also learned how to install and uninstall node versions using the nvm tool.

Finally, we looked at lists of useful nvm commands that we can perform to ease the use of the nvm tool.

Leave a Comment