Installing NodeJS on Ubuntu 14.04

Installing NodeJS on an installation of Ubuntu Server 14.04 is fairly straight forward but most tutorials I’ve found gloss over some details that I constantly run into any time I install it so I thought I’d clarify the procedure that I had to follow to get it installed successfully.
Run the following commands to install NodeJS and NPM

sudo apt-get install nodejs
sudo apt-get install npm

After I had it installed, I tried to install the excellent project scaffolding tool Yeoman but immediately ran into permission errors as shown below.

chris@daenerys:/data/www$ npm -g install yo
npm http GET https://registry.npmjs.org/yo
npm http 304 https://registry.npmjs.org/yo
npm WARN engine yo@1.7.0: wanted: {"node":">=0.12.0"} (current: {"node":"v0.10.25","npm":"1.3.10"})
npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules'
npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/usr/local/lib/node_modules',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/usr/local/lib/node_modules/yo',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ '/usr/lib/nodejs/fstream/lib/writer.js:171:23',
npm ERR! '/usr/lib/nodejs/mkdirp/index.js:37:53',
npm ERR! 'Object.oncomplete (fs.js:107:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! System Linux 3.13.0-79-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "-g" "install" "yo"
npm ERR! cwd /data/www
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! path /usr/local/lib/node_modules
npm ERR! fstream_path /usr/local/lib/node_modules/yo
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules'
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:171:23
npm ERR! fstream_stack /usr/lib/nodejs/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /data/www/npm-debug.log
npm ERR! not ok code 0

This is a fairly well known problem and is even documented on the NodeJS website but I needed another step in between. The NodeJS directory wasn’t created so the next step failed. I had to create the directory, then change the ownership:
NOTE: Be very aware that you can properly screw up your whole OS if you do not verify that the prefix is /usr/local and not just /usr. You will change the permissions of everything in the /usr/bin directory (including sudo) and it will all break. I learnt this the hard way. Don’t do it!

chris@daenerys:/data/www$ sudo mkdir /usr/local/lib/node_modules
chris@daenerys:/data/www$ sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

When I tried to install Yeoman again, I got the weird error:

npm ERR! weird error 127

This was fixed by simply running:

apt-get install nodejs-legacy

That’s it! That got NodeJS installed on my Ubuntu 14.04 Server system!

9 thoughts on “Installing NodeJS on Ubuntu 14.04

  1. It is remarkable, rather amusing phrase

  2. I think, that you commit an error. I can prove it.

  3. I have read so many articles or reviews about the blogger lovers but this article is in fact a nice post, keep it up. Karna Read Lucienne

  4. I join. So happens. We can communicate on this theme.

  5. In my opinion you are mistaken. I can defend the position. Write to me in PM, we will discuss.

Leave a Comment

Your email address will not be published. Required fields are marked *