Using NVM to Install Node

Within the past 6 months alone, Node.js has gone from v0.12.x to v5.1.x. There were 35+ releases in that time period, with each one adding some significant functionality or bugs fixes. A big part of this jump was the merging of io.js in to Node, which was under much more rapid development.

Installing Node isn't the easiest process, or at least it isn't convenient to constantly be uninstalling and reinstalling it every few days. This became a big problem for developers needing to support multiple versions of Node for their libraries and for users who had to switch between versions for different software.

For example, the popular blogging platform Ghost only supported v0.10.0 and v0.12.0 for a long time. So if you were doing any development with io.js or v1.0.x+ versions then you'd have to switch back and forth between Node/io.js versions to use Ghost.

Between the fast-paced releases and having to support multiple versions of Node, there's got to be an easier way to switch between versions. And this is where NVM comes in.

What is NVM?

Node Version Manager (NVM) is a utility to help you quickly install and switch between Node versions. With NVM, there is no need to manually install and uninstall versions like you used to.

Also, every time you install a new Node.js version, you also get the corresponding npm package with it, so there's no need to worry about compatibility there.

Each version you install remains on the system and can be accessed at any time. So if you're maintaining a library you created you can easily go back to any version of Node and test it out or create a patch.

NVM is basically just a few bash scripts that help you install, uninstall, and link different Node binaries. Since it is written as a bash script, it's only available for OSX and Linux. This means it isn't natively compatible with Windows, unfortunately. There are, however, a few alternatives created mimic the functionality of NVM: nvmw and nvm-windows.

Installing NVM

The easiest way to install NVM is to run the following command from your terminal. But first, you'll need to install a few dependencies if you don't have them already.

Linux:

$ sudo apt-get update
$ sudo apt-get install build-essential libssl-dev

On OSX the only dependency you need is the command line developer tools. Execute the following command, and then click "Install".

OSX:

$ xcode-select --install

Finally, you can install NVM with:

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

Or, by using the wget alternative:

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

These commands will download and run the installation script directly from the Github repository.

Essentially, this installation script will download the NVM repository in to the ~/.nvm directory and add the source line to your shell profile (~/.bash_profile, ~/.zshrc, or ~/.profile).

I've had problems on some systems in the past where the source line didn't get properly added to .bash_profile, so I had to activate NVM manually each time I opened a shell, so watch out for this problem. You many need to open .bash_profile and manually fix it yourself.

How to Use NVM

To install a new version of Node using NVM, just run the install command:

$ nvm install 5.1

This will download and install the latest v5.1.x release. To actually activate that version, you can do this:

$ nvm use 5.1

Now all calls to node will be directed to the v5.1.x version.

Alternatively, if you just want to make a one-off call and not actually completely switch binaries, you can just use the run command:

$ nvm run 5.1 index.js
Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

This will run index.js as Node v5.1.x, but the node command will still be linked to whatever version it was before you ran nvm run.

Not sure which versions you've already installed? Use the ls command to get a list of all those available to you:

$ nvm ls
        v0.12.2
    iojs-v2.0.0
    iojs-v3.3.1
         v4.1.2
->       v5.1.0
         system
default -> v4.1.2
node -> stable (-> v5.1.0) (default)
stable -> 5.1 (-> v5.1.0) (default)
iojs -> iojs-v3.3 (-> iojs-v3.3.1) (default)

And finally, once you know you're done with a version and don't need it anymore, uninstalling it is easy:

$ nvm uninstall 5.1

For more information, check out the NVM readme, which has a more complete list of commands.

Last Updated: December 7th, 2015
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms