First, make sure you have a C++ compiler. Open the terminal and install the build-essential and libssl-dev packages if needed. By default, Ubuntu does not come with these tools — but they can be installed in the command line.
Use apt-get to install the build-essential package:
sudo apt-get install build-essential checkinstall
Employ a similar process to get libssl-dev:
sudo apt-get install libssl-dev
You can install and update Node Version Manager, or nvm, by using cURL
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash
You will be asked to close and reopen the terminal. To verify that nvm has been successfully installed after you reopen the terminal, use:
command -v nvm
That command will output nvm if the installation worked.
To download, compile and install the latest version of Node:
nvm install 5.0
In any new shell, you’ll need to tell nvm which version to use:
nvm use 5.0
To set a default Node.js version to be used in any new shell, use the alias default:
nvm alias default node
Not only does nvm allow you to run newer versions of Node.js and npm, you can install and migrate any desired versions you’d prefer. Go to the nvm GitHub repository for more information.