How to Install Node.js on Linux (Ubuntu/Debian): Complete Installation Guide

Node.js is a powerful JavaScript runtime that enables server-side development and modern web applications. This comprehensive guide covers multiple methods to install Node.js on Ubuntu and Debian Linux distributions, ensuring you choose the best approach for your needs.

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside a web browser. It’s built on Chrome’s V8 JavaScript engine and includes npm (Node Package Manager) for managing JavaScript packages.

Prerequisites

Before installing Node.js, ensure you have:

  • Ubuntu 18.04+ or Debian 9+ (recommended)
  • Terminal access with sudo privileges
  • Internet connection
  • Basic command-line knowledge

Method 1: Install Node.js from Ubuntu/Debian Repository (Easiest)

Step 1: Update System Packages

sudo apt update
sudo apt upgrade -y

Step 2: Install Node.js and npm

sudo apt install nodejs npm -y

Step 3: Verify Installation

node --version
npm --version

Pros:

  • Quick and simple installation
  • Automatic security updates
  • Stable, tested packages

Cons:

  • Usually older Node.js versions
  • Limited version control

Method 2: Install Node.js Using NodeSource Repository (Recommended)

NodeSource provides up-to-date Node.js packages for Ubuntu and Debian.

Step 1: Download and Run NodeSource Setup Script

For Node.js 20.x (LTS):

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

For Node.js 18.x (LTS):

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

Step 2: Install Node.js

sudo apt install nodejs -y

Step 3: Verify Installation

node --version
npm --version

Step 4: Install Build Tools (Optional)

Some npm packages require compilation:

sudo apt install build-essential -y

Method 3: Install Node.js Using Snap Package Manager

Step 1: Install Snapd (if not already installed)

sudo apt install snapd -y

Step 2: Install Node.js via Snap

sudo snap install node --classic

Step 3: Verify Installation

node --version
npm --version

Method 4: Install Node.js Using NVM (Node Version Manager)

NVM allows you to install and manage multiple Node.js versions.

Step 1: Install NVM

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

Step 2: Reload Shell Configuration

source ~/.bashrc

Step 3: Verify NVM Installation

nvm --version

Step 4: Install Latest Node.js LTS

nvm install --lts
nvm use --lts

Step 5: Install Specific Node.js Version

nvm install 18.17.0
nvm use 18.17.0

Step 6: Set Default Node.js Version

nvm alias default 18.17.0

Useful NVM Commands:

  • nvm list – Show installed versions
  • nvm list-remote – Show available versions
  • nvm current – Show current active version
  • nvm uninstall <version> – Remove specific version

Method 5: Compile Node.js from Source Code

Step 1: Install Dependencies

sudo apt update
sudo apt install build-essential curl file git python3 -y

Step 2: Download Node.js Source

cd /tmp
wget https://nodejs.org/dist/v20.5.1/node-v20.5.1.tar.gz
tar -xzf node-v20.5.1.tar.gz
cd node-v20.5.1

Step 3: Configure and Compile

./configure
make -j$(nproc)
sudo make install

Step 4: Verify Installation

node --version
npm --version

Post-Installation Configuration

Update npm to Latest Version

sudo npm install -g npm@latest

Set npm Global Directory (Optional)

Create a directory for global packages:

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'

Add to your ~/.bashrc:

echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

Install Useful Global Packages

npm install -g yarn nodemon pm2 express-generator

Troubleshooting Common Issues

Permission Errors with npm

If you encounter permission errors, fix npm permissions:

sudo chown -R $(whoami) ~/.npm
sudo chown -R $(whoami) /usr/local/lib/node_modules

Node.js Command Not Found

Add Node.js to PATH:

echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

Uninstall Node.js

From apt repository:

sudo apt remove nodejs npm -y
sudo apt autoremove -y

From NodeSource:

sudo apt remove nodejs npm -y
sudo rm -rf /etc/apt/sources.list.d/nodesource.list

From Snap:

sudo snap remove node

Which Installation Method Should You Choose?

Use NodeSource Repository if:

  • You need the latest stable Node.js version
  • You’re building production applications
  • You want automatic updates

Use NVM if:

  • You work on multiple projects with different Node.js requirements
  • You need to test across Node.js versions
  • You’re a developer who needs flexibility

Use Default Repository if:

  • You need a quick, simple installation
  • You’re just learning Node.js
  • System stability is more important than latest features

Verifying Your Installation

Check Node.js and npm Versions

node --version
npm --version

Test Node.js Installation

Create a test file:

echo 'console.log("Hello, Node.js!");' > test.js
node test.js

Expected output: Hello, Node.js!

Check npm Configuration

npm config list

Conclusion

You now have multiple methods to install Node.js on Ubuntu and Debian systems. The NodeSource repository method offers the best balance of current versions and stability for most users, while NVM provides maximum flexibility for developers managing multiple projects.

Choose the installation method that best fits your development needs and system requirements. Remember to keep your Node.js installation updated for security and performance improvements.

Next Steps

  1. Learn Node.js Basics – Start with simple scripts and modules
  2. Explore npm Packages – Discover the vast npm ecosystem
  3. Set Up Development Environment – Configure your IDE and debugging tools
  4. Build Your First Application – Create a simple web server or CLI tool

Happy coding with Node.js on Linux!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top