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
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:
sudo required):curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
source ~/.bashrc
nvm install 7
npm install -g npm # update npm
Now you can install -g without sudo and everything goes into ~/.nvm/
Node.js v6 (current LTS as of May 2017):
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs
Node.js v7:
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs
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.
$HOME directory:$ npm install npm -g
/home//.npm-global/bin/npm -> /home//.npm-global/lib/node_modules/npm/bin/npm-cli.js
/home//.npm-global/lib
└─┬ 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.