Node Version Manager (NVM) is a neat little bash script that allows you to manage multiple versions of Node.js on the same box. A version manager really helps to test our applications under different versions of the related software.
Installation
Just clone the git repository from github into ~/.nvm
or any other folder of your wish.
What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.
$ git clone git://github.com/creationix/nvm.git ~/.nvm
To activate it, source it from the bash shell using source
or .
–
$ . ~/.nvm/nvm.sh
If you want to know what the .
does try one of these commands –
$ help source $ help .
It basically reads and executes all the commands from the filename passed to it (~/.nvm/nvm.sh
in our case), in the current shell.
To make sure the nvm
command is available in your shell, add the source line to your profile, which is ~/.bash_profile
or ~/.profile
. These profiles get read on login, hence it’ll automatically source the nvm
script. For non-login shells (for example terminals fired up from your GUI environment) you’ll need to source from ~/.bashrc
.
For a quicker installation, just download and execute the install script provided via cURL or wget –
$ curl https://raw.github.com/creationix/nvm/master/install.sh | sh .. or .. $ wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh
These commands will clone the git repository to ~/.nvm
and put the source line to your profile (~/.bash_profile
if found else ~/.profile
).
Usage
Using it is super duper simple, let’s see how –
# To check what versions can be installed $ nvm ls-remote # To install: # nvm install [version] $ nvm install 0.10.0 # To check what versions are installed $ nvm ls # To use the installed version $ nvm use 0.10.0 # .. or just run the node repl with that version $ nvm run 0.10.0 # To remove/uninstall $ nvm uninstall 0.10.0
When you install node v0.10.0
, it get’s installed in ~/.nvm/v0.10.0
and the new node binary residing in ~/.nvm/v0.10.0/bin
gets added to the PATH
environment variable.
$ node -v v0.10.3 $ nvm install 0.10.0 ######################################################################## 100.0% Now using node v0.10.0 $ node -v v0.10.0 $ ls ~/.nvm/v0.10.0/ bin ChangeLog lib LICENSE README.md share $ ls ~/.nvm/v0.10.0/bin/ node npm $ echo $PATH /home/rishabh/.nvm/v0.10.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
This is what you’d do to restore your path and get back to the original node installation (if any) –
# Installed via nvm $ node -v v0.10.0 $ nvm deactivate /home/rishabh/.nvm/*/bin removed from $PATH /home/rishabh/.nvm/*/share/man removed from $MANPATH # Initial node installation $ node -v v0.10.3 $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
The best part of nvm
is that, you can set a default version of Node to be used by setting the alias default.
# nvm alias <name> <version> # Set node v0.8.22 to use by default $ nvm alias default 0.8.22
Basically nvm alias
is used to set a name pointing to a particular node version.
# More aliases? $ nvm alias ten 0.10.0 ten -> 0.10.0 (-> v0.10.0) $ nvm alias default -> 0.8.22 (-> v0.8.22) ten -> 0.10.0 (-> v0.10.0) $ nvm use ten Now using node v0.10.0
Auto Command Completion
If you type nvm
and hit the tab key twice, it won’t list related commands/options. To activate this you’ll need to source ~/.nvm/bash_completion
right after the sourcing of the nvm.sh
file (as shown above).
This is what needs to be added –
[[ -r $NVM_DIR/bash_completion ]] && . $NVM_DIR/bash_completion
Checks whether the file is readable or not, if yes then source it. Now when you hit tab key twice –
$ nvm [tab][tab] alias deactivate list ls-remote uninstall clear-cache help list-remote run use copy-packages install ls unalias version $ nvm use [tab][tab] default ten v0.10.0 v0.8.22 # .. and so on!
Notes
Technically nvm
is not a script executable file. It’s a bash function. You can read the contents of ~/.nvm/nvm.sh
.
Conclusion
It’s awesome! Really helpful especially when you’re trying out a new version of Node and your app suddenly breaks due to incompatibilities. Your best bet is to rollback to the older version and nvm
makes this process dead simple.
Informative article! Do you have any ideas as to how to set your node alias to be permanent? Once i log off my shell and back in, the “node” command is not found until i once again type “nvm use “
You could try adding
nvm use
to your profile. Although as I’ve already mentioned the new node binary residing in~/.nvm/*/bin
gets added to$PATH
on installation, sonode
command should work.