DEV Community

loading...

A 5 minute guide to nvm

sunflower profile image sunflowerseed Updated on ・2 min read

To have multiple versions of node and npm on our machine, we can use nvm.

The official docs of nvm is on: https://github.com/nvm-sh/nvm

If we only need one version of node and npm, then we may not need nvm, but to solve the problem of the write permission, we can use: https://dev.to/sunflower/don-t-run-sudo-npm-install-g-the-simplest-way-to-make-it-work-30e5

Here is a quick start guide:

To install nvm

curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh -o install_nvm.sh
Enter fullscreen mode Exit fullscreen mode

Or because Ubuntu doesn't have curl by default, we can also use wget:

wget https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh -O install_nvm.sh
Enter fullscreen mode Exit fullscreen mode

and we can look at install_nvm.sh -- I usually don't like to curl and directly pipe it to bash, because it is like we don't even know what was running. So then we download it, and can run it:

bash install_nvm.sh
source ~/.profile     # no need to do it after reboot
Enter fullscreen mode Exit fullscreen mode

Useful commands

nvm ls-remote       # to see all available versions
nvm install --lts   # install the latest LTS (long term support) version
nvm install node    # install the latest
nvm which current   # tells the path of current node
nvm ls              # tells what are all the node versions we have
nvm ls current      # tells the version we are using
nvm use             # use the .nvmrc specified version if any
nvm use 14          # use the version 14.x.x
nvm use 15          # use 15.x.x
nvm use --lts       # use the latest LTS
nvm use node        # use the latest
nvm use system      # use the system's version of node
nvm alias default 14.16.0   # set the default version to use
Enter fullscreen mode Exit fullscreen mode

As of 2021 March, all we need to do is:

nvm install --lts   # install the latest LTS (long term support) version
nvm install node    # install the latest
Enter fullscreen mode Exit fullscreen mode

and then depending on which one we want to use (version 14.16.0 which is LTS, or version 15.13.0, which is the latest), we can just use

nvm use 14     # or
nvm use 15
Enter fullscreen mode Exit fullscreen mode

to switch between them.

Discussion (0)

pic
Editor guide