<\/span><\/h2>\n\n\n\nFirst, log in to your Ubuntu 20.04 server via SSH as the root user:<\/p>\n\n\n\n
ssh root@IP_Address -p Port_number<\/pre>\n\n\n\nYou will need to replace \u2018IP_Address\u2018 and \u2018Port_number\u2018 with your server\u2019s respective IP address and SSH port number. Additionally, replace \u2018root\u2019 with the username of the admin account if necessary.<\/p>\n\n\n\n
Before starting, you have to make sure that all Ubuntu OS packages installed on the server are up to date. You can do this by running the following commands:<\/p>\n\n\n\n
apt-get update -y\napt-get upgrade -y<\/pre>\n\n\n\n<\/span>Install Node.js and npm from Ubuntu Repository<\/span><\/h2>\n\n\n\nThe simple and easiest way to install Node.js and npm is to install them from the Ubuntu default repository. However, it does not contains the latest Node.js version. At the time of writing this tutorial, the latest Node.js version available in the Ubuntu 20.04 is 10.19.0.<\/p>\n\n\n\n
First, update the system packages by running the following command:<\/p>\n\n\n\n
apt-get update -y<\/pre>\n\n\n\nOnce all the packages are updated, install the Node.js and npm with the following command:<\/p>\n\n\n\n
apt-get install nodejs npm -y<\/pre>\n\n\n\nOnce both packages are installed, verify the Node.js version using the following command:<\/p>\n\n\n\n
node -v<\/pre>\n\n\n\nYou should get the following output:<\/p>\n\n\n\n
v10.19.0\n<\/pre>\n\n\n\nYou can also check the npm version using the following command:<\/p>\n\n\n\n
npm -v<\/pre>\n\n\n\nYou should get the following output:<\/p>\n\n\n\n
6.14.4\n<\/pre>\n\n\n\n<\/span>Install Node.js and npm from NodeSource<\/span><\/h2>\n\n\n\nNodeSource maintains an APT repository and contains multiple Node.js versions. It allows you to install the specific version of Node.js in your system.<\/p>\n\n\n\n
In this section, we will install Node.js v14.x from the NodeSource.<\/p>\n\n\n\n
First, install the curl with the following command:<\/p>\n\n\n\n
apt-get install curl -y<\/pre>\n\n\n\nNext, download and run the Node.js installation script by running the following command:<\/p>\n\n\n\n
curl -sL https:\/\/deb.nodesource.com\/setup_14.x | bash -<\/pre>\n\n\n\nThis will add the GPG key and Node.js repository to the APT.<\/p>\n\n\n\n
Next, install the Node.js version 14.x by running the following command:<\/p>\n\n\n\n
apt-get install nodejs -y<\/pre>\n\n\n\nOnce installed, verify the installed version of Node.js with the following command:<\/p>\n\n\n\n
node -v<\/pre>\n\n\n\nYou should get the following output:<\/p>\n\n\n\n
v14.15.1\n<\/pre>\n\n\n\nYou can also verify the npm version with the following command:<\/p>\n\n\n\n
npm -v<\/pre>\n\n\n\nYu should get the following output:<\/p>\n\n\n\n
6.14.8\n<\/pre>\n\n\n\n<\/span>Install Node.js and npm with NVM<\/span><\/h2>\n\n\n\nNVM also know as “Node Version Manager” is a script that allows you to manage multiple version of Node.js.<\/p>\n\n\n\n
First, you will need to download and install NVM in your system. You can download and run the script manually with the following command:<\/p>\n\n\n\n
curl -o- https:\/\/raw.githubusercontent.com\/nvm-sh\/nvm\/v0.37.2\/install.sh | bash<\/pre>\n\n\n\nOnce installed, you should get the following output:<\/p>\n\n\n\n
% Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n100 13527 100 13527 0 0 528k 0 --:--:-- --:--:-- --:--:-- 528k\n=> Downloading nvm as script to '\/root\/.nvm'\n\n=> Appending nvm source string to \/root\/.bashrc\n=> Appending bash_completion source string to \/root\/.bashrc\n=> Close and reopen your terminal to start using nvm or run the following to use it now:\n\nexport NVM_DIR=\"$HOME\/.nvm\"\n[ -s \"$NVM_DIR\/nvm.sh\" ] && \\. \"$NVM_DIR\/nvm.sh\" # This loads nvm\n[ -s \"$NVM_DIR\/bash_completion\" ] && \\. \"$NVM_DIR\/bash_completion\" # This loads nvm bash_completion\n<\/pre>\n\n\n\nNext, you will need to close and reopen the terminal to add the path to nvm script to the current shell session.<\/p>\n\n\n\n
Next, verify the NVM version with the following command:<\/p>\n\n\n\n
nvm --version<\/pre>\n\n\n\nYou should get the following output:<\/p>\n\n\n\n
0.37.2\n<\/pre>\n\n\n\nYou can list all Node.js version that can be installed with NVM using the following command:<\/p>\n\n\n\n
nvm list-remote<\/pre>\n\n\n\nThis command will show you a long list of all Node.js versions.<\/p>\n\n\n\n
Next, install the latest stable version of Node.js with the following command:<\/p>\n\n\n\n
nvm install node<\/pre>\n\n\n\nYou should get the following output:<\/p>\n\n\n\n
Downloading and installing node v15.3.0...\nDownloading https:\/\/nodejs.org\/dist\/v15.3.0\/node-v15.3.0-linux-x64.tar.xz...\n######################################################################################################################################## 100.0%\nComputing checksum with sha256sum\nChecksums matched!\nNow using node v15.3.0 (npm v7.0.14)\nCreating default alias: default -> node (-> v15.3.0)\n<\/pre>\n\n\n\nOnce the installation is completed, verify the Node.js version with the following command:<\/p>\n\n\n\n
node --version<\/pre>\n\n\n\nYou should get the following output:<\/p>\n\n\n\n
v15.3.0\n<\/pre>\n\n\n\nIf you want to install the latest LTS version, run the following command:<\/p>\n\n\n\n
nvm install --lts<\/pre>\n\n\n\nYou can now list all installed Node.js versions using the following command:<\/p>\n\n\n\n
nvm ls<\/pre>\n\n\n\nYou should get the following output:<\/p>\n\n\n\n
-> v14.15.1\n v15.3.0\ndefault -> node (-> v15.3.0)\nnode -> stable (-> v15.3.0) (default)\nstable -> 15.3 (-> v15.3.0) (default)\niojs -> N\/A (default)\nunstable -> N\/A (default)\nlts\/* -> lts\/fermium (-> v14.15.1)\nlts\/argon -> v4.9.1 (-> N\/A)\nlts\/boron -> v6.17.1 (-> N\/A)\nlts\/carbon -> v8.17.0 (-> N\/A)\nlts\/dubnium -> v10.23.0 (-> N\/A)\nlts\/erbium -> v12.20.0 (-> N\/A)\nlts\/fermium -> v14.15.1\n<\/pre>\n\n\n\nIf you want to switch the current Node.js version, run the following command:<\/p>\n\n\n\n
nvm use 14.15.1<\/pre>\n\n\n\nYou should get the following output:<\/p>\n\n\n\n
Now using node v14.15.1 (npm v6.14.8)\n<\/pre>\n\n\n\nIn the above guide, you learned three different ways to install Node.js on Ubuntu 20.04 server. You can now choose your desired way to install the Node.js as per your needs.<\/p>\n\n\n\n