In this section, we will get node.js and npm installed and running on our machines. Earlier node.js and npm required separate installations. But in later versions npm made it to the core of node.js, and now comes bundled with it. We will use command line extensively throughout this book, including during installation. npm does not provide any tool with graphical user interface, so some familiarity with the command line is necessary to work with it.
Before beginning to install node.js, you will need to check that your system meets the following requirements:
Disk Space: 50 MB free (min). This much should suffice for all our needs of installing node.js and creating our own programs. However, as you begin using elaborate node.js packages and libraries, you might need more space to store them.
Memory: 256 MB (min); 1 GB (recommended).
Operating System: node.js supports a wide range of operating systems including Windows, Mac, Solaris, and Linux. It is unlikely that you will fail to install node.js due to an operating system.
Download instructions vary for different platforms. We have provided instructions for three most popular platforms, Windows, Mac, and *nix flavored operating systems. The instructions for Linux hold for most of the derivatives and distributions. You need to download the respective files from node.js downloads page: http://nodejs.org/download.
The easiest way to install node.js on Windows/Mac is through respective installers.
Download the Windows installer
.msi package from the download page (http://nodejs.org/download). If you are using Mac, download the Mac
There are lots of ways of installing node on *nix flavored Operating Systems. The instructions given here will hold for most of them.
There is a one-step installation process by which you can install node.js through command line. But that may not always work.
$ echo prefix = ~/local >> ~/.npmrc $ sudo curl https://npmjs.org/install.sh | sh
Following is a step-by-step guide for manual installation.
Go to http://nodejs.org/download/ and choose Linux Binaries. Choose a 32-bit or a 64-bit version according to your machine's CPU architecture. If you don't know what this means, choose 32-bit. A
tar.gz file will be downloaded with a name similar to
node-v0.10.5-linux-x86.tar.gz. The file name might differ depending upon the version of node, operating system, and your computer architecture. In general, the name will follow the pattern, node-v<version>-<OS>-<architecture>
Extract the file either using your favorite GUI based extractor or using command line.
$ cd path/to/downloaded/tar-file $ tar -xvf node-v0.10.5-linux-x86.tar.gz
You should be left with a folder named
$ cd node-v0.10.5-linux-x86/bin $ ./node -–version && ./npm --version
This should print something like the following:
Binaries are runnable pieces of code that can be directly run from the command line. This package has given us exactly that. But to run them, we need to locate them on our machine. We don't want to remember where we have stored the binaries. That's why we will put them in a location which is present in our system's
PATH variable so that they can be accessed from anywhere. To do that, we will symlink the binaries to:
$ sudo ln -s ./node /usr/local/sbin/node $ sudo ln -s ./npm /usr/local/sbin/npm
If you do not have super user privilege on your machine you can add the node.js binary folder on your system's
PATH variable instead:
$ export PATH=PATH:/path/to/nodejsfolder/node-v0.10.5-linux-x86/bin
$ cd to/a/random/location/on/your/computer $ node –-version && npm –-version v0.10.5 1.2.10
Various Linux distributions provide node.js from their package distributions. You can install node.js from your respective package distribution. Specific instructions for most distros can be found at https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager. Usually these distros do not keep up with the node.js development and are several versions old.
On *nix systems, installing node directly as binaries doesn't help much in keeping the installation up to date. In fact, you will have to repeat the exact instructions again for the version you want to upgrade or downgrade to. node version manager (nvm) is a shell program that provides a convenient way of trying multiple node.js versions at the same time.
Switching between two versions is just a matter of running one shell command. It does this by creating a virtual environment. What this means is that nvm keeps binaries in a user's home directory. Whenever the user instructs it to change versions, nvm manipulates the system's
PATH variable so that it exposes the binary requested.
Github URL for nvm is https://github.com/creationix/nvm.git where you can find the instructions for installing and using nvm. In a nutshell, following sequence of instructions will install node v0.10.
wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh source ~/.nvm/nvm.sh # do it every time a new shell is started nvm install 0.10 nvm use 0.10
You can install different versions of node by using
nvm install command and switch between these versions by using
nvm use command. Note that the second command,
source ~/.nvm/.nvm.sh has to be run every time a new shell is started. To automate this step, add the command at the end of
A similar program called nave also exists which accomplishes the same purpose. It can be found at https://github.com/isaacs/nave. Nave also creates a virtual environment for testing out different versions of node. There are subtle differences between nvm and nave, the major one being that nvm downloads binaries directly, while nave compiles each version from source. Nave requires you to have a C++ compiler installed on your system.
You can go through the
README of both the programs and choose for yourself the one that fits your needs. Generally any one of them should work for most purposes.
Installing node through nave or nvm is recommended since it will help you test your modules between different versions quickly.
The downside of using virtual environment is that it will be available only for the user who has installed it and not other users on the same system.
If you are interested in building and installing node.js from its source code, the detailed instructions are available on the wiki of node.js github repository. Here is the link: https://github.com/joyent/node/wiki/Installation.