Using Node Version Manager (nvm)
Node Version Manager, otherwise known as nvm, is a bash script that simplifies the management of multiple Node.js versions.
To install nvm, use the provided install script:
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install.sh | bash
Then install the latest Node version:
$ nvm install node
You can also install a specific Node version, by passing the major, minor, and/or patch versions:
$ nvm install 6
$ nvm install 4.2
You can then switch versions by passing the version the same way you do when installing:
$ nvm use 5
You can set a specific version of Node that you installed to be the default version by entering:
$ nvm alias default 4.2
To display a list of Node versions that are installed on your machine, enter:
$ nvm ls
When Node is installed via nvm we don't have to use sudo
to install global packages since they are installed in home folder. Thus npm i -g http-server
works without any permission errors.
Installing Node.JS in Windows
Node.js can be installed globally or downloaded as a portable binary.
Download here: https://nodejs.org/en/download/
The installer is labelled Windows installer (.msi)
. The portable version contains only one file (node.exe
) and is labelled Windows Binary (exe)
.
Installing Node.js on Mac using Homebrew
You can install Node.js using the Homebrew package manager.
Start by updating brew:
brew update
You may need to change permissions or paths. It's best to run this before proceeding:
brew doctor
Next you can install Node.js by running:
brew install node
Once Node.js is installed, you can validate the version installed by running:
node -v
Install Node.js From Source on Ubuntu
Prerequisites
sudo apt-get install build-essential
sudo apt-get install python
[optional]
sudo apt-get install git
Get source and build
cd ~
git clone https://github.com/nodejs/node.git
OR For node version 6.3.0
cd ~
wget https://nodejs.org/dist/v6.3.0/node-v6.3.0.tar.gz
tar xzvf node-v6.3.0.tar.gz
Change to the source directory such as in cd ~/node-v6.3.0
./configure
make
sudo make install
Install Node.JS via Version Manager
A better way to install node is to use a version manager such as NVM, because it allows you to quickly switch between different versions of Node quickly.
OSX:
- You'll need to install the Command Line Tools beforehand.
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install.sh | bash
Windows:
- Download the executable of nvm-windows from here
Installing using MacOS X Installer
- Download the installer from Node.js official site.
- Upon completion of download, open the package and follow on-screen instructions
- Re-initialize your terminals (if they're already open)
You should now be able to use node
command from your terminals.
Installing with Node Version Manager under Fish Shell with Oh My Fish!
Node Version Manager (nvm
) greatly simplifies the management of Node.js versions, their installation, and removes the need for sudo
when dealing with packages (e.g. npm install ...
). Fish Shell (fish
) "is a smart and user-friendly command line
shell for OS X, Linux, and the rest of the family" that is a popular alternative among programmers to common shells such as bash
. Lastly, Oh My Fish (omf
) allows for customizing and installing packages within Fish shell.
This guide assumes you are already using Fish as your shell.
Install nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.4/install.sh | bash
Install Oh My Fish
curl -L https://github.com/oh-my-fish/oh-my-fish/raw/master/bin/install | fish
(Note: You will be prompted to restart your terminal at this point. Go ahead and do so now.)
Install plugin-nvm for Oh My Fish
We will install plugin-nvm via Oh My Fish to expose nvm
capabilities within the Fish shell:
omf install nvm
Install Node.js with Node Version Manager
You are now ready to use nvm
. You may install and use the version of Node.js of your liking. Some examples:
- Install the most recent Node version:
nvm install node
- Install 6.3.1 specifically:
nvm install 6.3.1
- List installed verisons:
nvm ls
- Switch to a previously installed 4.3.1:
nvm use 4.3.1
Final Notes
Remember again, that we no longer need sudo
when dealing with Node.js using this method! Node versions, packages, and so on are installed in your home directory.