How to Install latest Nodejs using Node Version Manager(NVM)

Node.js is here and it's here to stay thanks to the meteoric rise of Javascript. Node.js's pitch is that you can use single scripting language (Javascript) on both the front-end and server-side which alleviates the need of having to change programing environments.

Node.js is also event-driven which means the node.js server doesn't have to wait for a request to complete first before serving another one. Here's an analogy from restaurant example explaining event-driven programing.

If the fast food joint followed a traditional thread-based model, you'd order your food and wait in line until you received it. The person behind you wouldn't be able to order until your order was done. In an event-driven model, you order your food and then get out of line to wait. Everyone else is then free to order.

There are several ways of installing Node.js on Linux. You could install it from supported operating system repository. This is fast and easy but has one draw back; you can't install several versions of the software. So if for instance one package depends on Node js 4.x and you have 6.x, then you'll run into issues. That's why I use to install using Node version manager(nvm) because it allows you to manage several versions of node much like Ruby on Rail's rvm or python's pyenv.

Download and install nvm

 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash     

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7766  100  7766    0     0   3474      0  0:00:02  0:00:02 --:--:--  3476  
=> Downloading nvm from git to '/home/oquidave/.nvm'
=> Cloning into '/home/oquidave/.nvm'...
remote: Counting objects: 6504, done.  
remote: Compressing objects: 100% (7/7), done.  
remote: Total 6504 (delta 3), reused 7 (delta 2), pack-reused 6495  
Receiving objects: 100% (6504/6504), 1.97 MiB | 222.00 KiB/s, done.  
Resolving deltas: 100% (4029/4029), done.  
Checking connectivity... done.  
* (HEAD detached at v0.31.0)
  master

=> Appending source string to /home/oquidave/.zshrc
=> Close and reopen your terminal to start using nvm

Verify nvm has been installed
It should output "nvm". If it doesn't close and re-open your terminal.

command -v nvm  
nvm  

Download latest version of nodejs
At the time of this writing, it is 6.11.2

nvm install 6.11.2                                                                              ~/workspace/bash  
Downloading https://nodejs.org/dist/v6.11.2/node-v6.11.2-linux-x64.tar.xz...  
######################################################################## 100.0%
Now using node v6.11.2 (npm v3.10.10)  
Creating default alias: default -> 6.11.2 (-> v6.11.2)  

List available node js versions

nvm ls                                                                                         ~/workspace/bash  
->      v6.11.2
default -> 6.11.2 (-> v6.11.2)  
node -> stable (-> v6.11.2) (default)  
stable -> 6.11 (-> v6.11.2) (default)  
iojs -> N/A (default)  

show current node js version in use

nvm current                                                                                     ~/workspace/bash  
v6.11.2  

set default version Use format nvm alias

nvm alias default 6.11.2                                                                   ~/workspace/bash 127 ↵  
default -> 6.11.2 (-> v6.11.2)  

*show path to installed node version *

nvm which 6.11.2                                                                                ~/workspace/bash  
/home/oquidave/.nvm/versions/node/v6.11.2/bin/node

As you can see, the path is specific to a certain user's home directory. Node js is not installed globally for everyone. If you want to install node globally, you can use the following command;

n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local  

David Okwii

David Okwii is a Systems Engineer who currently works with Uganda's country code top-level domain registry.

Kampala Uganda http://www.davidokwii.com

Subscribe to oquidave@geek:~ #

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!