Skip to content

Instantly share code, notes, and snippets.

@isaacs
Last active March 8, 2024 02:11
Show Gist options
  • Save isaacs/579814 to your computer and use it in GitHub Desktop.
Save isaacs/579814 to your computer and use it in GitHub Desktop.
Use one of these techniques to install node and npm without having to sudo. Discussed in more detail at http://joyeur.com/2010/12/10/installing-node-and-npm/ Note: npm >=0.3 is *safer* when using sudo.
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install # ok, fine, this step probably takes more than 30 seconds...
curl https://www.npmjs.org/install.sh | sh
# this way is best if you want to stay up to date
# or submit patches to node or npm
mkdir ~/local
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
# could also fork, and then clone your own fork instead of the official one
git clone git://github.com/joyent/node.git
cd node
./configure --prefix=~/local
make install
cd ..
git clone git://github.com/isaacs/npm.git
cd npm
make install # or `make link` for bleeding edge
# take ownership of the folders that npm/node use
# please don't do this if you don't know what it does!
sudo mkdir -p /usr/local/{share/man,bin,lib/node,include/node}
sudo chown -R $USER /usr/local/{share/man,bin,lib/node,include/node}
# now just a pretty vanilla node install
# let it use the default paths, but don't use sudo, since there's no need
mkdir node-install
curl http://nodejs.org/dist/node-v0.4.3.tar.gz | tar -xzf - -C node-install
cd node-install/*
./configure
make install
# now the npm easy-install
curl https://www.npmjs.org/install.sh | sh
# use nave, but without a subshell
# note that we're doing "usemain" instead of "use"
mkdir ~/.nave
cd ~/.nave
wget http://github.com/isaacs/nave/raw/master/nave.sh
sudo ln -s $PWD/nave.sh /usr/local/bin/nave
# take ownership
# please don't do this if you don't know what it does!
sudo mkdir -p /usr/local/{share/man,bin,lib/node,include/node}
sudo chown -R $USER /usr/local/{share/man,bin,lib/node,include/node}
# install the latest stable nodejs in the "main" root.
nave usemain stable
curl https://www.npmjs.org/install.sh | sh
# this way is really handy if you want to test things
# in different versions of node and use stable release
# versions of things.
# make a folder where you want to keep this stuff.
mkdir ~/.nave
cd ~/.nave
wget http://github.com/isaacs/nave/raw/master/nave.sh
sudo ln -s $PWD/nave.sh /usr/local/bin/nave
# now you can forget about that folder.
# you never have to go back in there.
# to use a version of node in a virtual environment
nave use 0.4.8
# to install npm in that virtualenv
curl https://www.npmjs.org/install.sh | sh
# do stuff...
npm install whatever etc
# return to non-nave-land
exit
# use a different version of node..
nave use 0.4.6
# etc...
# using Homebrew
# Note: `brew install npm` has problems, as of 2010-12-30.
# hopefully it will eventually be good and happy.
# As of npm@0.2.13, however, this is an option
PREFIX=$(brew --prefix)
# take ownership
# this will also let homebrew work without using sudo
# please don't do this if you don't know what it does!
sudo mkdir -p $PREFIX/{share/man,bin,lib/node,include/node}
sudo chown -R $USER $PREFIX/{share/man,bin,lib/node,include/node}
brew install node
# now install npm
# prefix will default to $(brew --prefix)
curl https://www.npmjs.org/install.sh | sh
echo 'export PATH=$HOME/.local/bin:$PATH' >> ~/.bashrc
echo 'export npm_config_userconfig=$HOME/.config/npmrc' >> ~/.bashrc
. ~/.bashrc
mkdir ~/.local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/.local
make install
curl https://www.npmjs.org/install.sh | sh
# install node wherever.
# use sudo even, it doesn't matter
# we're telling npm to install in a different place.
echo prefix = ~/local >> ~/.npmrc
curl https://www.npmjs.org/install.sh | sh
@rogerramosme
Copy link

First option worket fine to me <3

@sethbergman
Copy link

curl -L https://git.io/n-install | bash

This is by far the simplest way to get started with both n and Node.js - even if you're looking to install only the latest stable Node.js version, with no (immediate) plans to install multiple versions.

n is installed as follows:

  • The installation target is a dedicated directory, which defaults to ~/n and can be overridden with environment variable N_PREFIX; n itself as well as the active Node.js version are placed there.
    • When overriding, it is advisable to choose a user location - typically, a subfolder of ~ (at any level) - so as to avoid the need to use sudo for installation of global npm packages.
    • Either way, the target directory must either not exist yet or be empty.
    • Using a dedicated directory to hold both n and the Node.js versions greatly simplifies later uninstallation.
  • If your shell is bash, ksh, or zsh, the relevant shell initialization file is modified:
    • Environment variable N_PREFIX is defined to point to the installation directory.
    • Directory $N_PREFIX/bin is appended to the $PATH, unless already present.
    • For other shells, these modification must be performed manually; instructions are provided during installation.
    • You can also explicitly suppress modification with the -n option.
  • By default, the latest stable Node.js version is installed; you can suppress that or even specify multiple Node.js versions to install.
  • Note that any preexisting n, Node.js installation must be removed before using this installation method.
  • All installation prerequisites are met by default on OSX and some Linux distros; notably, git and curl must be present - see Installing n for details.
  • After installation, be sure to open a new terminal tab or window or reload your shell initialization file before attempting to use n / Node.js - see

See examples below, and Installing n for prerequisites and installation options.

Examples

See Installation options for details.

  • Installation with confirmation prompt to default location $HOME/n and installation of the latest stable Node.js version:
curl -L https://git.io/n-install | bash
  • Automated installation to default location $HOME/n and installation of the latest stable Node.js version:
curl -L https://git.io/n-install | bash -s -- -y
  • Automated, quiet installation to default location $HOME/n and installation of the latest stable Node.js version; no status information
    is displayed:
curl -sL https://git.io/n-install | bash -s -- -q
  • Automated installation to the default location, with subsequent installation of the latest LTS
    (Long Term Support) version, and the latest 0.10.x release:
curl -L https://git.io/n-install | bash -s -- -y lts 0.10
  • Automated installation to custom location ~/util/n, with subsequent installation of the latest stable Node.js version:
curl -L https://git.io/n-install | N_PREFIX=~/util/n bash -s -- -y

Installing n

Supported platforms and prerequisites

Among the platforms supported by n, any Unix-like platform with the following is supported:

source

@missxu
Copy link

missxu commented Dec 21, 2017

This is awesome - exactly what I was looking for. I used option one ('cause I'm lazy and wanted to get on to the hacking) - for those on OSX, install XCode first. Also, I ended up getting a 301 Moved Permanently error but I just did subbed in:

curl -L https://npmjs.org/install.sh | sh

and now it looks like I'm gtg.

@ng-model
Copy link

ng-model commented Jan 12, 2018

@isaacs the second one works, if your macbook/iMAc doesn't allow the git clone just download the zip of node/npm and you can do the remaining from the downloads itself. It works, Thanks.

@wyattbiker
Copy link

I am on RHEL 7. What does the final line in the 30 seconds procedure do: curl https://www.npmjs.org/install.sh | sh

I keep getting registry.npmjs.org error and fails because I am behind a proxy and I would have to fix it. Do I need it?
It seems my node --version and npm --version shows shows the latest version anyway when I run them.

@testLit
Copy link

testLit commented Feb 13, 2018

I am using CentOS 7 and ran first script. I ran the last script as:

curl -k -L https://npmjs.org/install.sh | sh

when i tried checking the version says: no such file or directory.

@toxdes
Copy link

toxdes commented Apr 2, 2018

Well, it took longer than 5 minutes for me to install with step 1.
I don't know why people don't use ...
sudo apt-get install nodejs-legacy npm
Edit:: Oh I tried. It installs node 3.X in Ubuntu 16.04.

Also, if you quickly want to install (as I did):
Step 1:
curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -(latest)
Now, install it

Step 2:
sudo apt-get install -y nodejs

To install other versions of node, follow this guide.

@sethbergman
Copy link

It looks like one person used the method I mentioned earlier in this thread. I'll post this again because problems need solutions. 🚀

n-install

Make sure you follow the instructions if you already have node and npm installed.

@VasilisKosmas
Copy link

VasilisKosmas commented May 30, 2018

How do I uninstall if I installed with the 1st way?

node-and-npm-in-30-seconds.sh
CentOS 7

@ppsimatikas
Copy link

@ms609
Copy link

ms609 commented Sep 19, 2018

And to update @eltrhn 's comment, the npm github repository link should now read git://github.com/npm/cli.git
cd npm will thus also need changing to cd cli.

@ilndinesh
Copy link

Thanks you for compiling this, it helped!

@dreaddymck
Copy link

yet-another-option.sh needs the -L option for curl now.

echo prefix = ~/local >> ~/.npmrc
curl -L https://www.npmjs.org/install.sh | s

@ismarsantos
Copy link

@dreaddymck The correct command would be ...
curl -L https://www.npmjs.org/install.sh | sh

@Lathanao
Copy link

Lathanao commented May 15, 2019

Method one works fine on Debian Stretch. Really helpful !!!

After I got PhpStorm problem : Failed to run File Watcher 'Sass'. The watcher has been disabled.
So I needed to do :
ln -s /home/yourname/local/bin/node /usr/bin/node

@cjm91
Copy link

cjm91 commented Feb 26, 2020

Still working @sethbergman - cheers!

@mtmhill
Copy link

mtmhill commented Mar 4, 2020

Any suggestions, followed method 1 and all successful until the end of the make process: ./deps/zlib/deflate.c:54:10: fatal error: 'contrib/optimizations/insert_string.h' file not found. Any suggestions? Had been running for 30+ mins prior to the error.

@shauryamehta96
Copy link

Below are the simple steps to proceed with the installation

Open Terminal
Run command to install nodejs : sudo apt install nodejs
Run command to verify installation by checking version: node -v or node –version
Run command to install npm: sudo apt install npm
Run command to verify installation of npm: npm -v or npm –version

For reference: https://youtu.be/DGjfw4y0nTI

@ayush1221
Copy link

I am using the first method for installation in a ruby docker image ,and it finally throws error.Tried many approach doesn’t seems to work,if someone can guide me how to install it inside a ruby docker image,thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment