Node.js is a powerful JavaScript runtime that lets you build fast, scalable applications on the server side. If you’re running Linux and want to get Node.js up and running, this guide breaks it down step-by-step.
Why Node.js?
Node.js is widely used for building web servers, real-time apps, APIs, and tooling for development workflows. If you’re into JavaScript development, it’s a must-have.
Step 1: Update Your Package Index
Before you install anything, make sure your system is up to date.
sudo apt update && sudo apt upgrade -y
For Red Hat-based systems (like CentOS or Fedora), use:
sudo dnf update -y
Step 2: Choose Your Installation Method
There are a few ways to install Node.js. The best option depends on whether you want the latest features or long-term support (LTS).
Option 1: Install via NodeSource (Recommended)
This gives you access to the latest Node.js versions.
- Add the NodeSource repository: For the LTS version (e.g., Node 18.x):
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
- Install Node.js:
sudo apt install -y nodejs
This also installs
npm
(Node Package Manager) automatically.
Option 2: Install via NVM (Node Version Manager)
This is the most flexible method, letting you switch between Node versions.
- Install NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
Then, restart your terminal or run:source ~/.nvm/nvm.sh
- Install Node.js with NVM:
nvm install 18
- Set the default Node version:
nvm use 18 nvm alias default 18
Great for developers managing multiple Node.js projects.
Option 3: Install via Package Manager (Quick & Easy)
This is the simplest method, but it might not give you the latest version.
sudo apt install -y nodejs npm
Useful for quick installs, but not recommended for production use.
Step 3: Verify the Installation
After installation, check the versions:
node -v
npm -v
If you see version numbers, you’re good to go.
Final Tips
- Use NVM if you’re switching between projects with different Node versions.
- Use NodeSource if you want a specific stable version for production.
- Avoid using your distro’s default package manager unless you just need Node temporarily.
Wrapping Up
Installing Node.js on Linux is quick once you choose the right method for your needs. Whether you’re building a web app, an API, or just playing around with JavaScript on the backend, Node.js is an essential tool to have in your toolkit.
If you ran into any issues or want a guide on setting up a Node.js project next, drop a comment below!