3v-Hosting Blog

How to Install Node.js on Ubuntu 22.04

Administration

7 min read


Node.js is a cross-platform JavaScript runtime environment that allows developers to execute JavaScript code server-side. It has become a fundamental tool for modern web development, powering a wide variety of applications from web servers to API development. This guide provides a detailed, step-by-step process for installing Node.js on Ubuntu 22.04, covering different methods to cater to diverse user needs.

 


Why Choose Node.js for Your Development Environment?

Node.js is built on the V8 JavaScript engine, which makes it fast, scalable, and efficient. Its event-driven, non-blocking I/O model is particularly suited for data-intensive real-time applications. Node.js is also tightly integrated with npm (Node Package Manager), which simplifies the installation and management of third-party packages and libraries, offering access to thousands of tools for development.

Given its importance, knowing how to install Node.js on an Ubuntu-based system is essential for developers working on Linux environments. There are multiple ways to accomplish this task, and choosing the right one depends on your specific requirements.

 

 

 

 

Method 1: Installing Node.js from the Ubuntu Repository

The easiest way to install Node.js on Ubuntu 22.04 is to use the default package repository. This method is quick and simple, but the version of Node.js included in the Ubuntu repositories may not always be the latest version.

 

Step 1: Update the Package Index

Start by ensuring your system is up-to-date:


    sudo apt update

This command updates your system’s package index, ensuring that you’re downloading the latest available versions of software from Ubuntu’s repository.

 

 

Step 2: Install Node.js

After updating the package index, you can install Node.js using the following command:


    sudo apt install nodejs

This will install Node.js from the default Ubuntu repositories. To verify the installation and check the version installed, use:


    node -v

 

 

Step 3: Install npm (Node Package Manager)

Since npm doesn’t always come preinstalled with Node.js, you may need to install it separately. npm is crucial for managing JavaScript packages, modules, and dependencies.


    sudo apt install npm

You can verify the npm installation by running:


    npm -v

While this method is quick, it may not give you the latest version of Node.js, which can be a drawback if you need the most recent features or security updates.

 

 

 

 

Method 2: Installing Node.js Using NodeSource PPA

To install the latest version of Node.js, it is recommended to use the NodeSource PPA (Personal Package Archive). NodeSource provides an up-to-date version of Node.js that can be installed on Ubuntu 22.04.

 

Step 1: Install NodeSource Repository

First, you need to add the NodeSource repository to your system. To do this, use the following command:


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

This script configures your package manager to access the NodeSource repository, making it easy to install Node.js and its latest version (version 18.x at the time of writing).

 

 

Step 2: Install Node.js

Once the repository is set up, install Node.js using the apt package manager:


    sudo apt install -y nodejs

This command will install both Node.js and npm. Verify the installation by checking the Node.js and npm versions:


    node -v
    npm -v

 

 

Step 3: Managing Different Node.js Versions (Optional)

If you need to switch between different Node.js versions, consider installing a version management tool like 'n'. After installing Node.js from the NodeSource PPA, you can install 'n' through npm:


    sudo npm install -g n

You can then switch between different Node.js versions using:


    sudo n <version>

 

 


 

Other useful articles for web developers and administrators:


    - How to Reload and Restart Nginx

    - 5 Web Development Trends in 2024

    - How to make your website more secure for less money?

    - How to Create an Awesome Website Homepage

 


 

 

Method 3: Installing Node.js via nvm (Node Version Manager)

For developers who frequently work with multiple versions of Node.js, using nvm (Node Version Manager) is often the best solution. This method gives you the flexibility to install and manage multiple Node.js versions on your system.

 

Step 1: Install nvm

First, install nvm using the official installation script:


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

After the installation is complete, either close and reopen your terminal, or run the following command to apply the changes to your session:


    source ~/.rc

You can verify that nvm has been installed by running:


    nvm --version

 

 

Step 2: Install Node.js Using nvm

Once nvm is installed, you can install the latest version of Node.js using the following command:


    nvm install node

You can also install a specific version of Node.js by specifying the version number:


    nvm install 16.13.0

 

 

Step 3: Switching Between Node.js Versions

To switch between different versions of Node.js, use:


    nvm use <version>

For example:

    nvm use 14.17.0

To view all the installed versions of Node.js on your system, run:


    nvm ls

This method is highly flexible and is ideal for developers who need to manage multiple Node.js projects with different version requirements.

 

 

 

 

Method 4: Building Node.js from Source (Advanced)

For advanced users who need full control over the Node.js build process, installing Node.js from source might be the best approach. This method ensures that you can tailor the Node.js build to your specific system requirements.

 

Step 1: Install Required Build Tools

First, ensure that your system has the required dependencies for building Node.js from source:


    sudo apt install -y build-essential curl

 

 

Step 2: Download Node.js Source Code

Next, download the latest stable version of Node.js from the official website, unzip it and go to the unzipped directory:


    curl -sL https://nodejs.org/dist/latest-v18.x/node-v18.x.tar.gz -o node-latest.tar.gz
    tar -xzf node-latest.tar.gz
    cd node-v18.x

 

 

Step 3: Build and Install Node.js

Once the source code is downloaded and extracted, compile Node.js:


    ./configure
    make -j4
    sudo make install

After the installation is complete, verify the version installed:


    node -v

This method is not recommended for beginners as it requires a deeper understanding of the Linux build environment and may take considerably longer to complete than the other methods.

 

 

 

Conclusion

There are several ways to install Node.js on Ubuntu 22.04, each with its advantages and considerations. The method you choose depends on your specific needs:

    - If you need a quick and simple installation, use the default Ubuntu repository.
    - For the latest version of Node.js, consider using the NodeSource PPA.
    - If you work with multiple projects and need flexibility, nvm is an excellent tool for managing different Node.js versions.
    - Advanced users who require custom builds may opt to install Node.js from source.

Regardless of the method you choose, managing Node.js on Ubuntu is a relatively straightforward process that offers flexibility, enabling you to set up your development environment exactly as needed. This guide ensures you can install Node.js efficiently, whether you’re a beginner or an advanced user.

By following these steps, you'll be equipped to install Node.js and start building your JavaScript applications on Ubuntu 22.04 with confidence.