Node.js is a powerful JavaScript runtime environment designed for server-side programming. It allows developers to build scalable backend functionality using the familiar JavaScript language. If you’re an Ubuntu 20.04 user, this guide will walk you through three different methods of installing Node.js on your server. Whether you prefer the simplicity of the default repositories, the flexibility of an alternate PPA, or the versatility of the Node Version Manager (nvm), we’ve got you covered.
Prerequisites
Before we dive into the installation process, let’s make sure you have everything you need to get started. You’ll need an Ubuntu 20.04 server already set up, along with a non-root user account that has sudo privileges. If you haven’t done this yet, you can follow the official Ubuntu 20.04 initial server setup tutorial for guidance.
Option 1: Installing Node.js with Apt from the Default Repositories
Ubuntu 20.04 includes a version of Node.js in its default repositories, which provides a consistent experience across multiple systems. However, it’s important to note that the version in the repositories may not always be the latest. At the time of writing, the default version is 10.19, but it’s no longer supported or maintained. Therefore, it’s recommended to use one of the alternative methods discussed later in this guide for production environments.
To install Node.js using apt, start by updating your local package index:
sudo apt update
Once the update is complete, you can install Node.js:
sudo apt install nodejs
To verify that Node.js is successfully installed, you can check its version:
node -v
If everything went smoothly, you should see the version number displayed, such as v10.19.0
. In most cases, you’ll also want to install npm (Node.js package manager) to manage packages and modules:
sudo apt install npm
Congratulations! You have successfully installed Node.js and npm using the default repositories on your Ubuntu 20.04 server.
Option 2: Installing Node.js with Apt Using a NodeSource PPA
If you require a specific version of Node.js that is not available in the default Ubuntu repositories, you can use a Personal Package Archive (PPA) maintained by NodeSource. NodeSource PPAs offer a wider range of Node.js versions compared to the official repositories.
To install a different version of Node.js using a NodeSource PPA, follow these steps:
- Change to your home directory using the
cd
command:
cd ~
- Use
curl
to retrieve the installation script for your desired Node.js version, replacing16.x
in the URL with your preferred version:
curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh
Refer to the NodeSource documentation for the available versions.
- Inspect the contents of the downloaded script using a text editor like
nano
:
nano /tmp/nodesource_setup.sh
Ensure that the script is safe to run, and exit the editor.
- Run the script with
sudo
to add the PPA to your configuration and update the package cache:
sudo bash /tmp/nodesource_setup.sh
- Install the Node.js package using
apt
, as we did in Option 1:
sudo apt install nodejs
- Check the installed version of Node.js:
node -v
You should see the version number of the installed Node.js, such as v16.19.0
.
The NodeSource PPA installation method allows you to have more control over the Node.js version installed on your Ubuntu 20.04 server.
Option 3: Installing Node.js with the Node Version Manager (nvm)
If you’re actively developing Node.js applications and need to switch between different Node.js versions frequently, the Node Version Manager (nvm) provides a flexible solution. With nvm, you can install and manage multiple independent versions of Node.js and their associated packages simultaneously.
To install nvm on your Ubuntu 20.04 server, follow these steps:
- Visit the nvm project’s GitHub page to get the most recent version of the installation script. Copy the
curl
command displayed on the main page:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh
- Before running the command, review the script to ensure its safety. Remove the
| bash
segment at the end of the command to prevent immediate execution:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh
- Once you’re satisfied with the script, run the command again with
| bash
appended to install nvm:
curl-o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
This will install the nvm script to your user account.
- To use nvm, source the
.bashrc
file:
source ~/.bashrc
- Verify the available versions of Node.js using nvm:
nvm list-remote
This will display a long list of available Node.js versions.
- Install a specific version of Node.js using nvm. For example, to install version
v14.10.0
, run the following command:
nvm install v14.10.0
- View the installed versions of Node.js:
nvm list
The currently active version will be displayed on the first line, followed by any named aliases and their associated versions.
Note: If you also have a version of Node.js installed through apt, you may see a system entry. You can activate the system-installed version using nvm use system
.
- Switch between installed versions of Node.js using
nvm use
. For example, to switch to versionv14.10.0
, run:
nvm use v14.10.0
This will set v14.10.0
as the active version.
Congratulations! You have successfully installed and managed multiple versions of Node.js using the Node Version Manager (nvm).
Removing Node.js
If you need to uninstall Node.js for any reason, the process may vary depending on how you installed it.
To remove the default repository version of Node.js, you can use apt
:
sudo apt remove nodejs
By default, apt remove
retains any local configuration files created during the installation. If you want to remove these configuration files as well, use apt purge
instead:
sudo apt purge nodejs
If you installed Node.js using nvm, you can uninstall a specific version by running:
nvm uninstall node_version
Replace node_version
with the actual version number you want to remove. This command will uninstall the selected version of Node.js.
If the version you want to uninstall is the currently active version, you’ll need to deactivate nvm first:
nvm deactivate
After deactivating nvm, you can uninstall the current version using the uninstall
command mentioned earlier. This will remove all files associated with the targeted version of Node.js.
Conclusion
Installing Node.js on your Ubuntu 20.04 server is made easy with the various methods we’ve covered in this comprehensive guide. Whether you prefer the simplicity of the default repositories, the flexibility of a NodeSource PPA, or the versatility of the Node Version Manager, you can choose the method that best suits your needs.
Node.js empowers developers to build scalable and efficient backend functionality using JavaScript, a language many are already familiar with. By following the installation steps outlined in this guide, you’ll have Node.js up and running on your Ubuntu 20.04 server in no time.
For further information on programming with Node.js, check out our tutorial series on How To Code in Node.js. And if you’re looking for reliable cloud hosting solutions for your Node.js applications, consider Shape.host, the leading provider of Linux SSD VPS services.