How To Install Node On Ubuntu

How To Install Node On Ubuntu

Reading time1 min
#JavaScript#Development#Linux#Node.js#Ubuntu#NVM

Effortless Node.js Installation on Ubuntu: A Step-by-Step Guide for Reliable Development

Node.js powers countless modern applications, from web servers to real-time tools, making it an essential part of any developer’s toolkit. Ubuntu, renowned for its stability and performance, is a top choice for developers seeking a reliable environment. Getting Node.js up and running properly on Ubuntu lays a solid foundation—cutting down setup headaches and future compatibility issues. If you’ve ever been frustrated by clunky or outdated installation methods, this guide is for you.

Forget clunky, outdated installation methods. This post walks you through a streamlined, foolproof process to install the latest Node.js version on Ubuntu, so you can kick off your projects faster with less debugging related to environment setup.


Why Installing Node.js Correctly Matters

Before diving in, it’s important to understand why the right installation approach is key:

  • Latest Features & Security Updates: Official Ubuntu repo versions are often outdated.
  • Compatibility: Ensures npm and other tools work smoothly together.
  • Stability: A clean install avoids environment conflicts down the road.

With that in mind, let’s jump into how to install Node.js easily on Ubuntu.


Step 1: Update Your System Packages

First things first—make sure your package lists are up-to-date. Open your terminal and type:

sudo apt update && sudo apt upgrade -y

This updates your repository info and ensures existing packages are current.


Step 2: Install curl (if not already installed)

You need curl to fetch installation scripts. Verify with:

curl --version

If it’s not installed, run:

sudo apt install curl -y

Step 3: Add the NodeSource Repository for the Latest Node.js

Ubuntu’s default repos often include older versions of Node.js. To get the most recent stable release directly maintained by Node.js developers, use NodeSource's setup script.

Pick your desired version (LTS recommended). For instance, install Node.js 20.x (LTS as of mid-2024) like this:

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

This command downloads and executes a script that adds the NodeSource PPA (Personal Package Archive) to your system sources list securely.


Step 4: Install Node.js

With the repository added, install Node.js by running:

sudo apt-get install -y nodejs

This installs both node and npm (Node Package Manager).


Step 5: Verify Installation

Check that everything installed correctly by printing versions:

node -v
npm -v

Example output might be:

v20.2.0
9.6.7

If you see versions output without errors, congratulations! You have successfully installed the latest Node.js on your Ubuntu system.


Optional Step 6: Install build-essential for Native Module Support

Some npm packages require compiling native add-ons. To avoid build errors during npm install, it’s smart to set up some tools:

sudo apt-get install -y build-essential

Bonus Tip: Using nvm for Multiple Node Versions (Optional)

If you want flexibility managing multiple Node.js versions (for example testing in v18 and v20), use nvm (Node Version Manager) instead of system-wide installs.

Install nvm:

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

Then load nvm into current shell session:

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Use nvm to install latest LTS easily:

nvm install --lts
nvm use --lts
node -v   # should show LTS version number now 

This approach is popular among developers juggling projects with different version needs.


Wrapping Up

Installing Node.js on Ubuntu doesn't have to be painful or confusing. By using the official NodeSource repository or nvm, you ensure you're working with reliable, up-to-date versions tailored to your development needs.

You’re now ready to build scalable web apps, APIs, automation scripts — whatever passion project awaits — all without wrestling with old software or dependency hell!


Happy coding! 🚀

If this guide helped you get set up smoothly or if you have questions about advanced usage, drop a comment below or reach out on Twitter!


Summary Commands Recap

sudo apt update && sudo apt upgrade -y  
sudo apt install curl build-essential -y  
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -  
sudo apt-get install nodejs  
node -v  
npm -v  

Or using nvm if preferred:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash  
export NVM_DIR="$HOME/.nvm"  
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  
nvm install --lts  
nvm use --lts  
node -v  

Enjoy building with Node on Ubuntu!