read

The Node.js project and its community have undergone major changes in recent years, among which is the forking of the project (and perhaps the community itself). This situation causes confusion for newcomers, who find themselves with compatibility issues and difficulties in setting up a working and up-to-date environment.

In this post, I show how to get the recent version of Node.js on a Linux OS in an easy way.

What is Node.JS?

Node.js is an event-driven I/O server-side (backend) JavaScript environment based on Chrome’s V8 JavaScript engine. It is considered a very popular backend environment, and it uses NPM, the best package manager in my opinion (however, we’ll leave the discussion on what makes NPM so good for a different post).

At some point, Node.js was forked into two projects: Node.js and IO.js, causing compatibility issues and disputes in the community. While recently, these projects have been merged back with Node v4.x.

However, not everyone have caught on with the recent changes (which at some point should be a thing of the past). My OS of choice, Ubuntu, only supports Node.js 0.12.7 through the official repositories. Where version 0.12.7 is the latest stable version prior to the forking of the project. Thus, the best way to get the current version of Node.js on Linux is through NVM.

Removing the older version of Node

First, create a backup list of the previously globally installed NPM packages:

npm ls -gp > node_packages.txt

If you installed Node.js through apt-get, uninstall by using the following:

sudo apt-get remove nodejs

Additionally, your home folder may contain a .npm folder that can be removed (since NVM will use its own path for NPM packages).

Installing Node.js with NVM

Grab the latest copy of NVM (you may need to sudo apt-get install curl first):

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

Update NVM to latest version:

cd ~/.nvm
git pull origin master

Then restart your terminal window, and install the latest Node version:

nvm install stable

or if you want a specific version (e.g., version 4.1.1):

nvm install 4.1.1

And tell NVM which version of Node you want to use (when a specific version is needed):

nvm use 4.1.1

Define a default version, so that you don’t have to pick a Node version each time you start your terminal. This will activate the specified version each time the terminal starts, and it will do it silently.

nvm alias default stable

And lastly, check the installed version of Node:

node --version

When updating to a newer version:

nvm ls
nvm install stable
nvm ls
nvm uninstall 4.1.1

If later on you encounter errors with installing packages globaly, make sure your NODE_PATH is defined. You can do so by adding the following to ~/.bashrc or ~/.zshrc:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

export NODE_PATH=$NODE_PATH:/home/alexeyza/.nvm/versions/node/v4.1.1/lib/node_modules

If there are any issues loading the selected node version, make sure the above lines are at the bottom part of ~/.bashrc (the order matters).


The above steps are mostly based on a Stack Overflow answer and the NVM docs, with minor changes to make it more sustainable for future updates. If you prefer to install a system-wide Node.js with NVM, please follow this guide.

Blog Logo

Alexey Zagalsky


Published

Image

Alexey Zagalsky

Software Engineering Researcher

Back to Overview