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.
Make sure to update your Ubuntu box with the latest packages by running these commands:
apt-get && apt-get upgradeInstall NVM by running this command:
curl https://raw.githubusercontent.com/creationix/nvm/v0.24.0/install.sh | bashThis 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/.bashrcEnsure NVM is properly installed by running the following command. This will output the usage instructions for NVM.
nvmAt the writing of this article, the latest major version Node.js is version 0.12.x. We will be using this version as an example. Install version 0.12.
nvm install 0.12You can replace 0.12 it with any version of Node that’s available (0.10, 0.11.2, etc). NVM will download a pre-built binary version 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.0Test your node installation. This command will output the version of the node that has been installed.
node -vTo 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.12This will ensure that version 0.12 the node is always used when logging onto the server.
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.10Note 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.