A bit of context
You can install node in Ubuntu with the
apt-get manager (or
apt from 16.04 on), but it will give you an old version of node . Also, it will install node in a directory that needs root permissions. This means that all your future npm packages will be installed as root.
Node packages are scripts made by who knows whom, running freely in your system. Because anybody can create and publish a node module! That's why Isaac Z. Schlueter, the founder and CEO of npm, has been discouraging people from installing node as root for this and other reasons.
The other thing that you don't get with node (well, now you do!) is being able to install different versions of node alongside each other. Node has
npm to handle package installation and
package.json to handle package versions, but there was nothing to handle node versions. If you ever used Ruby, you've been spoiled. Ruby has several version managers to choose from (I use rbenv) as well as a convenient way to handle gem versions (mainly through bundler). So I did some research and I discovered that
nvm exists for node. It allows you to install several different versions of node and switch from one to the other depending on your project's requirements, very easily. So I decided that I had to give it a try.
If you read the README in the official repository, you will see that there are several ways you can install it, including manually. The easiest way and the one you will find in every tutorial is just using
curl and running the
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
Boom. Done. BUT: this will install nvm in your home directory, creating an
~/.nvm folder inside of which the different node versions will be installed, in a very similar way as how
rbenv works. It's a clean install, to uninstall you just have to remove that folder.
However, I didn't want to install it in my home directory, but in
/opt. The path to the home directory is kind of hardcoded in the install script, but you can jump over it if you set the
NVM_DIR variable in your enviroment before doing the installation. But then the installation process is a bit different.
Installing in a different directory
I decided to choose the manual install, which is easy and keeps you updated because it's just a git repository that you can
git pull at anytime. This is what I did:
Download code from official repo
Use a directory that you don't have to edit with root persmissions. I created an
nvm directory inside of
/opt that is owned by my user. Following the official instructions for a manual installation:
git clone email@example.com:creationix/nvm.git /opt/nvm cd nvm git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" origin`
In this repo you will find the
nvm.sh files. If you want to do the usual installation (in the home folder) just run
install.sh. If not, keep reading.
Install in another folder
I want to install nvm in the
/opt/nvm folder that I just cloned, so:
export NVM_DIR=/opt/nvm source $NVM_DIR/nvm.sh nvm
And that's it! You have installed nvm.
Having the cloned repo is also cool because it's all written in bash, so you can contribute bugfixes! and it also has a nice test suite (I didn't know there was a test framework for bash but I am nicely surprised!)
Make it permanent
You have been doing this in the current terminal window, so if you close it, the definition of
NVM_DIR will be lost.
Fix that by adding this to your
# nvm export NVM_DIR=/opt/nvm [ -s "$NVM_DIR/nvm.sh" ] &:&: . "$NVM_DIR/nvm.sh"
If you want to uninstall it, you just have to remove the
.nvm folder inside
So this section is what we are here for. We wanted to install several versions of node and be able to switch between them easily. The documentation on how to use nvm to manage node installations is in the official README.
At first, if you ask nvm for the available node packages, you will get:
$ nvm ls N/A node -> stable (-> N/A) (default) iojs -> N/A (default)
So I decided to choose the first:
$ nvm install node
This will add these three folders to the directory.
$ ls -al /opt/nvm total 20 drwxrwxr-x 5 ubuntu ubuntu 4096 Mar 17 22:07 . drwxr-xr-x 6 ubuntu ubuntu 4096 Mar 17 21:01 .. drwxrwxr-x 3 ubuntu ubuntu 4096 Mar 17 22:01 alias drwxrwxr-x 3 ubuntu ubuntu 4096 Mar 17 22:07 .cache drwxrwxr-x 3 ubuntu ubuntu 4096 Mar 17 22:07 versions ...etc
Node versions will be installed in the
/opt/nvm/versions directory. Sounds a lot like
$ which node /opt/nvm/versions/node/v7.7.3/bin/node
As you can see, we have installed the latest stable version of node at the moment of writing this post, which is v7.7.3.