Install NPM into home directory with distribution nodejs package (Ubuntu)


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.

3/8/2019 12:13:41 AM

Accepted Answer

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:

 $ NPM_PACKAGES="$HOME/.npm-packages"
 $ mkdir -p "$NPM_PACKAGES"

Set NPM to use this directory for its global package installs:

 $ echo "prefix = $NPM_PACKAGES" >> ~/.npmrc

Configure your PATH and MANPATH to see commands in your $NPM_PACKAGES prefix by adding the following to your .zshrc/.bashrc:

# NPM packages in homedir

# Tell our environment about user-installed node tools
# Unset manpath so we can inherit from /etc/manpath via the `manpath` command
unset MANPATH  # delete if you already modified MANPATH elsewhere in your configuration

# Tell Node about these packages

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 your PATH.

Just use npm install -g as you would normally:

[justjake@marathon:~] $ npm install -g coffee-script
... (npm downloads stuff) ...
/home/justjake/.npm-packages/bin/coffee -> /home/justjake/.npm-packages/lib/node_modules/coffee-script/bin/coffee
/home/justjake/.npm-packages/bin/cake -> /home/justjake/.npm-packages/lib/node_modules/coffee-script/bin/cake
coffee-script@1.3.3 /home/justjake/.npm-packages/lib/node_modules/coffee-script

[justjake@marathon:~] $ which coffee
6/2/2014 7:44:30 PM

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):

curl -o- | bash
source ~/.bashrc
nvm install 7
npm install -g npm  # update npm

Now you can install -g without sudo 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):

    curl -sL | sudo -E bash -
    sudo apt-get install -y nodejs
  • Node.js v7:

    curl -sL | sudo -E bash -
    sudo apt-get install -y nodejs

Change npm's default directory to a local one:

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH="$HOME/.npm-global/bin:$PATH"  # ← put this line in .bashrc
source ~/.bashrc  # if you only updated .bashrc

Alternatively replace .npm-global by the directory of your choice.

Update npm and check it is installed in your $HOME directory:

$ npm install npm -g
/home/<username>/.npm-global/bin/npm -> /home/<username>/.npm-global/lib/node_modules/npm/bin/npm-cli.js
└─┬ npm@3.10.6 
  ├─┬ glob@7.0.5 
  │ └── minimatch@3.0.2 
  ├── npm-user-validate@0.1.5 
  └── rimraf@2.5.3 

Now you can install -g without sudo and without messing with your system files.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow