Skip to content

Instantly share code, notes, and snippets.

@thejmazz
Last active March 30, 2026 13:40
Show Gist options
  • Select an option

  • Save thejmazz/72456e3f29cf0bf56d4a to your computer and use it in GitHub Desktop.

Select an option

Save thejmazz/72456e3f29cf0bf56d4a to your computer and use it in GitHub Desktop.
Install Node and npm on Linux/OS X/Windows w/o sudo

Install Node and npm

Can also just do: nvm

TLDR; Download install-node.sh, modify the settings, make it executable (chmod u+x install-node.sh), and run it (./install-node.sh). Unless your on Windows, then you need to follow the full instructions.

These instructions are composed of what I've learned from other install instructions posted online, as well as my experience getting Node/npm installed on a variety of machines.If something does not work, please post a comment below.

First off, cheers to isaacs, a member of npm and joyent for publishing this gist: node-and-npm-in-30-seconds.sh. As well, thanks to sindresorhus for publishing npm-global-without-sudo.md. This gist will never get as many stars as those above, but it is essentially a combination of those instructions plus my own instructions for Windows.

Linux/OS X

Feel free to install using a prebuilt installer. However, I've never done that, and don't know where it installs to, etc, so do so on your own. If you follow my method you won't have to use sudo all the time.

First, we will simply download the latest source code for Node. This includes Node and V8. As of right now, the latest version is 5.x. However, you may want to opt for LTS 4.x if you are deploying to production. We will use curl which is one of my favourites acronyms (because it is recursive for curl URL Request Library). We can pipe this download right into tar and extract it as we download:

curl http://nodejs.org/dist/node-latest.tar.gz | tar xz

Now we need to decide where we want to install node to. By default, it will go to /usr/local. This will force you to use sudo to (un)install packages with npm. That's kind of annoying, and theres no reason to use sudo if we really don't need to. You can change ownership of the relevant directories (see here), but chown -R /usr/local/{share/man,bin} is generally not recommended. The simplest option is ~/node. If you want to keep your home folder clean, you can install node to /usr/local/node. Then you will have to sudo chown `whoami` /usr/local/node.

If you are on a server and want to give different users the same global node and npm packages,

# Assumes Linux (Ubuntu)
sudo mkdir /usr/local/node

# Make node user group
sudo groupadd node

# Make a new user and add them to group
sudo useradd -G node foo
# Add an existing user to the group
sudo usermod -a -G node bar
# Note: users need to logout/in for changes to take effect

# Change group ownership of node folder
sudo chgrp node /usr/local/node

Once we've decided where to install, we can add the appropriate directories to our environment variables. Add the following to your ~/.bashrc (or ~/.profile on OS X, .profile is loaded before .bashrc):

export NODE_DIR=/usr/local/node # or wherever
export NPM_PACKAGES="$NODE_DIR/npm-packages"
export NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
export PATH="$PATH:$NPM_PACKAGES/bin:$NODE_DIR/bin"

# Unset manpath so we can inherit from /etc/manpath via the `manpath` command
unset MANPATH # delete if you already modified MANPATH elsewhere in your config
export MANPATH="$NPM_PACKAGES/share/man:$MANPATH"

and source ~/{.bashrc|.profile} (or restart your terminal). Make sure it worked by testing one of them with echo $NODE_DIR for example. Then we can run (use sudo where necessary)

mdkir $NODE_DIR
mdkir $NPM_PACKAGES

# Change ownership, group, etc as described above

# Let npm runtime configuration know where to look
echo prefix=$NPM_PACKAGES >> ~/.npmrc

Great, now that we have a proper permissions set up, we can go ahead and configure and install Node, cd into the node-vx.x.x folder you downloaded and

./configure --prefix=$NODE_DIR
# This will take a while, 20-30 mins or so. Building V8!
make install

Testing Install

Make sure you can get the version numbers for node and npm:

node -v
npm -v

You will notice the npm version is not the most up to date. This is because node ships with npm, but npm still releases updates on its own. Run

which npm # $NODE_DIR/bin/npm
npm install -g npm
which npm # $NPM_PACKAGES/bin/npm

When npm installs packages globally they will go into our $NPM_PACKAGES directory. It's very important $NPM_PACKAGES/bin comes before $NODE_DIR/bin in our path since otherwise when we update npm the path will still catch the old version first. I've wasted hours on this when I first started using Node and the terminal - couldn't figure out why updating wasn't working - ended up being the path.

Windows

First, download git. This will include git bash which is way nicer than the windows terminal (is resizable, has colours) and lets you use *Nix commands such as ls and mkdir. Even though Windows has its alternatives, tutorials online will almost always use bash syntax. When installing git, choose "Use git from the Windows Command Prompt". All other options can be left to default.

Grab the Windows Installer (.msi). I installed to C:\nodejs\ instead of the program files path to make things easier if we need to modify the PATH variable later. After installing, open up the Git Bash and run

node -v
npm -v
which npm # /c/users/Julian/AppData/Roaming/npm/npm

As of writing this, node v4.2.2 came with npm 2.1.18. The current npm is something like 3.5.x. So we're already a major version behind. Thats bad. So, lets update it:

npm install -g npm
# C:\nodejs\npm -> C:\nodejs\node_modules\npm\bin\npm-cli.js
npm -v

Hmm, thats weird, I'm still getting 2.1.18? Try closing/reoping bash. Still the old version? We need to repair the PATH.

  1. Right click Windows icon
  2. System
  3. Advanced system settings
  4. Environment Variables...

There will be one section for user variables and one for system variables. In Path in user variables, at the very end of mine is C:\Users\Julian\AppData\Roaming\npm. This is what which npm was giving us earlier. Inside my system Path variable, C:\Users\Julian\AppData\Roaming\npm comes before C:\nodejs. So, even though npm@3.5.0 is installed, the older 2.1.18 resides in a directory which is picked up earlier by the Path variable. Modify the system Path so that C:\nodejs is first (directories are semi-colon seperated). Close/open Git Bash. Now npm -v returns 3.5.0 and we are good to go.

#!/bin/bash
# Predefined links and files. Note: LTS may not be latest LTS
export NODE_STABLE=http://nodejs.org/dist/node-latest.tar.gz
export NODE_LTS=https://nodejs.org/dist/latest-v4.x/node-v4.2.2.tar.gz
export RC_BASH=~/.bashrc
export RC_PROFILE=~/.profile
# Settings
export NODE_VER=$NODE_STABLE
export RC_FILE=$RC_BASH
export UNSET_MANPATH=true # false if MANPATH already modifed elsewhere
export USE_GROUP=false # change group ownership
export GROUP_NAME=node # sudo groupadd $GROUP_NAME beforehand
# Download Node into current directory and extract
mkdir node-install
cd node-install
curl $NODE_VER | tar xz --strip-components=1
# Export env variables and add to PATH
echo 'export NODE_DIR=/usr/local/node' >> $RC_FILE
echo 'export NPM_PACKAGES="$NODE_DIR/npm-packages"' >> $RC_FILE
echo 'export NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"' >> $RC_FILE
echo 'export PATH="$PATH:$NPM_PACKAGES/bin:$NODE_DIR/bin"' >> $RC_FILE
# MANPATH
if [ "$UNSET_MANPATH" == "true" ]; then
echo 'unset MANPATH' >> $RC_FILE
fi
echo 'export MANPATH="$NPM_PACKAGES/share/man:$MANPATH"' >> $RC_FILE
# Source
source $RC_FILE
# Make directories for Node and npm global packages
sudo mdkir $NODE_DIR
sudo mdkir $NPM_PACKAGES
# Take ownership
if [ "$USE_GROUP" == "false" ]; then
sudo chown `whoami` $NODE_DIR
else
sudo chgrp $GROUP_NAME $NODE_DIR
fi
# Configure and install
./configure --prefix=$NODE_DIR
# This will take a while, 20-30 mins or so. Building V8!
make install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment