Skip to content

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

How to Install Node.js on Ubuntu - Softwarecosmos.com

Node.js has become an essential tool for developers, enabling the creation of scalable network applications and empowering front-end developers to build server-side applications using JavaScript. Whether you’re a beginner looking to start your development journey or an experienced programmer setting up a new environment, installing Node.js on Ubuntu is a straightforward process. This guide will walk you through various methods to install Node.js on Ubuntu, ensuring you choose the one that best fits your needs.

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to execute JavaScript code outside of a web browser. It is built on Chrome’s V8 JavaScript engine and is designed to build scalable network applications. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient for real-time applications like chat servers, streaming services, and APIs.

Why Install Node.js on Ubuntu?

Choosing Ubuntu as your operating system for development offers several advantages:

  • Stability and Performance: Ubuntu is known for its reliability and excellent performance, making it ideal for running development environments.
  • Community Support: A vast community ensures plenty of resources, tutorials, and forums for troubleshooting.
  • Ease of Use: Ubuntu’s user-friendly interface makes it accessible for both beginners and experienced developers.
  • Package Management: Advanced package management with apt simplifies the installation and updating of software like Node.js.
See also  How to Verify Fail2Ban is Working on Ubuntu 20.04: Comprehensive Guide

Installing Node.js on Ubuntu streamlines your development workflow, allowing you to build, test, and deploy applications efficiently.

Prerequisites

Before proceeding with the installation, ensure you have the following:

  • Ubuntu OS: This guide assumes you are using Ubuntu 22.04 LTS or later.
  • Access to Terminal: Familiarity with basic terminal commands will be beneficial.
  • Sudo Privileges: Administrative rights are required to install software.
  • Internet Connection: Required to download packages and dependencies.

Method 1: Installing Node.js via Ubuntu’s Default Repository

Ubuntu’s default repositories include a version of Node.js, making this method the simplest for quick installations. However, the version available might not be the latest.

Step 1: Update Your System

Before installing new packages, it’s a good practice to update your system’s package list to ensure you have the latest information.

sudo apt update

Step 2: Install Node.js and npm

Install Node.js along with npm (Node Package Manager), which is used to manage Node.js packages.

sudo apt install nodejs npm -y
  • nodejs: Installs the Node.js runtime.
  • npm: Installs the Node Package Manager.

Step 3: Verify the Installation

Check the installed versions of Node.js and npm to confirm successful installation.

nodejs -v
npm -v

Example Output:

v10.19.0
6.14.4

Note: The versions installed via the default repository might be outdated for some projects. Consider using NodeSource PPA or NVM for newer versions.

Method 2: Installing Node.js Using NodeSource PPA

The NodeSource PPA (Personal Package Archive) is recommended for installing the latest versions of Node.js. This method ensures you have access to the most recent features and security updates.

Step 1: Update Your System

Ensure your system is up-to-date.

sudo apt update
sudo apt upgrade -y

Step 2: Install Node.js from NodeSource

NodeSource provides up-to-date versions of Node.js. Replace setup_16.x with the desired version (e.g., setup_18.x).

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install nodejs -y
  • curl: Downloads the setup script for the specified Node.js version.
  • bash -E: Executes the script with enhanced environment variables.
  • nodejs: Installs Node.js and npm.

Step 3: Verify the Installation

Check the installed versions to ensure you have the correct Node.js version.

node -v
npm -v

Example Output:

v16.14.0
8.3.1

Tip: Node.js versions are updated regularly. Refer to the NodeSource GitHub for the latest setup scripts.

Method 3: Installing Node.js Using NVM (Node Version Manager)

NVM allows you to install and manage multiple Node.js versions on the same machine. This is especially useful for developers working on different projects requiring different Node.js versions.

See also  Backend-as-a-Service (BaaS) for Modern App Development

Step 1: Install NVM

Download and run the NVM installation script.

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

Note: Always check the official NVM repository for the latest version number.

Step 2: Reload the Shell

After installation, reload your shell configuration to make nvm available.

source ~/.bashrc

Alternative: Close and reopen your terminal.

Step 3: Install Node.js Using NVM

List available Node.js versions:

nvm ls-remote

Install a specific version (e.g., 16.14.0):

nvm install 16.14.0

Set the installed version as default:

nvm use 16.14.0
nvm alias default 16.14.0

Step 4: Set the Default Node.js Version

Ensure that the chosen Node.js version is set as default for future terminal sessions.

nvm alias default 16.14.0

Step 5: Verify the Installation

Check the active Node.js and npm versions.

node -v
npm -v

Example Output:

v16.14.0
8.3.1

Tip: To install the latest Node.js version, simply run nvm install node.

Managing Node.js Versions

With NVM, managing multiple Node.js versions is seamless. Here are some essential commands:

  • List Installed Versions:
    nvm ls
    
  • List Available Versions:
    nvm ls-remote
    
  • Switch to a Different Version:
    nvm use 14.17.0
    
  • Uninstall a Version:
    nvm uninstall 14.17.0
    
  • Upgrade NVM:
    nvm install-latest-npm
    

Troubleshooting Common Installation Issues

Issue 1: Permission Denied

Problem: Receiving a permission error during installation.

Solution:

  • Avoid Using sudo with NVM: NVM manages Node.js versions without requiring sudo. Ensure you’re not using sudo with NVM commands.
  • Check Directory Permissions:
    ls -ld ~/.nvm
    

    Ensure you have read/write permissions. If not, adjust them:

    chmod -R u+rw ~/.nvm
    

Issue 2: npm Not Found

Problem: After installing Node.js, npm is not available.

Solution:

  • Reinstall Node.js:
    nvm reinstall 16.14.0
    
  • Ensure NVM is Loaded: Verify that NVM is correctly loaded in your shell configuration (~/.bashrc or ~/.zshrc).

Issue 3: Old Node.js Version Installed

Problem: After installation, an older Node.js version is active.

Solution:

  • List Installed Versions:
    nvm ls
    
  • Switch to the Desired Version:
    nvm use 16.14.0
    
  • Set the Default Version:
    nvm alias default 16.14.0
    

Issue 4: Node.js Not Recognized

Problem: The system doesn’t recognize node or npm commands.

Solution:

  • Reload Shell Configuration:
    source ~/.bashrc
    
  • Ensure NVM is Properly Installed: Confirm the installation by checking the NVM directory.
    ls ~/.nvm
    

Best Practices

  • Use NVM for Flexibility: Employ NVM to manage multiple Node.js versions effortlessly, especially if working on diverse projects.
  • Regularly Update Node.js and npm: Keep your tools updated to benefit from the latest features and security patches.
    nvm install node --reinstall-packages-from=node
    
  • Avoid Using sudo with Global Packages: When installing global npm packages, refrain from using sudo. Instead, configure npm to install packages in your home directory.
    npm config set prefix '~/.npm-global'
    
  • Backup Configuration Files: Regularly backup your shell configuration files (~/.bashrc, ~/.zshrc) to prevent loss of NVM settings.
See also  10 Mobile App Development Mistakes That Can Make or Break Your App's

Frequently Asked Questions (FAQ)

Can I Install Multiple Versions of Node.js on Ubuntu?

Yes. Using NVM (Node Version Manager), you can install and switch between multiple Node.js versions effortlessly, allowing you to work on different projects with varying version requirements.

Is Installing Node.js via Ubuntu’s Default Repository Safe?

Yes. It’s safe and straightforward, but the version might be outdated. For the latest features and security updates, consider using NodeSource PPA or NVM.

Do I Need sudo to Install Node.js with NVM?

No. NVM manages Node.js versions within your user directory, eliminating the need for sudo and enhancing security by avoiding system-wide changes.

How do I remove Node.js, which is installed via NodeSource PPA?

Step 1: Remove Node.js Package:

sudo apt remove nodejs -y

Step 2: Remove NodeSource PPA Repository:

sudo rm /etc/apt/sources.list.d/nodesource.list
sudo apt update

How Often Should I Update Node.js on Ubuntu?

Regularly. Keep Node.js updated to access the latest features, improvements, and security patches. Using NVM makes updating simple:

nvm install node --reinstall-packages-from=node

Can I Use nvm and NodeSource PPA Together?

Yes, but using one method for managing Node.js versions is generally recommended to avoid conflicts. If you choose to use NVM, removing NodeSource PPA installations is best.

What Do I Do If My Node.js Project Requires a Specific Version?

Use NVM to Switch Versions. Install the required Node.js version and switch to it:

nvm install 14.17.0
nvm use 14.17.0

How Do I Set a Default Node.js Version in NVM?

Command:

nvm alias default 16.14.0

This sets Node.js version 16.14.0 as the default for new terminal sessions.

Is It Possible to Run Node.js Applications as a Background Service on Ubuntu?

Yes. Use process managers like PM2 to run Node.js applications as background services:

npm install -g pm2
pm2 start app.js
pm2 startup
pm2 save

How Do I Check Which Versions of Node.js are Installed with NVM?

Command:

nvm ls

Can I Install Node.js Without npm?

Yes. Node.js can be installed without npm, but npm is essential for managing packages. Most installation methods include npm by default.

Useful Resources

Conclusion

Installing Node.js on Ubuntu is a vital step for developers aiming to build robust and scalable applications. Whether you choose to install it via Ubuntu’s default repository, the NodeSource PPA for the latest versions, or NVM for flexible version management, each method offers unique advantages tailored to different needs.

Key Takeaways:

  • NVM is ideal for managing multiple Node.js versions and avoiding permission issues.
  • NodeSource PPA ensures access to the latest Node.js releases beyond Ubuntu’s default repository.
  • Regularly updating Node.js and npm safeguards your development environment with the latest features and security patches.
  • Following best practices, such as avoiding sudo with NVM and keeping your system updated, enhances security and efficiency.

By following this guide, you can set up a powerful Node.js development environment on Ubuntu, paving the way for creating dynamic and high-performing applications. Whether you’re developing web servers, API endpoints, or real-time applications, Node.js on Ubuntu offers the tools and flexibility needed to succeed in today’s fast-paced development landscape.

Happy Coding!

Author