Introduction
Node.js is a powerful JavaScript platform used for server-side programming, enabling developers to quickly build network applications. By utilizing JavaScript on both the front-end and back-end, Node.js offers a consistent development environment. Despite its popularity, installing Node.js on Ubuntu or Debian can still be a somewhat manual process. This guide will walk you through three methods to install Node.js on an Ubuntu/Debian server.
1. Distro-Stable Version
Ubuntu/Debian's default repositories include a version of Node.js that provides a stable and consistent experience across servers, although it may not be the latest release. To install this version, use the following command:
sudo apt-get install nodejs
You will also likely want to install npm, the Node.js package manager, to handle modules and packages:
sudo apt-get install npm
2. PPA (Personal Package Archive)
For a more recent version of Node.js, you can use a PPA maintained by NodeSource. This option generally offers more up-to-date versions than those in the official Ubuntu repositories.
First, install the PPA by running the following command:
curl -sL https://deb.nodesource.com/setup | sudo bash -
Note: If you encounter an error related to curl
, install it with:
sudo apt-get install curl -y
After adding the PPA, install Node.js using:
sudo apt-get install nodejs
3. NVM (Node.js Version Manager)
NVM allows you to install and manage multiple versions of Node.js, providing greater control over your development environment. This tool is particularly useful if your applications require specific Node.js versions.
First, install the required packages:
sudo apt-get install build-essential libssl-dev
Next, download and run the NVM installation script from GitHub:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash
This script will install NVM in your home directory and add necessary lines to your ~/.profile
file. To start using NVM, either log out and back in, or source your profile:
source ~/.profile
You can now install different Node.js versions. To see available versions, use:
nvm ls-remote
For example, to install version 6.2.0:
nvm install 6.2.0
Switch to the newly installed version with:
nvm use 6.2.0
You can verify the installed Node.js version by typing:
node -v
Conclusion
These three methods provide flexibility in installing Node.js on Ubuntu/Debian, whether you need a stable, up-to-date, or multiple versions for different projects. After installation, confirm that Node.js is correctly installed by checking the version with node -v
.