Install multiple versions of NodeJS with NVM on *nix environments

If you read my previous post on how to install multiple versions of Ruby on your machine, you might have realized some things that I like on my development machine:

  • Have programming languages isolated from the system directories.
  • Manage several version of a programming language.

Although, with tools like Vagrant and Docker you can have isolated development environments for each of your projects, sometimes it is really useful to have these package managers in your machine for quick testing purposes.

In this post I’m going to show you how to install multiple version of NodeJS (or its fork, IO.js) in your local machine.

Install NVM

Install NVM is very simple in *nix environtments: open the Terminal app, and execute the following command:

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

The line above will create a .nvm directory in your user’s home directory, and add he following line to the .bash_profile, .zshrc  and .profile files to ensure NVM is activate when you open a new Terminal window:

[[ -r "~ /.nvm/nvm.sh" ]] && source "~ /.nvm/nvm.sh"

If you want to make it work on the same Terminal window, enter the following line:

. ~/.nvm/nvm.sh
Note: There are solutions for windows as well (like this or this), but I will cover *nix environments only in this post.

How to use it NVM

First, list all the available version of NodeJS (or IO.js) with the command:

nvm ls-remote

Once you have identified the version you want, you can install it with the command:

nvm install NODEJS_VERSION

So, if you want to install latest stable version of NodeJS, the v4.1.2, the command will look like this:

nvm install v4.1.2

and that’s it! That’s how easy you can install a NodeJS version in your development machine.  To list all the version you have installed, enter:

nvm ls

it will show you something like the following image

NVM - installed versions os NodeJS and IO.js

 

From the list, you can choose the version you want to use with the command

nvm use [NODE_INSTALLED_VERSION]

And, if you want to use a version as default, you can enter

nvm alias default [NODE_INSTALLED_VERSION]

That’s all, folks! With these basic commands you are good to go, but for the curious ones, you can go to the project repo on GitHub and look for more ways to use NVM.

 


Tagged: , ,