How to setup n the Node.js Version Manager on Ubuntu, correctly

There are many incomplete guides about how to setup correctly the famous node.js version manager, and many others are using nvm, this guide or tutorial aims to show you how to setup n that is another node.js version manager, and for me the best one.

I assume that you have a fresh installation of Ubuntu, by default Ubuntu doesn't comes with any version of node.js, so we need to install it, doesn't matter what, the best way is using a node.js version manager.

1. Remove previous installations of Node.js

First we need to make sure that we don't have any previous installation of node.js, to check that, we'll run some commands on the terminal:

$ whereis node

and

$ whereis nodejs

The result of both commands must be just the word that we are looking for, something like this:

$ node:

and

$ nodejs:

If you get those results you can continue without problems, however if you get something like this on any command:

$ node: /usr/bin/node /usr/local/bin/node

You must to remove the previous installations, the easiest way is removing the directories where is node.js installed, to achieve that you need to run some commands.

Note: be careful when you run this commands, if you put a wrong path, it can cause unexpected results.

Remember that these paths only applies for this example, you need to replace them with your own paths obtained with the previous step.

$ sudo rm -rf /usr/bin/node*

$ sudo rm -rf /usr/local/lib/node* 

After these steps we can make sure that we don't have node.js on our system, now we can proceed with the installation itself.

2. Installing 'n' Node.js Version Manager

The easiest way to do this is with the next command:

$ curl -L https://git.io/n-install | bash

This command will install the latest version of n, so after that we have n and the latest stable version of node.js in our system, but we need to do some more steps to leave this ready to work properly.

Brief explanation

With the previous command we install n and node.js in our system but that command has installed everything in our local directory, and it has added the needed values to the PATH on the files .bash_profile or .bashrc, that is an expected result, the unexpected result is that we cannot use n or node.js as super user akka sudo, because the super user will look for the executables binaries on another location, to fix that we need to creat some symbolic links, then go for it.

3. Allowing correct execution as 'sudo'

When we try to run a command as sudo the user will try to find the binaries in this location /usr/bin/, so we need to point our symbolic links to that path as follows:

 $ sudo ln -sf /home/<your_user>/n/bin/n /usr/bin/n
 $ sudo ln -sf /home/<your_user>/n/bin/node /usr/bin/node
 $ sudo ln -sf /home/<your_user>/n/bin/npm /usr/bin/npm

You must to replace <your_user> with your user or you can get the path where is installed n using the command whereis n.

After these easy steps you have n, node.js and npm ready to use.

Now you can read the documentation of n to install the version of node.js that you need without any problem.

Ulises García

Owner and founder of pixeleate.com - passionate developer, learning new technologies every day, sharing my little knowledge.

Austin, Texas http://ulisesgarcia.me