Setup Nodejs in Ubuntu

1 minute read

This tutorial has been updated with installation instruction for nodejs6.


To setup nodejs in ubuntu run following command

sudo curl -sL | sudo -E bash -
sudo apt-get install -y nodejs

First command adds nodejs repo to software sources. This might be helpful in getting update automatically whenever you run sudo apt-get-update. Second command is simply software installation command in ubuntu.

This will also install install npm. Now to install other node packages you can run command like sudo npm install <package name>.

Locating node modules

Location of node_modules may be hard to find. Here is how I found out:

I listed all global node modules with this command: `npm list -g –depth=0’. Output was like:

├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

First line tells the location of node modules, which was /usr/lib.

Avoid running npm as root

Recently I ran into problem where running npm with sudo command would do nothing and npm command without sudo would not install packages either. Thus I thought of changing owner of node modules to myself so that I need not use npm as root user. Turned out that; not running npm as root user is actually a good practice. So here is what can be done, second method is my preferred one as I don’t format my /home folder when I format or fresh install newer ubuntu versions and I wont need to reinstall global node packages later.

option1: Change the permission to npm’s default directory

sudo chown -R `whoami` /usr/lib/node_modules/

option 2: Change npm’s default directory to another directory

# make a directory for global installations:
mkdir ~/.npm-global
# configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
# Open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
# Back on the command line, update your system variables:
source ~/.profile

Now we no longer need to run npm with sudo command. This can be tested with following command:

npm install -g jshint




Leave a Comment