BLOG POSTS
    MangoHost Blog / How to Install Node.js on Ubuntu 24 – Developer Guide
How to Install Node.js on Ubuntu 24 – Developer Guide

How to Install Node.js on Ubuntu 24 – Developer Guide

Node.js has become the backbone of modern web development, powering everything from simple APIs to complex enterprise applications. Whether you’re setting up a development environment or deploying production servers, knowing how to properly install Node.js on Ubuntu 24 is crucial for any developer or system administrator. This guide will walk you through multiple installation methods, performance considerations, troubleshooting common issues, and best practices to get your Node.js environment running smoothly on the latest Ubuntu release.

Understanding Node.js Installation Methods on Ubuntu 24

Ubuntu 24 offers several approaches to install Node.js, each with distinct advantages and trade-offs. The choice depends on your specific requirements, whether you need multiple Node.js versions, or if you’re setting up development versus production environments.

The three primary installation methods are:

  • NodeSource Repository – Provides the latest stable releases with automatic updates
  • Node Version Manager (NVM) – Ideal for developers who need to switch between multiple Node.js versions
  • Ubuntu Default Repository – Quick installation but often contains older versions
  • Official Binary Distribution – Direct installation from Node.js official releases
Installation Method Latest Version Multiple Versions Auto Updates Best For
NodeSource Repository ✅ Yes ❌ No ✅ Yes Production servers
NVM ✅ Yes ✅ Yes ❌ Manual Development environments
Ubuntu Repository ❌ Usually older ❌ No ✅ Yes Quick testing
Official Binary ✅ Yes ❌ No ❌ Manual Custom installations

Method 1: Installing Node.js via NodeSource Repository

The NodeSource repository is the most popular method for production environments because it provides recent Node.js versions with regular security updates. This approach integrates seamlessly with Ubuntu’s package management system.

First, update your package index and install essential build tools:

sudo apt update
sudo apt install -y curl software-properties-common

Download and execute the NodeSource setup script for Node.js 20.x (LTS):

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

Install Node.js and npm:

sudo apt install -y nodejs

Verify the installation:

node --version
npm --version

You should see output similar to:

v20.10.0
10.2.3

For the current stable version (Node.js 21.x), replace the setup script URL:

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

Method 2: Installing Node.js with Node Version Manager (NVM)

NVM is essential for developers working on multiple projects that require different Node.js versions. It allows seamless switching between versions without conflicts or complex uninstallation procedures.

Install NVM using the official installation script:

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

Reload your shell configuration:

source ~/.bashrc

Verify NVM installation:

nvm --version

List available Node.js versions:

nvm list-remote

Install the latest LTS version:

nvm install --lts

Install a specific version:

nvm install 18.19.0

Switch between installed versions:

nvm use 20.10.0
nvm use 18.19.0

Set a default version:

nvm alias default 20.10.0

List installed versions:

nvm list

Method 3: Ubuntu Default Repository Installation

While not recommended for production due to potentially outdated versions, the default Ubuntu repository provides a quick installation method for testing or temporary setups.

sudo apt update
sudo apt install -y nodejs npm

Check the installed versions:

nodejs --version
npm --version

Note that Ubuntu’s default repository might install Node.js as `nodejs` instead of `node`. Create a symbolic link if needed:

sudo ln -s /usr/bin/nodejs /usr/bin/node

Method 4: Official Binary Installation

Installing from official Node.js binaries gives you complete control over the installation process and is useful for custom deployment scenarios.

Navigate to the official Node.js download page or download directly:

cd /tmp
wget https://nodejs.org/dist/v20.10.0/node-v20.10.0-linux-x64.tar.xz

Extract the archive:

tar -xJf node-v20.10.0-linux-x64.tar.xz

Move to a system directory:

sudo mv node-v20.10.0-linux-x64 /opt/nodejs

Create symbolic links for system-wide access:

sudo ln -s /opt/nodejs/bin/node /usr/local/bin/node
sudo ln -s /opt/nodejs/bin/npm /usr/local/bin/npm
sudo ln -s /opt/nodejs/bin/npx /usr/local/bin/npx

Performance Considerations and Optimization

Node.js performance on Ubuntu 24 can be significantly improved with proper configuration. Here are key optimization strategies:

Memory Management

Configure V8 heap memory limits for applications handling large datasets:

node --max-old-space-size=4096 app.js

Process Management

Install PM2 for production process management:

npm install -g pm2

Create an ecosystem configuration file:

// ecosystem.config.js
module.exports = {
  apps: [{
    name: 'my-app',
    script: './app.js',
    instances: 'max',
    exec_mode: 'cluster',
    env: {
      NODE_ENV: 'development'
    },
    env_production: {
      NODE_ENV: 'production'
    }
  }]
}

System-Level Optimizations

Increase file descriptor limits for high-concurrency applications:

echo "* soft nofile 65536" | sudo tee -a /etc/security/limits.conf
echo "* hard nofile 65536" | sudo tee -a /etc/security/limits.conf

Real-World Use Cases and Examples

Development Environment Setup

For a typical development workflow supporting multiple projects:

# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc

# Install multiple Node.js versions
nvm install 18.19.0  # For legacy projects
nvm install 20.10.0  # For current projects
nvm install 21.5.0   # For experimental features

# Project-specific version switching
echo "20.10.0" > .nvmrc
nvm use  # Automatically uses version from .nvmrc

Production Server Configuration

Setting up a production server with NodeSource repository:

# Install Node.js
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs

# Install global production tools
sudo npm install -g pm2 yarn

# Configure firewall for Node.js applications
sudo ufw allow 3000
sudo ufw allow 8080

Docker Integration

Dockerfile example using Ubuntu 24 base image:

FROM ubuntu:24.04

RUN apt-get update && apt-get install -y curl
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
RUN apt-get install -y nodejs

WORKDIR /app
COPY package*.json ./
RUN npm install

COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Common Issues and Troubleshooting

Permission Errors with Global Packages

Avoid using `sudo` with npm by configuring a global directory:

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

Node.js Version Conflicts

Remove conflicting installations:

# Remove Ubuntu repository version
sudo apt remove nodejs npm

# Remove NodeSource version
sudo apt remove nodejs npm
sudo rm -rf /etc/apt/sources.list.d/nodesource.list*

# Clean NVM installations
nvm deactivate
nvm uninstall node

Build Tools Dependencies

Some npm packages require native compilation. Install build essentials:

sudo apt install -y build-essential python3-dev

SSL Certificate Issues

Configure npm to handle corporate firewalls or certificate issues:

npm config set strict-ssl false
npm config set registry https://registry.npmjs.org/

Security Best Practices

Regular Security Updates

Keep Node.js updated with automated security patches:

# For NodeSource installations
sudo apt update && sudo apt upgrade nodejs

# For NVM installations
nvm install-latest-npm
nvm install node --reinstall-packages-from=current

Vulnerability Scanning

Regularly audit your dependencies:

npm audit
npm audit fix

Process Isolation

Run Node.js applications with limited privileges:

sudo useradd -r -s /bin/false nodejs
sudo chown -R nodejs:nodejs /path/to/your/app

Advanced Configuration and Integration

Nginx Reverse Proxy Setup

Configure Nginx to proxy requests to your Node.js application:

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
    }
}

Systemd Service Configuration

Create a systemd service for your Node.js application:

[Unit]
Description=Node.js Application
After=network.target

[Service]
Type=simple
User=nodejs
WorkingDirectory=/path/to/your/app
ExecStart=/usr/bin/node app.js
Restart=on-failure
RestartSec=10
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl enable your-app.service
sudo systemctl start your-app.service

This comprehensive guide provides multiple installation approaches, real-world configurations, and troubleshooting strategies to ensure your Node.js environment runs optimally on Ubuntu 24. The choice of installation method should align with your specific use case, whether you’re setting up a development environment with version flexibility or a production server requiring stability and security updates.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

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