Easily install and manage any version of Node.JS using the Node Version Manager (NVM). NVM is similar to Ruby’s RVM, allowing you to install and switch between different versions of Node.JS and NPM using the command line.
Update Ubuntu
Make sure to update your Ubuntu box with the latest packages by running these commands:
apt-get && apt-get upgrade
Install NVM
Install NVM by running this command:
curl https://raw.githubusercontent.com/creationix/nvm/v0.24.0/install.sh | bash
This will download the latest install script for NVM via curl and execute it. This usually only takes a few seconds. Once NVM has downloaded and finished installing, it will ask you to close and reopen your terminal. This will not be necessary, simply run:
source /root/.bashrc
Ensure NVM is properly installed by running the following command. This will output the usage instructions for NVM.
nvm
Install latest Node.JS
At the writing of this article, the latest major version of Node.JS is version 0.12.x. We will be using this version as an example. Install version 0.12.
nvm install 0.12
You can replace 0.12 with any version of node that’s available (0.10, 0.11.2, etc). NVM will download a pre-built binary version of Node.JS and install it. This should only take a few seconds. Once installation has completed, NVM will output the version installed, such as:
Now using node v0.12.0
Test your node installation. This command will output the version of node that has been installed.
node -v
Set default version
To ensure that the same node version is used the next time you log in via ssh, you can set a “default” node version by running:
nvm alias default 0.12
This will ensure that version 0.12 of node is always used when logging onto the server.
Switching Between Versions
Using NVM, you can install multiple versions of node. Once a different version is installed, for example 0.10, you can switch to that node version by running:
nvm use 0.10
Note that when switching between node versions, the associated npm version and any global npm packages will be changed as well. You may need to reinstall your global npm packages when switching to a recently installed version.