Node.js powers APIs, real-time apps and most modern JavaScript tooling. On Ubuntu 24.04 there are three sensible ways to install it, and picking the right one saves headaches later: Ubuntu's apt package (simplest, but often older), NodeSource (current LTS, system-wide — best for servers), and NVM (multiple versions per user — best for development). Here are all three, plus running an app in production properly.
Option 1 — Ubuntu repository (quick, older version)
sudo apt update sudo apt install nodejs npm -y node -v
Fine for running scripts and tools; usually a version or two behind current LTS.
Option 2 — NodeSource: current LTS, recommended for servers
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash - sudo apt install nodejs -y node -v && npm -v
This adds NodeSource's repository so Node stays updated through the normal apt update routine. Swap 22.x for another LTS line if your app needs it.
Option 3 — NVM: multiple versions, per-user
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash source ~/.bashrc nvm install --lts nvm ls
Switch versions per project with nvm use 20, set a default with nvm alias default 22. Note NVM installs per user and its node is not on root's PATH — one reason servers usually prefer NodeSource.
Global npm packages without sudo
mkdir -p ~/.npm-global npm config set prefix ~/.npm-global echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc && source ~/.bashrc
Never sudo npm install -g — it litters root-owned files around the system and is a security risk.
Running a Node.js app in production
Two solid approaches. PM2, the popular process manager:
npm install -g pm2 pm2 start server.js --name myapp pm2 startup systemd # generates the boot integration command pm2 save
Or a plain systemd unit with no extra tooling — the template is in our systemctl guide. Either way, put Nginx in front as a reverse proxy for TLS and static files:
location / {
proxy_pass http://127.0.0.1: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;
}
Then add a free SSL certificate and your API is production-ready.
Related Ubuntu guides
- How to Manage Services on Ubuntu with systemctl: The Complete systemd Guide
- How to Install a LEMP Stack (Nginx, MySQL, PHP) on Ubuntu 24.04
- How to Get a Free SSL Certificate on Ubuntu with Lets Encrypt and Certbot
- How to Update Ubuntu Server: apt, Security Patches and Unattended Upgrades
Prefer to have experts handle it?
LFA IT provides fully managed Ubuntu VPS and dedicated servers — initial setup, security hardening, monitoring and 24/7 support, so you can focus on your business. Explore our hosting and server management services or open a support ticket and our engineers will take it from there.