summaryrefslogtreecommitdiff
path: root/_site_build/_posts/2012-05-06-installing-node-js-on-ubuntu.markdown
blob: 1ac5b3cd50741b5ddc67dae2b00667a99aade62c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
---
layout: post
title: Installing Node.js on Ubuntu
author: David T. Sadler
description: This guide will show you how to install Node.js on Ubuntu.
categories: ["Node.js","Ubuntu"]
robots: follow, noodp, noydir, noarchive
comments: true
google_plus: true
twitter_share: true
facebook_like: true
published: true
licensed: true
---
This guide will show you how to install Node.js on Ubuntu from either the source code or Git repository. I'm going to assume that you are using Ubuntu 12.04 (Precise Pangolin) and that you wish to install version 0.6.17 of Node.js. You're results may vary if you are using different versions.

## Getting started.

There are a few things that we require before we can install Node.js. Firstly we need a compiler which can be got by installing the `build-essential` package. This contains tools (such as the gcc complier, make tool, etc) for compiling/building software from source.

{% highlight bash %}
sudo apt-get update
sudo apt-get install build-essential -y
{% endhighlight %}

You will need `Git` if you are going to install from the repository.

{% highlight bash %}
sudo apt-get install git -y
{% endhighlight %}

Node.js itself requires very little in the way of dependencies.

* python - Version 2.6 or 2.7.
* libssl-dev - You will need this if you plan to use SSL/TLS encryption.

{% highlight bash %}
sudo apt-get install python libssl-dev -y
{% endhighlight %}

Wtih the dependencies installed we can now move onto installing Node.js itself. Since there is a very good chance that Node.js has been updated since this was written, you should check the <a href="http://nodejs.org/#download" rel="external nofollow" target="_blank" title="Go to the Node.js website">website</a> for the latest version number and substitute it for the one used in the rest of this post. You have two options when it comes to the installation. You can download the source code or clone the Git repository. The process is similar for both methods and so you should choose which ever one you are comfortable with. I personally use Git as I find it easier to update Node.js to the latest version.

* [Installing from source.](#from-source)
* [Installing with Git.](#via-git)

<h2 id="from-source">Installing from source.</h2>

You will need to download the tar archive of the source code and unpack it into a suitable directory. We will create this directory in `/usr/local/src`. Note that you will have to use the `sudo` command in order to write to this directory.

{% highlight bash %}
cd /usr/local/src
sudo mkdir node
cd node
sudo wget http://nodejs.org/dist/v0.6.17/node-v0.6.17.tar.gz
sudo tar -xzvf node-v0.6.17.tar.gz
{% endhighlight %}

We now need to enter the extracted directory and configure the code. The `configure` script checks your system to see if the required dependencies are present. Since we have installed these earlier it should report that everything is ok. Note that by default the `configure` script will ensure that Node.js is installed globally for the whole system. If you wish to install it for use by a single user you can follow the extra instructions [here](#single-user).

{% highlight bash %}
cd node-v0.6.17
sudo ./configure
{% endhighlight %}

The `make` command can now be used to compile and install Node.js.

{% highlight bash %}
sudo make
sudo make install
{% endhighlight %}

This will result in the commands `node` and `npm` been installed into the `/usr/local/bin` directory. Now that Node.js has been installed you may wish to try the example [application](#application).

## Upgrading from source.

To upgrade Node.js from source simply download the latest tar archive and repeat the above installation process. The updated version will overwrite the previous version.

<h2 id="via-git">Installing with Git.</h2>

The first step is to clone the repository into a suitable directory. For this guide we will use `/usr/local/src`. Note that you will have to use the `sudo` command in order to write to this directory.

{% highlight bash %}
cd /usr/local/src
sudo git clone git://github.com/joyent/node.git
{% endhighlight %}

We can now enter the cloned repository and checkout the v0.6.17 branch.

{% highlight bash %}
cd node
sudo git checkout v0.6.17
{% endhighlight %}

We now need to configure the source code by using the provided `configure` script. This checks your system to see if the required dependencies are present. Since we have installed these earlier it should report that everything is ok. Note that by default the `configure` script will ensure that Node.js is installed globally for the whole system. If you wish to install it for use by a single user you can follow the extra instructions [here](#single-user).

{% highlight bash %}
sudo ./configure
{% endhighlight %}

The `make` command can now be used to compile and install Node.js.

{% highlight bash %}
sudo make
sudo make install
{% endhighlight %}

This will result in the commands `node` and `npm` been installed into the `/usr/local/bin` directory. Now that Node.js has been installed you may wish to try the example [application](#application).

## Upgrading via Git.

To upgrade Node.js you need to go back into the cloned repository and pull down the latest source code.

{% highlight bash %}
cd /usr/local/src/node
sudo git checkout master
sudo git pull origin master
{% endhighlight %}

You can then checkout the version branch that you wish to upgrade to. To check which versions are available use the `git tag` command. The upgrade is then performed by using the normal install commands.

{% highlight bash %}
sudo git checkout vx.x.x
sudo ./configure
sudo make
sudo make install
{% endhighlight %}

<h2 id="single-user">Installing for a single user.</h2>

Node.js can be easily installed for a single user with the modified process outlined below. For the sake of brevity I will show how to do this using the Git repository. The method is the same when using the source code.

The idea is that we create a custom directory located in the user's home directory and configure Node.js to install itself there by using the `--prefix` option. For this example we will create a directory called `~/local/src` into which we will clone the Git repository. We then configure the code so that Node.js will be installed into the directory `~/local`. Note that because we are installing into the user's home directory we no longer need the `sudo` command.

{% highlight bash %}
mkdir -p ~/local/src
cd ~/local/src
git clone git://github.com/joyent/node.git
cd node
git checkout v0.6.17
./configure --prefix=~/local
make
make install
{% endhighlight %}

The install process will create several new directories under the `~/local` directory. One of them is the `bin` directory that contains the `node` and `npm` commands. Since the system will not look in this directory when searching for commands entered by the user, we will need to update the `PATH` environment variable.

{% highlight bash %}
echo 'PATH=$PATH:$HOME/local/bin' >> $HOME/.bashrc
source ~/.bashrc
{% endhighlight %}

Now when ever you enter `node` or `npm` on the command line the system will know where to locate them.

<h2 id="application">Example application.</h2>

This example is taken directly from the Node.js <a href="http://nodejs.org" rel="external nofollow" target="_blank" title="Go to the Node.js website">website</a> and is a web server that responds with 'Hello World' for every request.

{% highlight bash %}
cat > ~/server.js <<EOF
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
EOF
{% endhighlight %}

The server is started with the following command.

{% highlight bash %}
node ~/server.js
{% endhighlight %}

If everything has gone according to plan you should see 'Hello World' when you open a web browser at `http://localhost:1337/`.

For those of you who are working from the console and may not be able to see the results in a desktop browser, KENTOSI added a very good tip via the <a href="/archives/2012/05/06/installing-node-js-on-ubuntu/#comment-645459856" rel="bookmark">comments</a>. Simply install a text-based web browser such as lynx.

{% highlight bash %}
sudo apt-get install lynx
{% endhighlight %}

You can then use this browser to connect to the running web server and the results will be displayed onto the console.

{% highlight bash %}
lynx -dump http://127.0.0.1:1337/
{% endhighlight %}