Installing Node.js and npm on Ubuntu 14.04

edit: 2016, Mar 14th

I’ve written a short and updated installation guide for node in a new post. You should consult Installing node LTS version on Ubuntu 14.04 if you are looking to do a new install of node on Ubuntu 14.04. This post will remain available for reference.

edit: 2016, Jan 26

The install notes below are very out of date at this point– the versions of nodejs have changed very quickly in the past year! That said, the current install instructions seem to be very similar to those described below; check out the instructions for Ubuntu at nodejs: node install via package manager. Version 4.x seems to be the more stable, long-term version but you can install the 5.x version if you like.

Some notes on removing nodejs

Obviously I would like to remove the version of modejs installed below as well as remove the associated repository– you should only follow along if you are in the same position; also be sensible and make sure you are backed up etc. So, first up is to find out what repository was used for the nodejs install:

$ apt-cache policy nodejs
nodejs:
  Installed: 0.10.41-1nodesource1~trusty1
  Candidate: 0.10.41-1nodesource1~trusty1
  Version table:
 *** 0.10.41-1nodesource1~trusty1 0
        500 https://deb.nodesource.com/node/ trusty/main amd64 Packages
        100 /var/lib/dpkg/status
     0.10.25~dfsg2-2ubuntu1 0
        500 http://us.archive.ubuntu.com/ubuntu/ trusty/universe amd64 Packages

From this output we can see that https://deb.nodesource.com/node/ trusty/main is the repository used to install version 0.10.41 of node. Let’s purge nodejs:

$ sudo apt-get purge nodejs

The repostory can be removed by deleting two file in the /etc/apt/sources.list.d/ directoy using

$sudo rm /etc/apt/sources.list.d/nodesource.list*

Okay, that’s it. Time to install new versions– use the link above.

end edit

original post follows

I’ve decided to start being systematic about learning javascript, with a focus on getting good with d3js. I’ll be installing nodejs and npm (node package manager) as a way to get access to a javascript console and, for later, a powerful javascript environment. As always, constructive comments and questions are welcome and much appreciated.

I’ll be using the instructions at the nodejs github wiki that allows for the use, and updating, of more current versions of nodejs and npm. It is worth noting that both nodejs and npm are in the Ubuntu repository and can be installed with the usual sudo apt-get install. However, installing this way leaves the user stuck with older versions of the relevant software.

So, following the nodejs github wiki, the install is as simple as:

$ curl -sL https://deb.nodesource.com/setup | sudo bash -
$ sudo apt-get install -y nodejs

That’s it, we’re done. A check of the nodejs version gets:

$ node --version
v0.10.35

and, for npm:

$ npm --version
1.4.28

Finally, some examples of how I will use nodejs to learn javascript. First, I can use the console by starting node with no arguments:

$ node

Next, print Hello world! and exit:

> console.log("Hello world!")
Hello word!
undefined
> process.exit()

This provides access to the javascript console and makes it possible for me to try out simple commands.

Second, for longer chunks of code it is nice to be able to execute a file from the command line, just like I would with bash or Python. To do this, create the file helloworld.js with the following code:

#! /usr/bin/env node
// helloworld.js

console.log("Hello World!")

Notice that I’ve added a shebang to the top of the file that uses node to execute the file. Next, make the file executable:

$ chmod u+x helloworld.js

Then, execute the file from the bash prompt:

$ ./helloworld.js
Hello World!

Of course, you can also skip making the file executable and call node directly:

$ node helloworld.js
Hello World!

Either way, using this second setup makes it much easier (at least for me) to work with larger and more complex javascript code– very cool. If you are looking for more examples, starting at the level above and moving to more advanced material try of this beginners nodejs post.

If you know of good (online) learning resources for javascript, node and/or d3 please leave comments for everyone that reads the post– thanks!

Tags

api [1]   arduino [1]   audio [2]   audio features [1]   babel [1]   Bayesian [7]   Beta [1]   blog setup [1]   bootstrap [1]   bottleneck [1]   c++ [1]   caret [1]   cmpy [1]   conditional probability [6]   coursera [1]   coursera intro to data science [3]   css [1]   cython [1]   d3 [2]   decision trees [2]   diy [1]   dropbox [1]   dsp [1]   e1071 [1]   essentia [1]   garmin [1]   geojson [1]   ggplot2 [1]   gis [2]   git [1]   gnuplot [1]   graphs [1]   html5 [1]   igraph [1]   ipython [1]   javascript [7]   joint probability [6]   json [1]   LaTeX [2]   LDA [1]   Lea [2]   machine learning [3]   marginal probability [6]   matplotlib [1]   meteor [2]   mir [1]   MongoDB [3]   music [2]   my python setup [5]   my ubuntu setup [10]   mysql [3]   networks [1]   networkx [1]   nodejs [5]   npm [3]   numexpr [1]   numpy [1]   octave [1]   Open Oakland [2]   openpyxl [1]   pandas [3]   patsy [1]   pip [2]   pweave [1]   pygraphviz [1]   pymc [1]   PySoundFile [2]   python [15]   Python [1]   python 2.7 [5]   python 3.4 [2]   pyyaml [1]   qgis [1]   R [1]   randomForest [1]   restview [1]   resume [1]   rpart [1]   running [1]   scikit-learn [3]   scipy [1]   screen [1]   server setup [1]   shapefile [1]   social networks [1]   Socrata [1]   sound [2]   sphinx [1]   sql [4]   sqlite3 [1]   ssh [1]   ssh keys [1]   statsmodels [1]   supervised learning [2]   sympy [1]   tableau [1]   tinkerer [2]   topic models [1]   tree [1]   ubuntu 14.04 [13]   Ubuntu 14.04 [3]   ubuntu 16.04 [4]   vim [2]   virtualbox [1]   virtualenv [4]   virtualenvwrapper [3]   VPS [1]   vundle [1]   webpack [1]   yaml [1]