How to install and manage Node.js, sudo free, with NVM

By: Dean McDonnell

There are many ways to install Node.js, from package managers to installers; you can even download and build from source! Unfortunately, the vast majority of installations can put Node.js in a location that requires the need for elevated permissions when using either the node or npm commands. Today we will look at how to clean up and reinstall Node.js so that there is no need to use elevated permissions; as a bonus we will also gain the ability to jump between Node.js versions and even IO.js with ease.

sudo comic

Image Source :

You can tell if you need elevated permissions to run Node.js based on whether or not you need to append commands with sudo, which stands for Super-User DO. Any command run by prefixing it with sudo will be run with elevated permissions.

Requiring the need for sudo on Unix-like operating systems when using Node.js at the command line is not only laborious, but also likely to lead to all sorts of strange issues for certain globally installed Node.js modules.

There are 2 ways to enable the use of Node.js sudo free:

  • Download and build Node.js from source to a directory you have permissions for.
  • Use a Node.js version manager that works within your home directory.

For this post we will look at using a Node.js version manager, NVM, to enable sudo free usage of any node or npm based commands. Building from source sure is a fun way to spend an afternoon and we highly encourage it! but for everyday use, a good Node.js version manager is the way to go.

NOTE: If you are a Windows user you will be pleased to know that the problem described in this article does not apply to you. Go grab a coffee, you earned it.

IMPORTANT: sudo free is not technically true. Some global modules may require sudo if they are installing or working with directories that are only available with root permission. Still, nearly always is better than never!

Before we begin

If you do not require the use of sudo due to changing file permissions, you should reset your permissions and follow this guide. Messing with permissions is a sure-fire way to lead to all sorts of awful problems later on. On OSX, permissions can be reset by following this guide. On Linux there is no easy way to reset permissions. In general, a reinstall is the best route to go here.


If you have any global Node.js modules installed via NPM, now would be a good time to delete them. We will most likely be changing the destination of the global modules folder and will have to reinstall them anyway.

We first need to check if Node.js is already installed on your system. This can be done by running each of the commands below individually at the command line:

which node
which npm

If there is no output, great! If there is, don’t panic! Next we are going to look at how to completely remove each one in turn. Be sure to make a note of the output of each command, we will be using these shortly.

Remove existing Node.js installs

There are a multitude of ways to remove Node.js from your system, the exact details will depend upon how it was installed. If you have used Node.js version manager or a package manager, consult their respective documentation for details on how to remove Node.js.

If Node.js has been installed manually from the site, or from source, you will need to remove it manually. Luckily we have the instruction on how to do that just below.

IMPORTANT: The steps below for removing Node.js involve the use of the rm -rf command which can be read as ReMove -Recursively -Force. There is no undo for this command and you will not be warned before deletion. Be extremely careful of the directory name you enter. Check each directory name you enter, twice, before hitting return.

First up, we need to remove Node.js by using rm -rf on each of the directories listed in the which command.

sudo rm -rf /directory/of/node/npm

As well as the above, there are some other odds and ends that hang around. Each command will need to be ran in turn.

sudo rm -f /usr/local/share/man/man1/node.1
sudo rm -f /usr/local/lib/dtrace/node.d
sudo rm -rf ~/.npm
sudo rm -rf ~/.node-gyp

Before going any further, run each ‘which’ command again. If any directories still appear, go ahead and rm -rf each of those directories too.

Install Node.js via NVM

Introduced above, NVM is a Node.js/NVM version manager that targets works with most Unix-like OSes. To install NVM used to be a bit of a manual effort, thankfully there is now a script that does the hard work for you.

curl | bash

Note that this script works for both Bash and ZSH, and will pick the correct profile automagically! NVM allows you to install many versions of Node.js. To install the latest stable version of Node.js simply run:

nvm install stable

Next set stable as the default version of Node.js for any new console window created:

nvm alias default stable

You can test this by creating a new console window and typing node --version. The version should match the stable version of Node.js.

NVM quick look

We now have sudo free use of the node and npm commands for the stable version of Node.js and NPM; but what if we want to work with other versions, or even IO.js?

To list all versions available for install, including IO.js:

nvm ls-remote

To install a particular version:

nvm install 0.12

To list installed versions:

nvm ls

To use an installed version for the current console session:

nvm use 0.12

To set an installed version as the default for all sessions:

nvm alias default 0.12


IMPORTANT: When switching versions of Node.js it is important to run the npm build command directly after. This rebuilds any native addons for the target version of Node.js amongst other things.


By removing old Node.js installations and installing NVM we are now able to use both node and npm commands without the need for elevated permissions in the vast majority of cases. We also have the added benefit of using NVM to manage different versions of Node.js and even IO.js using our quick look section above. NVM installs all versions of Node.js and IO.js the same way so regardless of the particular version you are using it will always work, sudo free!

Want to work for nearForm? We’re hiring.


Twitter @nearform

Phone +353-1-514 3545

Check out nearForm at


By: Dean McDonnell

Dean is a self-taught software engineer who cut his teeth on .Net and Microsoft-oriented technologies. Having initially worked in the food sector and specialized in traceability, he has a wide range of experience with large systems and industrial hardware. He made the transition to Node.js quickly, having already made contributions to Johnny-Five, an IoT-focused robotics framework, as well as nearForm's nScale and Seneca. In his spare time, Dean contributes to open source software (see his GitHub profile and plays retro games.
  • mustafa motiwala

    Hi Dean.. Nice article..It was super easy to install nodejs on my VPS without the elevated permissions.. Thanks.

    • Dean McDonnell

      Hi Mustafa, that’s awesome to hear! Thanks for the comment!

  • Stav Pan Geffen

    Great tut! Thanks

    Looks like a typo right under the ‘Install Node.js via NVM’ title.

    “NVM” => “NPM”

    • Dean McDonnell

      Good spot! I’ll get this updated!

  • Soreine

    Many thanks Dean for this fine solution.

    You don’t how to deal with the few global modules that still need sudo. Just doing `sudo npm install -g {module}` doesn’t work since npm is not in root’s PATH.
    For now I am prefixing every command needing sudo with `sudo env “PATH=$PATH {command}` but it’s not convenient. Do have any simpler solution for that ?

    • Dean McDonnell

      I haven’t come across this, could you give me an example of a package that requires sudo when using this method of installation? I can have a look and then update the article with instructions on what to do. Thanks!

      • Soreine

        I don’t think it is a big issue since most packages don’t require sudo to install. My use case is for executing this command-line tool that allows to control the keyboard backlight colouring on MSI devices. To do so, it needs to access and write to USB devices and thus requires sudo. For example you would `npm install -g msi-keyboard-cli` and run ̀sudo msikeyboard -c green`. On top of that, there’s bug with one of the dependency that makes it incompatible with Node >0.12.6 so you need to do the `nvm use` dance. I am not using it anymore though, so I have no problem.

        Back to the initial topic, I’m not knowledgeable enough about security to conclude.

  • Holmes Wilson

    One issue I had (as an extreme beginner) is that I don’t think mac OS has a .bash_profile by default. So the script fails at some step that’s essential to getting nvm in the path. I had to create .bash_profile and run the install again for this to work.

  • sudeep


  • quax

    Came across this after compiling from source caused me grief. Worked like a charm! Thank you.

  • John Romani

    thank you

  • Pity Maia

    I try this, but still can’t install karma without sudo…

    The command: npm install karma

    throws an EACCES error.

    npm ERR! Linux 4.2.0-34-generic
    npm ERR! argv “/home/andre/.nvm/versions/node/v7.4.0/bin/node” “/home/andre/.nvm/versions/node/v7.4.0/bin/npm” “install” “karma”
    npm ERR! node v7.4.0
    npm ERR! npm v4.0.5
    npm ERR! path ../mime/cli.js
    npm ERR! code EACCES
    npm ERR! errno -13
    npm ERR! syscall symlink

    npm ERR! Error: EACCES: permission denied, symlink ‘../mime/cli.js’ -> ‘/home/andre/py/divvy/node_modules/.bin/mime’
    npm ERR! { Error: EACCES: permission denied, symlink ‘../mime/cli.js’ -> ‘/home/andre/py/divvy/node_modules/.bin/mime’
    npm ERR! errno: -13,
    npm ERR! code: ‘EACCES’,
    npm ERR! syscall: ‘symlink’,
    npm ERR! path: ‘../mime/cli.js’,
    npm ERR! dest: ‘/home/andre/py/divvy/node_modules/.bin/mime’ }
    npm ERR!
    npm ERR! Please try running this command again as root/Administrator.

    npm ERR! Please include the following file with any support request:
    npm ERR! /home/andre/py/divvy/npm-debug.log