Skip to content

Instantly share code, notes, and snippets.

@creationix
Created April 15, 2010 16:13
Show Gist options
  • Save creationix/367305 to your computer and use it in GitHub Desktop.
Save creationix/367305 to your computer and use it in GitHub Desktop.
Node Version Manager

Node Version Manager

Installation

First you'll need to make sure your system has a c++ compiler. For OSX, XCode will work, for Ubuntu, the build-essential package works. You'll also need git if you want to track HEAD.

To install create a folder somewhere in your filesystem with the "nvm.sh" file inside it. I put mine in a folder called ".nvm".

Or if you have git installed, then just clone it:

git clone git://gist.github.com/367305.git ~/.nvm

Then add three lines to your bash profile:

NVM_DIR=$HOME/.nvm
. $NVM_DIR/nvm.sh
nvm use v0.1.91

The first line tells your system where NVM is installed, you should already have nvm.sh there. The second line loads the nvm function into your bash shell so that it's available as a command. The third line sets your default node version.

Usage

To download, install, and use the v0.1.91 release of node do this:

nvm install v0.1.91

And then in any new shell just use the installed version:

nvm use v0.1.91

If you want to track HEAD then use the clone command:

nvm clone

Then in any new shell you can get this version with:

nvm use HEAD

When you want to grab the latest from the node repo do:

nvm update

If you want to see what versions you have installed issue:

nvm list

# Node Version Manager
# Implemented as a bash function
# To use source this file from your bash profile
#
# Implemented by Tim Caswell <tim@creationix.com>
# with much bash help from Matthew Ranney
nvm()
{
START=`pwd`
if [ $# -lt 1 ]; then
nvm help
return
fi
case $1 in
"help" )
echo
echo "Node Version Manager"
echo
echo "Usage:"
echo " nvm help (Show this message)"
echo " nvm install version (Download and install a released version)"
echo " nvm clone (Clone and install HEAD version)"
echo " nvm update (Pull and rebuild HEAD version)"
echo " nvm list (Show all installed versions)"
echo " nvm use version (Set this version in the PATH)"
echo
echo "Example:"
echo " nvm install v0.1.91"
echo
;;
"clone" )
if [ $# -ne 1 ]; then
nvm help
return;
fi
mkdir -p "$NVM_DIR/src" && \
cd "$NVM_DIR/src" && \
git clone git://github.com/ry/node.git && \
cd node && \
./configure --prefix="$NVM_DIR/HEAD" && \
make && \
make install && \
nvm use HEAD
cd $START
;;
"update" )
if [ $# -ne 1 ]; then
nvm help
return;
fi
cd "$NVM_DIR/src/node" && \
git pull --rebase origin master
./configure && \
make clean all && \
make install && \
nvm use HEAD
cd $START
;;
"install" )
if [ $# -ne 2 ]; then
nvm help
return;
fi
mkdir -p "$NVM_DIR/src" && \
cd "$NVM_DIR/src" && \
wget "http://nodejs.org/dist/node-$2.tar.gz" -N && \
tar -xzf "node-$2.tar.gz" && \
cd "node-$2" && \
./configure --prefix="$NVM_DIR/$2" && \
make && \
make install && \
nvm use $2
cd $START
;;
"use" )
if [ $# -ne 2 ]; then
nvm help
return;
fi
if [ ! -d $NVM_DIR/$2 ]; then
echo "$2 version is not installed yet"
return;
fi
if [[ $PATH == *$NVM_DIR/*/bin* ]]; then
PATH=${PATH%$NVM_DIR/*/bin*}$NVM_DIR/$2/bin${PATH#*$NVM_DIR/*/bin}
else
PATH="$NVM_DIR/$2/bin:$PATH"
fi
export PATH
echo "Now using node $2"
;;
"list" )
if [ $# -ne 1 ]; then
nvm help
return;
fi
if [ -d $NVM_DIR/HEAD ]; then
if [[ $PATH == *$NVM_DIR/HEAD/bin* ]]; then
echo "HEAD *"
else
echo "HEAD"
fi
fi
for f in $NVM_DIR/v*; do
if [[ $PATH == *$f/bin* ]]; then
echo "v${f##*v} *"
else
echo "v${f##*v}"
fi
done
;;
* )
nvm help
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment