Introduction
Node.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 three ways to get started with Node.js on an Ubuntu/Debian server.
Starting from updating server packages:
sudo apt update
sudo apt upgrade
1. Installing Distro-Stable version
Ubuntu/Debian includes a version of Node.js in its default repositories, allowing for a consistent experience across multiple servers. Usually, it will not be the latest version, but it should be pretty stable.
β
In order to get this version, we just have to use the apt package manager:
sudo apt install nodejs
To check if the installation was successful:
node -v
Output:
If the package in the repositories suits your needs, that's all you need to do. In most cases, you'll also want to install npm, which is the Node.js package manager. You can do this by typing:
sudo apt install npm
This allows you to install modules and packages to use with Node.js.
2. PPA (Personal Package Archive)
An alternative to get 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.
PPA should be installed from the root directory:
cd ~
curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh
If you want to check the content of the downloaded script:
nano /tmp/nodesource_setup.sh
Output:
To run the script:
sudo bash /tmp/nodesource_setup.sh
The PPA will be added to your configuration, and the local package cache will be updated automatically. The output:
Now, go ahead with the NodeJS installation:
sudo apt install nodejs
To check NodeJS version, use:
node -v
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 more easily. It will provide you with on-demand access to the latest versions of Node.js, while also allowing 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 install build-essential libssl-dev
Now, we should run the installation script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
If you want to check that script before running it, you can use the same command without bash:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh
If you're okay with the changes he makes, run the command with bash.
This will install the nvm script to your user account.
To use it, you must first source your .bashrc file:
source ~/.bashrc
Now, we can check which versions of NodeJS are available:
nvm ls-remote
Output:
If running nvm ls-remote
the error Command 'vm' not found
appears, use:
apt install mgetty-voice
Now, we can install the required versions of NodeJS:
nvm install v21.0.0
To check the current version:
nvm list
or
node -v
Output:
Conclusion
You're done! Regardless of the installation method you used, run nvm list
or node -v
to verify that the build/install worked correctly.