How to install Node.js on Ubuntu 20.04?
Introduction
Node.js is an open-source software development platform based on the JavaScript V8 engine, used by many developers worldwide to create scalable and fast web applications.
The main advantage of Node.js is that it allows developers to build server applications using JavaScript on the server side, instead of having to use other programming languages. Additionally, Node.js is very popular due to its ability to handle large numbers of simultaneous connections without application lock-ups.
As you can see, this tool is very useful; for this reason, in this tutorial we will explain how you can install Node.js on an Ubuntu 20.04 server.
Before starting the tutorial, don't you have a Cloud server with Ubuntu 20.04? Don't worry! With just one click, you can easily deploy it with SW Hosting.
That being said, let's now start this tutorial by explaining the first method, or option, to install Node.js on Ubuntu 20.04.
Option 1: Manual Installation
Step 1: Update existing packages
Before installing Node.js on your Ubuntu 20.04 server, make sure that all existing packages are updated. To do this, log in to your server and run the following command:
sudo apt update && sudo apt upgrade
info This command will update all existing packages on your server.
Step 2: Install Node.js
Next, we will explain how to install Node.js on your Ubuntu 20.04 server.
You can install Node.js from the Ubuntu repositories or from the official Node.js website. In this tutorial, Node.js will be installed using the Ubuntu repositories.
Run the following command to add the Node.js repository:
sudo apt install curl
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
After adding the Node.js repository, you can install Node.js and NPM (Node Package Manager) using the following command:
sudo apt install nodejs
To verify that Node.js and NPM have been installed correctly, run the following commands:
node -v
npm -v
These commands should print the version of Node.js and NPM installed on your server.
Step 3: Create an example application with Node.js
Once Node.js is installed, you can create an example application to verify if it is working correctly. Create a JavaScript file called app.js:
nano app.js
Copy the following code and save the file.
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!
');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Run the example application using the following command:
node app.js
The example application should now be available at http://127.0.0.1:3000/.
Step 4: Install global Node.js packages (optional)
If you need to install global Node.js packages, you can do so by running the following command:
sudo npm install -g package_name
Replace package_name with the name of the package you wish to install.
Option 2: One Click App
There is a very quick and easy way to install Node.js. With SW Hosting, you can deploy a cloud server with Node.js with just one click. Our One Click Apps will allow you to access cutting-edge cloud technology in a simple and fast way. Forget about complications and tedious configuration, and enjoy an exceptional cloud experience from the first moment.
Conclusion
Installing Node.js on Ubuntu 20.04 is a simple process that can be completed in a few steps. With Node.js and its package manager NPM, you have a powerful tool to build scalable and high-speed applications.
It is important to mention that the installation of Node.js can also be performed using other methods, such as installation through an nvm version manager. However, installation through SW Hosting's One Click Apps is a simple and recommended way to install Node.js on an Ubuntu 20.04 system, or on any other system.
To finish, we hope that this tutorial has been useful to you and helps you start your project with Node.js on Ubuntu.