I just installed NodeJS & NPM on Debian Jessie using the recommended approach:
apt-get install curl curl -sL https://deb.nodesource.com/setup | bash - apt-get install -y nodejs
However it’s a pretty old version (node v0.10.38 & npm 1.4.28).
Any suggestions on the easiest way to install newer versions, e.g., currently node is v0.12.4 and npm is 2.7.4? Is installing from source my only approach?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
There is a setup script available for Node.js (see installation insctructions):
# Adapt version number to the version you want curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash - sudo apt-get install -y nodejs
A little comment: In my humble opinion, it’s a very bad idea to curl | sudo bash. You are running a script you did not check with root privileges. It’s always better to download the script, read through it, check for malicious commands, and after that, run it. But that’s just my two cents.
The installation can be achieved manually in a few steps following the manual installation procedure:
- Remove old PPA (if applicable)
- Add node repo ssh key
- Add node repo to
sources.list - update package list and install using favorite apt tool
Method 2
You can download the latest version of Node (4.2.2) from their website, instead of using the package Debian provides (0.12). This will also mean you have an updated version of npm. I have not had any problems doing this on Jessie.
Download the .tar.gz from their website and cd into that dir (the name of the file is obviously specific to my download):
$ tar -xzvf node-v4.2.2-linux-x64.tar.gz $ cd node-v4.2.2-linux-x64
If you have a look in the bin folder you will see the binaries you need for node and npm:
$ ls node-v4.2.2-linux-x64/bin node npm
Now I would rename the folder to something a bit easier to keep track of
$ mv node-v4.2.2-linux-x64 nodejs
If you are not interested in keeping node updated, then simply move this folder into one of your bin locations (I use ~/bin), and skip the next step.
If you would like to be able to easily update your node version, then move the nodejs folder somewhere you can keep track of it (~/nodejs perhaps?). Then you want to create a symlink to one of your bin locations so that the binaries can be used from your shell without writing out the full path.
Assuming you put the nodejs folder in your home directory you can now do:
$ ln -s ~/nodejs ~/bin/nodejs
Obviously, this can go to any bin location you want. I used my home directory so it is only available to me, but you could also easily do:
$ ln -s ~/nodejs /usr/local/bin/nodejs
This will create a symlink from the nodejs folder in your home directory to the bin directory (meaning that any updates to the directory in your home folder are reflected in the folder elsewhere via the symbolic link). Now you want to make sure that the bin folder containing the nodejs directory is in your $PATH environment variable, so open the ~/.profile file in your home directory. You want to add this to the bottom of that file (changing the path, if you did not use ~/bin/):
# Set the node PATH if it exists
if [ -d "$HOME/bin/nodejs/bin" ] ; then
PATH="$HOME/bin/nodejs/bin:$PATH"
fi
This will check if the directory exists, and if it does, add it to your PATH. I use zsh so I just updated a line in ~/.zshrc:
export PATH="$HOME/bin/nodejs/bin:$PATH"
Close your terminal and re-open, then type the following to check:
$ node -v v4.2.2 $ npm -v 2.14.7
By creating the sym link, it now means that in the future, you can download a new .tar.gz from the Nodejs website, extract it to ~/nodejs, and the binaries that are available to you in your $PATH environment variable are automatically updated.
Method 3
in my case, I executed the recommended shell commands:
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - sudo apt-get install -y nodejs
the problem was that bash script didn’t update my APT pinning so apt-get installed the default, old Debian package from the debian.org source and not from nodesource.com
verify this is the problem with apt-cache policy nodejs. you should see something like Candidate: 6.10.2-1nodesource1~jessie1 and not Candidate: 0.10.29~dfsg-2
to set pin priority to nodesource, add a file in /etc/apt/preferences.d/ called node or whatever with this content:
Package: nodejs Pin: release o=Node Source Pin-Priority: 1200
save and run apt-get cache update. then try apt-cache policy nodejs again. if candidate looks right, install as normal apt-get install nodejs
Method 4
You can follow the official node.js documentation to install the latest version with package manager.
Node.js v6:
curl -sL https://deb.nodesource.com/setup_6.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
Running the latter, node v7.5.0 and npm v4.1.2 was installed on my system. Both node and nodejs aliases were enabled for Node automatically.
Method 5
You can download latest Linux Binaries (.tar.gz) from official site and use it
https://stackoverflow.com/a/23084499/1197421
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0