[Tutorial] Installing Node.js/NPM Without Sudo

Node JS / NPM have become essential parts of many applications. Because of this, and my recent experience with Node, I have decided to compile some instructions on how to install Node JS/NPM so that you do not require the sudo command (I found I needed this constraint when using Ember-CLI).

The Problem

Note: this tutorial was run on XUbuntu 14.04 (Ubuntu) & OSx Mavericks

There are many valid ways to install Node and NPM, so if you know of a different method that works for you by all means, stick with that. This tutorial on installing Node/NPM is directed towards those who have run into the unbearable EACCES errors when trying to:

  • install npm
  • use npm install (for packages such as bower, etc.)
  • etc.

I spent quite a bit of time searching for solutions to this dilemma. It seemed for a while that I would have to run npm with sudo, but I knew that would just cause issues down the road.

Many solutions suggested modifying permissions, using sudo chown -R 'whoami' [directory] or other permission commands, in order to give your user the appropriate permissions to run npm and it's packages without sudo. Unfortunately even modifying the permissions didn't always solve my problem.

Pre-requisites:

Before following the steps below to install node/npm, you should make sure that there are no prior existing versions of node/npm on your system. There are several ways to see if you have node/npm installed on your system. I prefer to use the following commands to show where node/npm exist if they are present on your system:

$ which node
$ which npm

If there is no output after either of the above, then that respective package is not present on your system (whether it be node or npm).

NPM has a good article on how to properly remove NPM. Most of the time the following command will suffice for removing NPM:

$ sudo npm uninstall npm -g

If it does not, I suggest taking a quick gander at the linked article.

Removing node is a bit more tedious, at least I found it to be. The way I went about it was to simply purge my system of all folders that referenced node. I followed the step listed in this StackOverflow answer. I won't restate the answer as it is fairly brief and just involves removing a few folders.

Note: if you installed node to your home directory the files will likely be in a different location than listed in the linked answer (in your home directory instead of the root).

There are other solutions for removing node/npm out there so feel free to leave a comment with any other solutions, or concerns.

The Solution

Fortunately I stumbled on a solution that did not require modified permissions. We will use the Node Version Manager (NVM) to install Node/NPM. The installation instructions are on their linked page, but I will re-state them in this post so that all the instructions to get Node & NPM working are in one concise page for you.

I've also included instructions, specifically for OSx, on how to install Node with Homebrew. These instructions are mostly just referenced from other posts, which I have added links to in the References section. I've included these because using Homebrew on OSx is very popular and convenient.

Installing With NVM

First, open a terminal window and enter one of the following commands:

use curl:

$ curl https://raw.githubusercontent.com/creationix/nvm/v0.12.1/install.sh | bash

or use wget:

$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.12.1/install.sh | bash

Once you've run either of those commands and NVM has finished downloading, run the following command:

$ source ~/.profile

Now NVM commands should be working in the terminal, try $ nvm to confirm the NVM command is working. If not, try restarting your terminal.

Once NVM is working you can install a version of node by using the following command:

$ nvm install [version-no]

To list the available versions, run this command:

$ nvm ls-remote

Although, I find it preferrable to check which is the current version on NodeJS. The current version, at the time of writing this article, is v0.10.29. To install this version run:

$ nvm install 0.10.29

You check which version of Node NVM activated by entering the following command:

nvm current

To check in general what version of Node is being used, use this command:

node -v

To check in general what version of NPM is being used, use this command:

npm -v

If you run the

$ which node

command you will see that node is installed in your home directory which is okay for the development of applications. In fact, from what I've experienced so far it is preferrable in some cases. You don't have to deal with permission errors and modifying permissions of root folders. However, once you've moved into production it is recommended that node get moved into a global/root location, as to prevent the client from modifying important configurations.

Installing with homebrew (OSx)

This portion is for OSx users only, I assume you already have installed homebrew on your machine. If not, visit the site for instructions: HomeBrew

This portion is heavily referenced from another blog, as I am not the most familiar with OSx & homebrew

Installing Node/NPM with homebrew is very simple. First, update your brew:

$ brew update

Then run doctor to check for any errors/warnings (you do not have to address all warnings, but it is a good idea to read over the warnings)

$ brew doctor
$ brew prune

Add the Homebrew location to your $Path and source your bash or zsh profile file after adding/saving this:

$ export PATH="/usr/local/bin:$PATH"

Now install Node:

$ brew install node

Ready to Install Packages

That's it, you now can use Node/NPM without using sudo! For example to get the dependencies for Ember-CLI, enter the following three commands:

$ npm install -g ember-cli     # Install Ember-cli
$ npm install -g bower         # Install bower package manager
$ npm install -g phantomjs     # Install phantomJS (integration testing)

To create a new ember project use:

$ ember new [project name]

If there are any questions, recommendations or concerns feel free to leave a comment below, or message me on Twitter @TylerWen. I will consistently alter/add to this article to ensure the solutions are relevant with on-going changes.

References