How to install Node.js by NVM?
Install and manage multiple versions of Node.js with nvm.
Sometimes, when I explore GitHub and clone some Node.js projects, It’s not compatible with my current installed Node.js, and I need to install the other version of that. NVM makes it easier and let us install and manage multiple version of Node.js on your local machine.
Install nvm on Ubuntu and Mac OS
There’s some way to install or update nvm on your machine, but I prefer to install nvm with the installer script. When I wrote this article, the latest version of the nvm installer was 0.38.0
. So, you can use curl
it to download and then run it with bash with the command below in the terminal.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
or with wget
:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
After that, the installer will clone the nvm repository into ~/.nvm/
directory and you should add some lines below at the end of ~/.bashrc
file to load nvm.
export NVM_DIR="$HOME/.nvm"
# This loads nvm
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
# This loads nvm bash_completion
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
Now you need to close and reopen the terminal to load nvm, then check and verify your installation
nvm --version
# 0.38.0
Now nvm is ready to use.
Use nvm to manage Node.js versions
Nvm has a lot of subcommands such as install, use, uninstall, and more. We want to choose some essential subcommands and explain them.
Install a version of Node.js
First, you can get the list of available versions by list-remote
or ls-remote
subcommand.
nvm list-remote # or nvm ls-remote
You can install a specific version by nvm with install
subcommand.
nvm install <version> # like: 14.17.6
If you want to install the LTS version you can use --lts
instead of version number.
nvm install --lts
Or you can install the latest version with node
instead of version number.
nvm install node
Load a specific version of Node.js
Now you install some versions of Node.js on your machine; So you can see the list of installed versions with list
or ls
subcommand.
nvm list # or nvm ls
If you want to load a specific version, use use
subcommand. With this subcommand, you can load Node.js by version number or --lts
flag.
nvm use <version>
Or use the LTS version
nvm use --lts
Or use the latest version
nvm use node
Uninstall a Node.js version
Finally, If you want to uninstall a version of Node.js, you can use uninstall
subcommand for that.
First, you need to switch to another version with use
then you can uninstall that.
nvm uninstall <version>
What’s the .nvmrc file?
In the Node.js project, you can store the version of Node.js compatible with that in a file, they called it .nvmrc
and you should create it in the root directory of your project.
node --version > /path/to/project/.nvmrc
Then you can use the compatible version on other machines of your teammates.
nvm use
# Found '/path/to/project/.nvmrc' with version <v14.17.6>
# Now using node v14.17.6 (npm v6.14.15)
Install a package on Node.js
Installing a package on Node.js installed by NVM is the same as the regular installation of node.js, but the installation of the package is based on the version number.
nvm use 14.17.5
npm install --global yarn
which yarn
# ~/.nvm/versions/node/v14.17.5/bin/yarn
Conclusion
NVM helps us to have some node.js versions together in a machine and use them in our project or test features of dev versions without side effects on our system or project.
So, I recommended using it to install Node.js and manage that easily.