How to Install Node.js on Ubuntu 22.04: A Step-by-Step Guide


5 min read 15-11-2024
How to Install Node.js on Ubuntu 22.04: A Step-by-Step Guide

Node.js has rapidly grown to become one of the most popular platforms for developing web applications and server-side software, owing to its non-blocking, event-driven architecture. With its vibrant ecosystem of libraries and tools, it allows developers to write scalable applications in JavaScript both on the client and server sides. If you’re an aspiring developer or a seasoned professional wanting to install Node.js on Ubuntu 22.04, you’ve come to the right place. In this comprehensive guide, we’ll take you through the step-by-step process of installing Node.js, ensuring you have a solid grasp of not only the how, but also the why.

Understanding Node.js

Before diving into the installation process, let’s take a moment to understand what Node.js is and why it might be beneficial for you. Node.js is an open-source, cross-platform runtime environment that executes JavaScript code outside of a browser. This unique capability allows developers to build server-side applications using JavaScript, making it a versatile tool for both front-end and back-end development.

The Node.js ecosystem is rich with packages available through the Node Package Manager (NPM), which simplifies the process of managing third-party libraries. This environment is particularly favored for building real-time applications, REST APIs, and microservices. Given its numerous advantages, ensuring you have Node.js properly installed on your Ubuntu system is the first step in unlocking its potential.

Pre-requisites

Before you embark on the installation process, ensure you have the following prerequisites:

  1. Ubuntu 22.04 Installed: You need a working version of Ubuntu 22.04.
  2. Terminal Access: Basic terminal knowledge is essential for navigating the command line interface (CLI).
  3. Sudo Privileges: You should have sudo access to install packages.

Step 1: Update Your System

To begin, it’s always a good idea to ensure your system packages are up to date. Open your terminal and run the following commands:

sudo apt update
sudo apt upgrade -y

This process updates the package index and upgrades all the installed packages to their latest versions.

Step 2: Install Required Dependencies

Next, install the necessary build dependencies for Node.js. To do this, execute the following command:

sudo apt install curl software-properties-common -y

The curl tool is essential for downloading scripts from the internet, while software-properties-common is a package that allows you to manage repositories.

Step 3: Adding the NodeSource Repository

The most reliable way to install Node.js is through NodeSource, which provides an up-to-date version of Node.js. Use the following command to add the NodeSource repository for Node.js v18 (the latest LTS version as of this writing):

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

This command downloads the setup script from NodeSource and executes it, which adds the appropriate repository to your system’s list of sources.

Step 4: Install Node.js

Once the repository is added, you can now install Node.js using the following command:

sudo apt install nodejs -y

This command installs both Node.js and NPM, the Node Package Manager, which will be useful for managing your JavaScript packages.

Step 5: Verify the Installation

To confirm that Node.js and NPM have been successfully installed, check their versions by running the following commands:

node -v
npm -v

The output should display the installed versions of Node.js and NPM, respectively. If you see version numbers, congratulations! You’ve successfully installed Node.js on your Ubuntu 22.04 system.

Step 6: Install Build Essentials (Optional)

If you intend to compile and install native addons from NPM, it’s advisable to install build-essential. This package includes the necessary tools to compile C/C++ programs and is often required for certain Node.js packages that depend on native code:

sudo apt install build-essential -y

Step 7: Configure Your Environment (Optional)

For development purposes, you may want to configure your environment variables. Create a .bashrc or .bash_profile file in your home directory if you don’t already have it, and add the following line:

export NODE_PATH=/usr/lib/node_modules

Using Node.js

Now that Node.js is installed, you can start building your applications! Here’s a simple example of how to create a basic HTTP server using Node.js.

  1. Create a New Directory:
mkdir my-node-app
cd my-node-app
  1. Initialize a New Node.js Project:
npm init -y
  1. Create an app.js File:
nano app.js
  1. Add the Following Code to app.js:
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}/`);
});
  1. Run the Application:
node app.js

Now, you can visit http://127.0.0.1:3000 in your browser to see your Node.js server in action!

Conclusion

Installing Node.js on Ubuntu 22.04 is a straightforward process that opens up a world of possibilities for web development and server-side applications. With the knowledge you’ve gained, you can explore the vast ecosystem of libraries and frameworks built upon Node.js, such as Express.js for building APIs or React.js for front-end development.

Feel free to experiment with different packages and frameworks available on NPM to enhance your skills. Remember, the journey of a thousand applications starts with a single installation!

FAQs

1. How do I uninstall Node.js? To uninstall Node.js, run the following command:

sudo apt remove nodejs npm -y

2. Can I install multiple versions of Node.js? Yes, you can use a version manager like nvm (Node Version Manager) to install and manage multiple versions of Node.js.

3. What is the difference between Node.js and NPM? Node.js is a runtime environment for executing JavaScript on the server side, while NPM is the package manager for Node.js, allowing you to install libraries and frameworks.

4. Is Node.js suitable for production environments? Yes, Node.js is widely used in production environments due to its performance and scalability, especially for I/O-heavy applications.

5. Can I use Node.js with databases? Absolutely! Node.js works seamlessly with various databases like MongoDB, MySQL, and PostgreSQL, providing drivers and ORM libraries for database interaction.

By following this guide, you should now have a solid foundation for using Node.js on your Ubuntu 22.04 system. Happy coding!