IntroductionNode.js is a Javascript platform for server-side programming that allows users to build network applications quickly. By leveraging Javascript on both the front-end and the back-end, development can be more consistent and be designed within the same system.
For as popular as this JavaScript run-time has become, you might be surprised to find out that it actually still isn't very easy to install Node.js on Ubuntu and other Linux distributions. It's still more of a manual process than it should be.
In this guide, we'll show you 3 ways how to get started with Node.js on an Ubuntu/Debian server.
Note: Tutorial on how to set up a Node.js application for production is on the way and will be released in few weeks.
1. Distro-Stable VersionUbuntu/Debian contains a version of Node.js in its default repositories that can be used to easily provide a consistent experience across multiple servers. Usually it will not be the latest version, but it should be quite stable.
In order to get this version, we just have to use the
apt package manager:
sudo apt-get install nodejs
If the package in the repositories suits your needs, this is all that you need to do to get set up with Node.js. In most cases, you'll also want to also install npm, which is the Node.js package manager. You can do this by typing:
sudo apt-get install npm
This will allow you to easily install modules and packages to use with Node.js.
2. PPA (Personal Package Archive) An alternative that can get you a more recent version of Node.js is to add a PPA maintained by NodeSource. This will probably have more up-to-date versions of Node.js than the official Ubuntu repositories.
First, you need to install the PPA in order to get access to its contents.
curl -sL https://deb.nodesource.com/setup | sudo bash -
Note: If you got error "
-bash: curl: command not found" it means you do not have
curl installed on your server. You can install it with command:
sudo apt-get install curl -y
The PPA will be added to your configuration and your local package cache will be updated automatically. After running the setup script from nodesource, you can install the Node.js package in the same way that you did above:
sudo apt-get install nodejs
3. NVM (Node.js Version Manager)Using NVM, you can install multiple, self-contained versions of Node.js which will allow you to control your environment easier. It will give you on-demand access to the newest versions of Node.js, but will also allow you to target previous releases that your app may depend on.
First of all we need to get the software packaged from Ubuntu/Debian repositories:
sudo apt-get install build-essential libssl-dev
Once the prerequisite packages are installed, you can pull down the
NVM installation script from the project's GitHub page:
https://github.com/creationix/nvmExecute command:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash
This will download the script and run it. It will install the software into a subdirectory of your home directory at
/.nvm. It will also add the necessary lines to your
/.profile file to use the file.
To gain access to the nvm functionality, you'll need to log out and log back in again, or you can source the
/.profile file so that your current session knows about the changes:
source ~/.profile
Now that you have nvm installed, you can install isolated Node.js versions.
To find out the versions of Node.js that are available for installation, you can type:
nvm ls-remote
As you can see, the newest version at the time of this writing is v6.2.0. You can install that by typing:
nvm install 6.2.0
Usually, nvm will switch to use the most recently installed version. You can explicitly tell nvm to use the version we just downloaded by typing:
nvm use 6.2.0
When you install Node.js using nvm, the executable is called node. You can see the version currently being used by the shell by typing:
node -v
ConclusionAnd that's it, you're done! Regardless of whatever installation method you used, you should run
node -v to verify the build/install worked correctly.