GICT knowledge base
How to Install Node.js and PM2 in Ubuntu 24.04
Published
In this short tutorial, I will show you how to install and configure Node.js and PM2 on your Ubuntu 24.04 server. It is not too hard if you do it step by step. I will use easy words so everyone can understand.
What is Node.js?
Node.js lets you run JavaScript on your server. You know how JavaScript works in web pages? Well, Node.js lets you use JavaScript to make apps that work on your server too.
People use Node.js to make:
- Web sites
- Apps that talk to other apps
- Tools you use in the command line
- Server programs
Many people like Node.js because they already know JavaScript. They do not need to learn a new language.
What is PM2?
PM2 helps your Node.js apps keep running. Sometimes apps stop working or crash. PM2 watches your apps and starts them again if they stop.
PM2 can do these things:
- Start your apps
- Stop your apps
- Show how much memory your apps use
- Run many apps at once
- Keep apps running when you restart your server
PM2 is like having someone watch your apps all the time.
Why Use These?
If you want to make web apps with JavaScript, you need Node.js. If you want your apps to keep working without problems, PM2 helps a lot.
Node.js and PM2 work good together. Node.js runs your code and PM2 makes sure it keeps running.
Getting Ready
You need these things first:
- Ubuntu 24.04 on your server
- Terminal (press Ctrl+Alt+T to open it)
- Permission to install things (sudo)
Make your system new first. Type this in terminal:
sudo apt update
sudo apt upgrade
Wait for it to finish. It might take some time.
Way 1: Install Node.js from Ubuntu
This is the easy way. Ubuntu has Node.js ready to install. But it might be an old version.
See what version Ubuntu has:
apt list nodejs
Install Node.js:
sudo apt install nodejs
You also need npm. It helps you get more Node.js tools:
sudo apt install npm
Check if it worked:
node --version
npm --version
You should see numbers if it worked.
Way 2: Install Node.js from NodeSource
This way gets you new versions of Node.js. NodeSource has the newest versions.
Get the setup script:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
This adds NodeSource to your server. Then install Node.js:
sudo apt install nodejs
Check the versions:
node --version
npm --version
This way usually gives you newer versions.
Way 3: Install Node.js with NVM
NVM means Node Version Manager. It lets you have many versions of Node.js and switch between them.
Install NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
Close terminal and open it again, or type:
source ~/.bashrc
Install Node.js with NVM:
nvm install node
This gets the newest version. You can get other versions too:
nvm install 18.17.0
Use a version:
nvm use 18.17.0
See what versions you have:
nvm list
Install PM2 on Your Server
Now you have Node.js working. Time to get PM2. PM2 is a Node.js package so you use npm to get it.
Type this:
npm install -g pm2
The -g means put it everywhere on your server so you can use it anywhere.
Check if PM2 is there:
pm2 --version
You should see a number.
How to Use PM2
Here are the main things you do with PM2:
Start an app:
pm2 start app.js
Stop an app:
pm2 stop app.js
Start an app again:
pm2 restart app.js
See all apps running:
pm2 list
Get info about apps:
pm2 status
Remove an app:
pm2 delete app.js
Test if Everything Works
Let me show you how to test this. Make a test file:
nano test.js
Put this code in it:
// Test server
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hi! Node.js and PM2 work!');
});
server.listen(3000, () => {
console.log('Server runs on port 3000');
});
Save it. Press Ctrl+X, then Y, then Enter.
Run it with PM2:
pm2 start test.js
See if it runs:
pm2 list
Your app should be in the list. You can also check it in a web browser at http://localhost:3000.
When Things Go Wrong
Sometimes stuff does not work. Here are fixes:
Problem: npm gives permission errors
Fix: Do not use sudo with npm. Make npm use a different folder:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
Problem: PM2 does not start apps when server starts
Fix: Make PM2 start by itself:
pm2 startup
pm2 save
Problem: Node.js is too old
Fix: Use NodeSource or NVM to get new versions.
Keep Things New
Update Node.js if you used apt:
sudo apt update
sudo apt upgrade nodejs
Update PM2:
npm update -g pm2
With NVM, get new versions anytime:
nvm install node
Conclusion
Now you have Node.js and PM2 on your Ubuntu 24.04! You can make web apps, APIs, and other fun projects.
Try these things:
- Make small projects to learn Node.js
- Try Express (a popular Node.js tool)
- Use PM2 to run many apps
- Learn about setting up your apps
PM2 can do more things like clustering, saving logs, and watching your apps. But what you know now is good to start.
The best way to learn is to build things. Start making something and have fun! Node.js and PM2 have good help docs online if you get stuck.
It might seem hard at first but it gets easy fast. Most people get it working the first time if they follow the steps.
Do not worry if you make mistakes. That is how we learn. Just try again. Ubuntu users are nice and help each other if you have problems.
This works the same on other Ubuntu versions too, not just 24.04. The commands are almost the same.
Always save your important files before you make big changes. These installs are safe but it is good to be careful.
I remember when I first did this, I got confused by all the commands. But after doing it a few times, it became easy. My friend had trouble with npm permissions but we fixed it quick.
Take your time with each step. Do not rush. If something does not work, go back and check what you did.
Good luck! You can now build cool things with Node.js and PM2.
Related guides
How to Find Services That Need a Restart After Updates with needrestart
Find services using outdated libraries with needrestart on Ubuntu. Read restart reports, review automatic restart policy and plan maintenance safely.
UbuntuHow to Enable Brotli Compression in Nginx on Ubuntu 24.04
Enable Brotli in Ubuntu Nginx, preserve gzip fallback, verify response headers and avoid stale precompressed assets. Measure savings on your own files.
UbuntuHow to Prevent Overlapping Cron Jobs with flock
Prevent overlapping cron jobs with flock. Use a shared lock, understand skipped runs and verify that every entry point follows the locking policy.
UbuntuSecuring Ubuntu 24 SSH and other best practices
How to Lock Down Your Ubuntu 24 Server: A Beginner's Guide So, you just spun up a brand new Ubuntu 24 server. You may find a lot of SSH brute force logins failures.
