Node.js is a JavaScript runtime built on Chrome's V8 engine. It lets you run JavaScript on your server. If you're building web applications or APIs, you'll need Node.js installed.
This guide walks you through installing Node.js on Ubuntu 24.04. No prior experience needed.
Prerequisites
Before starting, make sure you have:
- A VPS running Ubuntu 24.04 (check our VPS reviews if you don't have one)
- SSH access to your server
- Basic command line knowledge
Method 1: Install Node.js from Ubuntu Repository (Simplest)
This is the easiest method, but the version may not be the latest.
sudo apt update
sudo apt install nodejs npm -y
Verify the installation:
node --version
npm --version
Ubuntu's default repository often contains an older version. For most users, this is fine. If you need the latest version, use Method 2.
Method 2: Install Node.js from NodeSource (Recommended)
NodeSource maintains up‑to‑date packages for Ubuntu. This method gives you the latest stable version.
First, install the NodeSource setup script:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
Then install Node.js:
sudo apt install nodejs -y
npm is included, so you don't need to install it separately.
Verify the installation:
node --version
npm --version
You should see Node.js version 20.x and npm version 10.x or higher.
Method 3: Install Node.js Using NVM (Most Flexible)
NVM (Node Version Manager) lets you install and switch between multiple Node.js versions. This is useful if you work on different projects with different version requirements.
Install NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Close and reopen your terminal, or run:
source ~/.bashrc
List available Node.js versions:
nvm list-remote
Install the latest LTS version:
nvm install --lts
Set it as default:
nvm alias default 'lts/*'
Verify the installation:
node --version
npm --version
To switch between versions:
nvm use 18.0.0
Test Your Node.js Installation
Create a simple test file:
nano test.js
Add this code:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello from Node.js on Ubuntu!\n');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
Run the file:
node test.js
Open another terminal and test:
curl http://localhost:3000
You should see "Hello from Node.js on Ubuntu!"
Install PM2 (Process Manager for Node.js)
PM2 keeps your Node.js application running after you close your terminal. It's essential for production use.
sudo npm install pm2 -g
Start your application with PM2:
pm2 start test.js --name "myapp"
Set PM2 to start automatically on server boot:
pm2 startup
pm2 save
Set Up Nginx as Reverse Proxy (Optional)
For production websites, you'll want Nginx in front of Node.js. Nginx handles static files, SSL termination, and load balancing.
Install Nginx:
sudo apt install nginx -y
Create a new configuration file:
sudo nano /etc/nginx/sites-available/myapp
Add this configuration (replace your_domain.com with your domain):
server {
listen 80;
server_name your_domain.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_cache_bypass $http_upgrade;
}
}
Enable the site and test:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Troubleshooting Common Issues
"node: command not found"
Node.js isn't installed or not in your PATH. Try logging out and back in, or run `source ~/.bashrc`.
"Permission denied" when running global npm packages
You need to install packages with sudo or fix npm permissions:
sudo chown -R $USER /usr/local/lib/node_modules
"Address already in use"
Another application is using port 3000. Change the port in your code or stop the other process with `kill`.
Next Steps
Now that Node.js is installed, here's what to learn next:
- Build your first REST API using Express.js
- Connect your Node.js app to a database (MySQL, PostgreSQL, or MongoDB)
- Set up SSL with Certbot for HTTPS
- Implement logging and monitoring
Need a VPS to run Node.js on? Check our recommended VPS providers.