Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
NodeJs Installation on Ubuntu 20.04.
This guide will show you different ways to install NodeJS on a Ubuntu 20.04 Server.
Node.js is a free, open-source, cross-platform JavaScript runtime environment. It allows developers to create servers, web apps, command line tools, scripts, and much more.
In this guide, we will show you four different ways of getting Node.js installed on an Ubuntu 20.04 server:
nodejs
versionsnodejs
For the majority of users, installing Node.js with apt would be adequate. If you are actively developing applications using different Javascript Frameworks, you may need to switch between different versions of NodeJS then nvm or fnm is the preferred method.
You must have a Ubuntu 20.04 server setup to follow this tutorial. You should set up a non-root user account with sudo capabilities on your system before you start.
The default repositories of Ubuntu 20.04 include a version of Node.js, which ensures a consistent experience across various systems. At the time of writing, the version available in the repositories is v12. Although it may not be the most recent version, it is expected to be stable and suitable for rapid experimentation with the language.
the version of Node.js included with Ubuntu 20.04, version 12, is now unsupported and unmaintained. You should not use this version in production, and should refer to one of the other sections in this tutorial to install a more recent version of Node.
You can use the apt package manager to get this version. Refresh your package index first in the ubuntu:
sudo apt update
you don’t need to specify the sudo if you are already logged in as a root, You can log in as root using the following command:
sudo su
You may be prompted to enter the root password, so enter that password
You can proceed to NodeJS installation after Logging in as root.
apt install nodejs
Check that the install was successful by querying node for node version number:
node -v
Output
v12.22.9 # it maybe differnet depending on time
If the software available in the repositories meets your requirements, these are the simple steps you’ll need to follow to start using Node.js. Additionally, you may want to install npm, the package manager for Node.js. You can achieve this by installing the npm package using apt.
apt install npm
You can use this method to add modules and packages for use with Node.js.
After successfully installing Node.js and npm with apt and the default Ubuntu software repositories, the following section will demonstrate how to utilize a different repository to install varying versions of Node.js.
If you want to install a different version of Node.js, you have the option to utilize a PPA (personal package archive) that is maintained by NodeSource. These PPAs offer a broader range of Node.js versions compared to the official Ubuntu repositories. As of the current moment, Node.js v16 and v18 are both accessible.
Initially, you’ll need to install the PPA to gain access to its packages. Begin by using curl from your home directory to fetch the installation script for your preferred version. Make sure to substitute 18.x with your desired version string if it’s different.
cd ~
curl -sL https://deb.nodesource.com/setup_18.x -o /tmp/nodesource_setup.sh
The command cd allows you to switch to a subdirectory, return to the parent directory, go back to the root directory, or navigate to a specific directory.
In Unix-like operating systems like Linux and macOS, the cd ~ command is utilized to switch the current working directory to the user’s home directory. The tilde symbol (~
) serves as a shortcut for the current user’s home directory. Therefore, executing cd ~ in the terminal will directly lead you to your home directory.
Refer to the NodeSource documentation for more information on the available versions.
Examine the information in the script you downloaded using Nano or the text editor of your choice.
nano nodesource_setup.sh
After ensuring that the script is safe to execute, close your text editor. Next, use sudo to run the script. Again, no need sudo if you are already logged in as root.
nodesource_setup.sh
The PPA will automatically be integrated into your configuration, and your local package cache will be updated. You will be able to install the Node.js package in the same manner as you did in the preceding section.
apt install nodejs
Make sure to confirm that you have installed the new version by executing the node along with the -v version flag.
node -v
Output
v18.17.0
Here is the text with the specified words wrapped in tags:
The NodeSource package for nodejs includes both the node binary and npm, eliminating the need for a separate npm installation.
After successfully installing Node.js and npm using apt and the NodeSource PPA, the following section will demonstrate how to utilize the Node Version Manager for installing and managing multiple Node.js versions.
Flexibly installing Node.js can be achieved using nvm, the Node Version Manager. This tool allows for the installation and management of multiple independent versions of Node.js and their associated packages concurrently.
For Ubuntu 20.04, the installation of NVM involves visiting the project’s GitHub page and copying the curl command from the main page’s README file to obtain the latest installation script. It’s important to review the script before executing it by not piping the command through to bash right away.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh
Please review the script and ensure that you are okay with the modifications it will apply. Once you’re satisfied, rerun the command with | bash added at the end. The URL to use will vary based on the most current version of nvm. However, at this moment, the script can be downloaded and run using the following:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
This will install the nvm script to your user account. To use it, you must first source your .bashrc file:
source ~/.bashrc
To Allow automatic nvm directory loading and auto-completion run the following command:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Now, list available node versions by nvm:
nvm list-remote
Output
. . .
v20.0.0
v20.1.0
v20.2.0
v20.3.0
v20.3.1
v20.4.0
v20.5.0
v20.5.1
v20.6.0
v20.6.1
v20.7.0
v20.8.0
v20.8.1
v20.9.0 (LTS: Iron)
v20.10.0 (LTS: Iron)
v20.11.0 (LTS: Iron)
v20.11.1 (LTS: Iron)
v20.12.0 (LTS: Iron)
v20.12.1 (LTS: Iron)
v20.12.2 (LTS: Iron)
v20.13.0 (LTS: Iron)
v20.13.1 (LTS: Iron)
v20.14.0 (LTS: Iron)
v20.15.0 (Latest LTS: Iron)
v21.0.0
v21.1.0
v21.2.0
v21.3.0
v21.4.0
v21.5.0
v21.6.0
v21.6.1
v21.6.2
v21.7.0
v21.7.1
v21.7.2
v21.7.3
v22.0.0
v22.1.0
v22.2.0
v22.3.0
v22.4.0
The list is quite extensive. Any of the release versions can be used to install a version of Node.
For example, if you want to obtain version v16.7.0, you can execute:
nvm install v16.7.0
If you have installed Node.js through apt, you might see a system entry here. You have the option to switch to the system-installed version of Node anytime by using nvm use system.
You can switch between installed node versions with nvm use:
nvm use v16.7.0
Replace v16.7.0 with the version you have installed using the nvm install <node-version> command
Now using node v18.20.3 (npm v10.7.0)
# Confirm node version using node -v
Output
v18.20.3
You can List installed Node Versions using:
nvm list
Output
-> v18.20.3
default -> 18 (-> v18.20.3)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.20.3) (default)
stable -> 18.20 (-> v18.20.3) (default)
lts/* -> lts/iron (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.20.3
lts/iron -> v20.15.0 (-> N/A)
. . .
The first line displays the currently active version (-> v18.20.3), and it is followed by several named aliases along with the versions they point to.
The expected version of Node has been successfully installed on your computer. Additionally, a suitable version of npm is also present.
Another way to install node is to use fnm.
fnm (Fast Node Manager) is a command-line tool for managing multiple versions of Node.js. It is designed to be a faster and simpler alternative to other Node.js version managers like nvm
.
fnm is written in Rust, which contributes to its speed and efficiency. It allows developers to easily switch between different versions of Node.js, making it convenient for testing and development across various environments.
Let’s start installing node using fnm.
First, You have to install fnm:
curl -fsSL https://fnm.vercel.app/install | bash -s -- --skip-shell
The installation of fnm will occur within the $HOME/.fnm/ directory.
To update fnm, simply execute the same command used for its initial installation.
There is one more important step. Just add the following to your .bashrc file:
# fnm
export PATH=/home/$USER/.fnm:$PATH
eval "$(fnm env --use-on-cd --version-file-strategy=recursive)"
Save the changes you made to .bashrc by pressing Ctrl + O to write out the file, and then Enter to confirm. Exit nano by pressing Ctrl + X.
To apply the changes to your current shell session, either log out and log back in, or run:
source ~/.bashrc
fnm --version
Output
fnm 1.37.1
. . .
fnm ls-remote
Output
. . .
v20.0.0
v20.1.0
v20.2.0
v20.3.0
v20.3.1
v20.4.0
v20.5.0
v20.5.1
v20.6.0
v20.6.1
v20.7.0
v20.8.0
v20.8.1
v20.9.0 (Iron)
v20.10.0 (Iron)
v20.11.0 (Iron)
v20.11.1 (Iron)
v20.12.0 (Iron)
v20.12.1 (Iron)
v20.12.2 (Iron)
v20.13.0 (Iron)
v20.13.1 (Iron)
v20.14.0 (Iron)
v20.15.0 (Iron)
v21.0.0
v21.1.0
v21.2.0
v21.3.0
v21.4.0
v21.5.0
v21.6.0
v21.6.1
v21.6.2
v21.7.0
v21.7.1
v21.7.2
v21.7.3
v22.0.0
v22.1.0
v22.2.0
v22.3.0
v22.4.0
fnm install v18.3.0
Replace v18.3.0 with the version you have installed using the fnm install <node-version> command
For installing Node of the latest LTS version, you can use the –lts option. So run the following to install it also:
fnm install --lts
Just like nvm, fnm also supports partial version matching. fnm guesses the latest available version from your partial input. For example, if you just do:
fnm install 18
Output
Installing Node v18.20.3
. . .
It will install the Node of version v18.20.3 which is the latest available version starting with 18. So experiment with the above command.
You can switch between installed node versions with fnm use:
fnm use 18
Ouput
Using Node v18.20.3
. . .
To remove Node.js from your system, you have the option to uninstall it using either apt, nvm, or fnm depending on the method of installation. To remove the version from the system repositories, you can utilize the following command with apt:
sudo apt remove nodejs
By default, the apt remove command will retain any local configuration files that were created since the installation. If you want to completely remove Node.js without keeping the configuration files, you can use the apt purge command:
sudo apt purge nodejs
In the case of uninstalling a version of Node.js that was installed using nvm, you should first confirm whether it is the current active version:
nvm current
If the version you want to uninstall is not the current active version, you can proceed with the following command:
nvm uninstall node_version
The output of this command will indicate the successful uninstallation of the selected version of Node.js.
Output
Uninstalled node node_version # node version is your uninstalled version
When uninstalling a specific version of Node.js, you must first deactivate nvm if it is the currently active version to make changes.
This can be done by using the command:
nvm deactivate
Once nvm is deactivated, you can proceed to uninstall the targeted version of Node.js using the previously used uninstall command, which will remove all associated files.
In the case of uninstalling a version of Node.js that was installed using fnm, you should first confirm whether it is the current active version:
fnm current
If the version you want to uninstall is not the current active version, you can proceed with the following command:
fnm uninstall node_version
The output of this command will indicate the successful uninstallation of the selected version of Node.js.
Output
Node version v18.20.3 was removed successfully
Alias default was removed successfully
# node version is your uninstalled version
One can choose from several methods to start using Node.js on an Ubuntu 20.04 server, depending on their specific requirements. While opting for the NodeJS version available in Ubuntu’s repository is one approach, using nvm, fnm or a NodeSource PPA provides more flexibility.