
How to Install Node.js on Ubuntu 24
Node.js is a JavaScript runtime environment that allows you to run JavaScript on the server side, making it essential for full-stack development and modern web applications. Installing Node.js on Ubuntu 24 is a crucial step for developers looking to build scalable applications, set up development environments, or deploy production services. This guide will walk you through multiple installation methods, compare their advantages, troubleshoot common issues, and provide best practices for managing Node.js on your Ubuntu 24 system.
Understanding Node.js Installation Methods
There are several ways to install Node.js on Ubuntu 24, each with distinct advantages and use cases. The three primary methods are using the default Ubuntu repository, NodeSource repository, or Node Version Manager (NVM). Understanding these approaches helps you choose the right method for your specific needs.
The default Ubuntu repository provides stability and automatic security updates but often contains older versions. NodeSource offers more recent releases with regular updates, while NVM provides maximum flexibility for managing multiple Node.js versions simultaneously.
Installation Method | Version Control | Ease of Use | Best For | Update Mechanism |
---|---|---|---|---|
Ubuntu Repository | Single version | Very Easy | Production stability | APT package manager |
NodeSource Repository | Single version | Easy | Latest stable releases | APT with external repo |
NVM | Multiple versions | Moderate | Development flexibility | Manual version switching |
Method 1: Installing from Ubuntu Default Repository
The simplest approach uses Ubuntu’s built-in package repository. This method provides excellent integration with the system’s package management and automatic security updates.
sudo apt update
sudo apt install nodejs npm
Verify the installation by checking the versions:
node --version
npm --version
This method typically installs Node.js version 18.x on Ubuntu 24, which is suitable for most production environments. The main advantage is seamless integration with Ubuntu’s security update system, ensuring your Node.js installation receives patches through regular system updates.
Method 2: Installing from NodeSource Repository
NodeSource provides official Node.js packages with more recent versions than Ubuntu’s default repository. This method is recommended when you need the latest LTS or current releases.
First, download and execute the NodeSource setup script:
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
Then install Node.js:
sudo apt install nodejs
For specific versions, replace “lts” with the desired version number:
# For Node.js 20.x
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
# For Node.js 21.x (current)
curl -fsSL https://deb.nodesource.com/setup_21.x | sudo -E bash -
This method automatically configures the NodeSource repository and installs both Node.js and npm. The NodeSource packages include npm by default, eliminating the need for separate installation.
Method 3: Installing with Node Version Manager (NVM)
NVM provides the most flexibility for developers who need to work with multiple Node.js versions or want easy version switching capabilities.
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
Install the latest LTS version of Node.js:
nvm install --lts
nvm use --lts
Install specific versions:
# Install Node.js 18.x
nvm install 18
# Install Node.js 20.x
nvm install 20
# Switch between versions
nvm use 18
nvm use 20
# Set default version
nvm alias default 20
List all installed versions:
nvm list
Verification and Testing
After installation, verify Node.js works correctly by creating a simple test application:
mkdir nodejs-test
cd nodejs-test
echo 'console.log("Node.js is working! Version:", process.version);' > test.js
node test.js
Test npm functionality:
npm init -y
npm install express
echo 'const express = require("express"); const app = express(); app.get("/", (req, res) => res.send("Hello World!")); app.listen(3000, () => console.log("Server running on port 3000"));' > server.js
node server.js
Open another terminal and test the server:
curl http://localhost:3000
Common Issues and Troubleshooting
Several issues commonly occur during Node.js installation on Ubuntu 24. Understanding these problems and their solutions saves significant development time.
Permission issues with npm global packages are frequent:
# Fix npm permissions
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
For conflicting Node.js installations:
# Remove conflicting installations
sudo apt remove nodejs npm
sudo apt autoremove
# Clear npm cache
npm cache clean --force
# Reinstall using preferred method
If you encounter “command not found” errors after NVM installation:
# Manually add NVM to your shell profile
echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.bashrc
echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> ~/.bashrc
source ~/.bashrc
Best Practices and Performance Considerations
When deploying Node.js applications on production servers, consider these optimization strategies:
- Use LTS versions for production environments to ensure stability and long-term support
- Configure npm to use a local registry or cache for faster package installations
- Set up proper logging and monitoring for Node.js applications
- Use process managers like PM2 for production deployments
- Configure proper memory limits and garbage collection settings
For development environments on VPS services, NVM provides the best flexibility:
# Set up development environment
nvm install 18
nvm install 20
nvm use 20
# Install global development tools
npm install -g nodemon typescript ts-node @angular/cli create-react-app
Production deployments on dedicated servers benefit from NodeSource repository installations:
# Production setup
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install nodejs
# Install PM2 for process management
npm install -g pm2
# Configure PM2 to start on boot
pm2 startup
Advanced Configuration and Security
Configure Node.js for optimal performance and security in production environments. Set appropriate environment variables:
# Create environment configuration
sudo nano /etc/environment
Add these lines:
NODE_ENV=production
NODE_OPTIONS="--max-old-space-size=4096"
Set up a dedicated user for Node.js applications:
sudo adduser --system --group nodejs
sudo mkdir /var/www/nodejs
sudo chown nodejs:nodejs /var/www/nodejs
Configure firewall rules for Node.js applications:
sudo ufw allow 3000/tcp
sudo ufw allow 8080/tcp
sudo ufw reload
Integration with Development Tools
Node.js integrates seamlessly with various development tools and databases. Common integration patterns include:
# MongoDB integration
npm install mongodb mongoose
# Redis integration
npm install redis
# PostgreSQL integration
npm install pg
# Docker integration
echo 'FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]' > Dockerfile
Monitoring and Maintenance
Regular maintenance ensures optimal Node.js performance. Set up monitoring and update procedures:
# Check for security vulnerabilities
npm audit
npm audit fix
# Update packages
npm update
# Monitor system resources
htop
iostat 1 5
For comprehensive monitoring, integrate with system monitoring tools:
# Install system monitoring
sudo apt install htop iotop nethogs
# Monitor Node.js processes
ps aux | grep node
netstat -tlnp | grep :3000
Node.js installation on Ubuntu 24 provides a robust foundation for modern web development and server-side applications. Whether you choose the stability of Ubuntu repositories, the currency of NodeSource packages, or the flexibility of NVM, each method serves specific use cases effectively. Regular maintenance, proper security configuration, and performance monitoring ensure your Node.js environment remains efficient and secure for both development and production workloads.
For additional information and official documentation, visit the Node.js official documentation and NVM GitHub repository.

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.