How to Install Node.js on Ubuntu: A Comprehensive Guide

This guide will walk you through the steps to install Node.js on Ubuntu, ensuring your environment is ready for development.

Aug 12, 2024 - 10:20
 0  13
How to Install Node.js on Ubuntu: A Comprehensive Guide

Node.js is an essential tool for modern web development, allowing developers to run JavaScript on the server side. If you're working with a Node js development company or considering Node.js development services for your project, you might need to set up Node.js on your Ubuntu machine. This guide will walk you through the steps to install Node.js on Ubuntu, ensuring your environment is ready for development.

Why Use Node.js?

Before diving into the installation process, let's briefly discuss why Node.js is so popular:

  1. High Performance: Node.js uses a non-blocking, event-driven architecture, making it highly efficient for real-time applications.
  2. Scalability: Node.js is designed to build scalable network applications, making it a top choice for large projects.
  3. Single Language: With Node.js, you can use JavaScript on both the client and server sides, streamlining development.
  4. Large Ecosystem: Node.js has a vast ecosystem of libraries and frameworks, allowing developers to build applications quickly and efficiently.

Given these advantages, it's no wonder many businesses are looking to hire Node.js programmers or partner with a Node.js development agency to leverage this powerful technology.

Prerequisites

Before installing Node.js on Ubuntu, make sure you have the following:

  • Ubuntu 20.04 or later: This guide assumes you're using a modern version of Ubuntu.
  • Sudo Privileges: You'll need administrative access to install software on your system.

Step 1: Update Your System

The first step is to ensure your system is up to date. Open a terminal window and run the following commands:

bash
sudo apt update sudo apt upgrade

This command updates the list of available packages and their versions and installs newer versions of your packages.

Step 2: Install Node.js Using the Default Repository

Ubuntu's default repositories include Node.js. This is the simplest method to install Node.js, though it might not give you the latest version.

Install Node.js

To install Node.js from the default repository, run:

bash
sudo apt install nodejs

Verify the Installation

After the installation is complete, verify it by checking the installed version of Node.js:

bash
node -v

This command should display the version of Node.js installed on your system.

Install npm

npm is the Node.js package manager, and it allows you to install libraries and frameworks for your Node.js projects. Install npm by running:

bash
sudo apt install npm

Verify the installation:

bash
npm -v

This will display the version of npm installed.

Step 3: Install Node.js Using NodeSource

If you need a specific version of Node.js, you can use the NodeSource repository, which often has more up-to-date versions than Ubuntu’s default repository.

Add NodeSource Repository

First, add the NodeSource repository for the desired Node.js version. For example, to install Node.js 18.x, run:

bash
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

This command downloads the setup script for Node.js 18.x and runs it.

Install Node.js

Once the NodeSource repository is added, you can install Node.js using:

bash
sudo apt install nodejs

Verify the Installation

After installation, verify the Node.js version to ensure you have the correct one:

bash
node -v

The output should reflect the version installed from the NodeSource repository.

Step 4: Install Node.js Using nvm (Node Version Manager)

If you want the flexibility to install and manage multiple Node.js versions, nvm is the best option. It allows you to switch between different versions of Node.js easily.

Install nvm

First, download and install nvm by running:

bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

After running the script, close and reopen your terminal, or run the following command to load nvm:

bash
source ~/.bashrc

Install Node.js Using nvm

With nvm installed, you can install any version of Node.js. For example, to install Node.js 18.x, run:

bash
nvm install 18

To use the installed version, run:

bash
nvm use 18

Verify the Installation

Check the installed Node.js version:

bash
node -v

This should display the Node.js version managed by nvm.

Step 5: Setting Up a Node.js Development Environment

Once Node.js is installed, you can set up your development environment.

Create a Sample Project

Create a directory for your project:

bash
mkdir my-node-app cd my-node-app

Initialize a new Node.js project:

bash
npm init -y

This command creates a package.json file with default settings.

Install Express.js (Optional)

Express.js is a popular web framework for Node.js. You can install it using npm:

bash
npm install express

This will add Express.js to your project, allowing you to create a web server or API.

Create a Simple Server (Optional)

To test your environment, create a simple Express.js server. Create a new file called app.js:

bash
touch app.js

Add the following code to app.js:

javascript
const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello, Node.js!'); }); app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); });

Start the server by running:

bash
node app.js

Visit http://localhost:3000 in your web browser, and you should see "Hello, Node.js!" displayed on the page.

Conclusion

Installing Node.js on Ubuntu is straightforward, whether you choose to use the default repository, NodeSource, or nvm. Each method has its advantages depending on your needs. By setting up Node.js, you're ready to start building powerful web applications or work with a Node.js development company to bring your ideas to life.

If you're looking to leverage Node.js for your next project, consider hiring Node.js programmers from a reputable Node.js development agency. These experts can help you take full advantage of Node.js, ensuring that your application is built to the highest standards.

With Node.js installed on your Ubuntu machine, you're now equipped to dive into the world of server-side JavaScript development. Whether you're building a simple web app or a complex enterprise solution, Node.js offers the tools and performance you need to succeed.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow