Developer tools (such as GCC) are an optional installation on Mac OS X. Fortunately, they're easy to acquire.
You start with Xcode, which is available, for free through the Mac App Store. Simply search for Xcode and click on the Get button. Once you have Xcode installed, open a Command Prompt window and type the following:
$ xcnwode-select --install
This installs the Xcode command-line tools.
For additional information, visit http://osxdaily.com/2014/02/12/install-command-line-tools-mac-os-x/.
First, download the source from http://nodejs.org/download. One way to do this is with your browser and another way is as follows, substituting your preferred version:
$ mkdir src $ cd src $ wget https://nodejs.org/dist/v4.2.6/node-v4.2.6.tar.gz $ tar xvfz node-v4.2.6.tar.gz $ cd node-v4.2.6
The next step is to configure the source so that it can be built. It is done with the typical sort of configure script and you can see its long list of options by running the following:
$ ./configure –help.
To cause the installation to land in your home directory, run it this way:
$ ./configure –prefix=$HOME/node/4.2.6 ..output from configure
If you're going to install multiple Node.js versions side by side, it's useful to put the version number in the path like this.
If you want to install Node.js in a system-wide directory, simply leave off the -prefix
option and it will default to installing in /usr/local
.
After a moment, it'll stop and will likely have successfully configured the source tree for installation in your chosen directory. If this doesn't succeed, it will print a message about something that needs to be fixed. Once the configure script is satisfied, you can go on to the next step.
With the configure script satisfied, you compile the software:
$ make .. a long log of compiler output is printed $ make install
If you are installing into a system-wide directory, do the last step this way, instead:
$ make $ sudo make install
Once installed, you should make sure that you add the installation directory to your PATH
variable as follows:
$ echo 'export PATH=$HOME/node/4.2.6/bin:${PATH}' >>~/.bashrc $ . ~/.bashrc
Alternatively, for csh
users, use this syntax to make an exported environment variable:
$ echo 'setenv PATH $HOME/node/4.2.6/bin:${PATH}' >>~/.cshrc $ source ~/.cshrc
This should result in some directories like this:
$ ls ~/node/4.2.6/ bin include lib share $ ls ~/node/4.2.6/bin
Normally, you won't have multiple versions of Node.js installed and doing so adds complexity to your system. But if you are hacking on Node.js itself, or are testing against different Node.js releases, or are in any similar situation, you may want to have multiple Node.js installations. The method to do so is a simple variation on what we've already discussed.
If you noticed during the instructions discussed earlier, the –prefix
option was used in a way that directly supports installing several Node.js versions side by side in the same directory:
$ ./configure –prefix=$HOME/node/4.2.7
And:
$ ./configure –prefix=/usr/local/node/4.2.7
This initial step determines the install directory. Clearly when version 4.2.7 or version 5.5.3 or whichever version is released, you can change the install
prefix to have the new version installed side by side with previous versions.
To switch between Node.js versions is simply a matter of changing the PATH
variable (on POSIX systems), as follows:
$ export PATH=/usr/local/node/4.2.7/bin:${PATH}
It starts to be a little tedious to maintain this after a while. For each release, you have to set up Node.js, NPM, and any third-party modules you desire in your Node install. Also, the command shown to change your PATH
is not quite optimal. Inventive programmers have created several version managers to simplify setting up not only Node.js but also NPM and providing commands to change your PATH
the smart way:
Both maintain multiple simultaneous versions of Node and let you easily switch between versions. Installation instructions are available on their respective websites.
For example, with nvm
, you can run commands like these:
$ nvm ls v0.10.40 v0.12.7 v4.0.0 v4.1.1 v4.1.2 v4.2.0 v5.1.0 v5.3.0 v5.5.0 -> system node -> stable (-> v5.5.0) (default) stable -> 5.5 (-> v5.5.0) (default) $ nvm use v5 Now using node v5.5.0 (npm v3.3.12) $ node --version v5.5.0 $ nvm use v4.2 Now using node v4.2.0 (npm v2.14.7) $ node --version v4.2.0 $ nvm install v5.4 ######################################################################## 100.0% Checksums empty Now using node v5.4.1 (npm v3.3.12) $ node --version v5.4.1 $ /usr/bin/node --version v0.10.40
This demonstrates that you can have a system-wide Node.js installed and keep multiple private Node.js versions and switch between them as needed. When new Node.js versions are released, they are simple to install with nvm
even if the official packaged version for your OS doesn't immediately update.