How to install node.js, the easy way
We are going to look at how to install node.js the easy way on Mac OS X, Windows and Linux.
You don't need to download the node package and try to install it. No! We are going to do it the easy way using package managers.
Once you have node installed you will be able to run server-side JavaScript on your computer.
We will also look at how you can upgrade node so you always have the latest version.
Chances are, you are only trying to install node on one of these operating systems (OS).
Click the link below to install node.js for your OS:
How to Install Node.js on Mac OS X
The easiest way to install Node.js on Mac OS X is to use Brew.
Brew is a package manager which is a tool that allows you to easily install software on your Mac. First, we need to install Brew. To do that you need to run this command using the terminal app:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Once this has finished you will be able to check that Brew has installed using this command:
brew --version
We are now ready to install node, by running this command:
brew install node
It will take a few minutes to install but once it has finished you will be able to check the version of Node by running:
node --version
When working with node there will be times when you need to change version or run the latest version of node.
We will look at how you can change node version at the end of the tutorial.
How to Install Node.js on Windows
To install node on Windows I recommend that you use a package manager. This can be either Chocolatey or Scoop.
If you have neither of these installed then let's go ahead and install Scoop. To do that you need to open PowerShell. Click Start and type PowerShell into the search bar. Then double-click to open the application.
Once inside PowerShell, you will need to run this command:
iwr -useb get.scoop.sh | iex
When the installation is complete you can verify the install by running this command:
scoop help
Now we are ready to install node. To do that run the following command:
scoop install nodejs
Once complete you can check that the installation has worked by running:
node --version
Now that you have node installed you may need to upgrade it in the future. There are also times when you need to run a specific version of node. To do this we can use a node version manager.
How to Install Node.js on Linux
The easiest way to install node on Linux is to use the Advanced Package Tool (APT). We will look at installing node on both Ubuntu and Debian-based Linux distros.
The default source for node.js in APT is a bit outdated so we need to give a new source. After adding the source you can then install node.
First, let's look at how we do this in Ubuntu:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
It is a similar process in Debian, like this:
curl -sL https://deb.nodesource.com/setup_8.x | bash -
apt-get install -y nodejs
Now that you have node installed you can verify that it is working by running:
node --version
Now that you have node.js installed, make sure to install a node version manager. This allows you to easily upgrade node to the latest version.
How to upgrade Node.js
Once you have Node.js installed you may need to upgrade it or change the version. You can only have a single version of node installed at once so how can you do it?
When you install node it also installs a program called NPM which stands for Node Package Manager.
You can use npmjs.com/ to find packages that you might want to install. One of these packages is called n. n
will let you change the version of node that is running.
To install n
we need to run the following command:
npm i n -g
Here we have told NPM to install the n
package globally (-g). This means that you can now access the n
from the command line. To verify the install you can run:
n --version
You should see the version number printed in the command or terminal.
Now there are a few useful commands. The first is to upgrade node to the latest version to do this run:
n latest
n
will then fetch the latest version of node and install it. Once complete you can verify the install by checking the node version:
node --version
By installing the latest version of node you will get all the features but often there are bugs too. To install the most stable version you should install the Long Term Support (LTS) version of node. To do that with n
run this command:
n lts
You can also install a specific version of node. For example, if we wanted to install version 10.16.0 of node then we would run:
n 10.16.0
Now you have an easy way of upgrading or running a specific version of node.
Wrapping Up, How to Install Node.js
We have looked at how to install node.js on Windows, Mac OS X and Linux.
With Mac OS X we used the brew which is a package manager. Once you installed brew it is easy to install node with a single command.
The Windows installation also used a package manager called Scoop. With Scoop installed you can open PowerShell and install node.js.
Finally, we looked at Linux and upgraded the node.js source for APT and then installed node via apt-get
.
You can use n
the node version manager to upgrade node on all operating systems. This allows you to specify a specific version of node to run. You also have the option of upgrading to the latest version of node or the Long Term Support version.
This makes it much easy to manage multiple versions of node on a computer.