Edited 3 weeks ago by ExtremeHow Editorial Team
Node.jsJavaScriptUbuntuProgrammingLinuxSoftwareInstallationOperating SystemsDevelopmentSystem
This content is available in 7 different language
Node.js is a powerful, JavaScript-based platform built on Google Chrome's V8 JavaScript engine. It is used to develop server-side and networking applications. Node.js applications are written in JavaScript and can be run within the Node.js runtime on a variety of platforms, including Windows, macOS, and Linux. In this article, we will explore how to install Node.js on the popular Linux distribution Ubuntu. We will walk through the steps in simple terms and provide practical examples where necessary.
Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside of a web browser. This means that with Node.js, you can write server-side code using JavaScript. Node.js was initially created to build scalable network applications. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, ideal for data-intensive and real-time applications. Node.js has a huge library of JavaScript modules, called npm (Node Package Manager), which simplifies the development of web applications.
Before you install Node.js, you need to prepare your environment. Follow these steps to get started:
sudo apt update && sudo apt upgrade
This command updates your package list and installs any updated packages.
There are several ways to install Node.js on Ubuntu. Here, we will cover three popular methods:
This is the simplest method, but it may not provide the latest version. There are Node.js packages in the Ubuntu repositories that can be easily installed using the apt package manager.
sudo apt update
sudo apt install nodejs
sudo apt install npm
nodejs -v
And for npm, type:
npm -v
The Node.js version installed through this method may not be the latest. If you need the latest version, consider using nvm or NodeSource.
Node Version Manager (nvm) is a tool that allows you to manage multiple Node.js versions in a single machine. This is especially useful when you need to maintain compatibility with different projects.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Load the newly installed nvm into the current terminal session:
source ~/.bashrc
command -v nvm
If the installation was successful the output should be 'nvm'.
nvm install node
Optionally, specify a specific version:
nvm install v14.17.0
nvm alias default node
or use:
nvm use node
node -v
The advantage of using nvm is the ability to switch between different Node.js versions depending on the project requirements. nvm use VERSION
.
NodeSource maintains up-to-date Node.js packages for enterprise Linux and Ubuntu. This method ensures that you install the latest version.
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
This script automatically adds the required repositories.
sudo apt install -y nodejs
node -v
and for npm:
npm -v
The NodeSource method is a very reliable way to get the latest version of Node.js, while also requiring using the apt package manager for installation management.
After installation, it is important to check if Node.js runs correctly. You can do this by creating a simple Node.js application.
app.js
, in the directory:
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\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
http://127.0.0.1:3000/
to see the "Hello World" output.This confirms that Node.js is installed correctly and you can start developing your applications.
Installing Node.js on Ubuntu can be done in several different ways. Whether you use the system's repository, the Node version manager or the NodeSource repository depends on your personal needs and the specific versioning requirements of your project. Once installed, use it to build traditional web applications, real-time applications, APIs or even complex enterprise-level systems. Each technology has its own advantages, providing different levels of flexibility and ease of maintenance.
After setting up Node.js correctly, you now have the baseline to explore the vast possibilities of JavaScript on server machines. From developing scalable applications to taking advantage of the wide catalog of npm packages, Node.js opens up a whole world of possibilities for any developer working on the web and beyond.
If you find anything wrong with the article content, you can