I'd like to use the distribution Node.js packages (or the chris-lea ppa for more recent releases) but install NPM to my home directory.
This may seem picky, but it's a pretty idiomatic way for polyglot/github-using developers to setup language runtime/library environments under Linux: distro packages for the runtime, 3rd-party libraries in per-user environment (see virtualenv, RVM - RVM will also build Ruby for you if you want). If necessary I will build node locally but it's a PITA since Node is becoming an incidental development requirement for lots of projects.
- Instructions for installing node+npm to home directory
- Another method for installing node+npm to home
The second link seems like it might work with distribution nodejs packages, but it seems pretty sketchy.
Update this paste sums up the second link above, but doesn't work for me (it seems like npm ignores ~/.npmrc, at least during install).
Jake's answer was posted in 2012 and while useful it references Chris Lea's Node.js PPAs who are no longer updated since march 2015.
Here's the steps I use to install Node.js and npm in my home directory:
Install Node.js with nvm (no
sudo
required):Now you can
install -g
withoutsudo
and everything goes into~/.nvm/
Or install Node.js without nvm (official instructions):
Install Node.js
Node.js v6 (current LTS as of May 2017):
Node.js v7:
Change npm's default directory to a local one:
Alternatively replace
.npm-global
by the directory of your choice.Update npm and check it is installed in your
$HOME
directory:Now you can
install -g
withoutsudo
and without messing with your system files.As stated already here and here
The solution posted by Just Jake is great. However, due to a bug with npm > 1.4.10, it may not work as expected. (See this and this)
While the bug is solved, you can downgrade to npm 1.4.10 by following this steps:
sudo npm install -g npm@1.4.10
npm --version
)To expand on the answer provided by Just Jake and user1533401: I am unable to downgrade as I use shared hosting and node is installed in a system directory. This is also why I have change the directory where npm installs global scripts if I want it to do that. For those in the same boat, here is a another temporary fix I found works:
npm install -g --prefix=$(npm config get prefix) <package>
The bug is that npm doesn't read your per-user config file, but specifying it every time you install a global script fixes that. Found here.
NPM will install local packages into your projects already, but I still like to keep the system away from my operating system's files. Here's how I suggest compartmentalizing Nodejs packages:
Install Nodejs and NPM via the chris-lea PPA. Then I set up a package root in my homedir to hold the Node "global" packages:
Set NPM to use this directory for its global package installs:
Configure your PATH and MANPATH to see commands in your $NPM_PACKAGES prefix by adding the following to your .zshrc/.bashrc:
Now when you do an
npm install -g
, NPM will install the libraries into~/.npm-packages/lib/node_modules
, and link executable tools into~/.npm-packages/bin
, which is in yourPATH
.Just use
npm install -g
as you would normally:I used @just-jake solution for some time and found that nvm is easier to setup. Also it's much powerful solution that allows to install and use different versions of
nodejs
.On Ubuntu 14.04 or 16.04:
Install prerequisite packages for building
nodejs
:Install nvm:
In case newer version of nvm will be available you can find actual installation command on nvm site.
nvm
installer will add bootstrap script to~/.bashrc
, so you need either to reopen terminal to run it, or to do:Now you can install any
nodejs
version you like, switch between them etc.Use
nvm ls-remote
to list availablenodejs
versions.To install, for example,
nodejs
v4.2.4 do: