Mastering Node.js Installation on Ubuntu: A Step-by-Step Guide for Developers
Forget generic tutorials laden with outdated commands—this guide cuts through the noise with a streamlined, foolproof process to install Node.js on Ubuntu, ensuring your setup is current, secure, and optimized from the start.
Node.js has become an indispensable part of modern web development and backend services. Whether you're building REST APIs, real-time applications, or full-stack projects, having a robust Node.js environment is critical. Installing Node.js correctly on Ubuntu is the foundation of this setup. A proper installation ensures a stable and performant development environment that scales efficiently as your projects grow.
Let’s dive into the step-by-step process to master Node.js installation on your Ubuntu machine.
Step 1: Update Your System Packages
Before installing anything, it’s good practice to update your package repositories and existing packages. Open your terminal and run:
sudo apt update && sudo apt upgrade -y
This guarantees you get the latest security patches and package lists.
Step 2: Choose Your Installation Method
There are multiple ways to install Node.js on Ubuntu. The most reliable and recommended methods are:
- Using NodeSource PPA (provides the latest stable versions)
- Using nvm (Node Version Manager) for flexible version management
Each method suits different use cases.
Option A: Install Node.js Using NodeSource PPA (Recommended for Most Developers)
NodeSource maintains official repositories for different Node.js versions compatible with Ubuntu.
- Add the NodeSource PPA
For the latest LTS version, run:
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
If you want a specific version (e.g., 18.x), replace setup_lts.x
with setup_18.x
.
- Install Node.js
sudo apt-get install -y nodejs
- Verify Installation
Check which versions installed:
node -v
npm -v
You should see something like:
v18.16.0
9.5.0
Option B: Install Using NVM (Best if You Need Multiple Versions)
NVM allows you to easily switch between Node versions — perfect for testing or multiple project needs.
- Download and install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
Close and reopen your terminal or source your profile:
source ~/.bashrc # Or ~/.zshrc if you use zsh
- Install a specific Node.js version
For example, to install the latest LTS version:
nvm install --lts
- Set default Node.js version
nvm use --lts
nvm alias default lts/*
- Verify installation
Run:
node -v
npm -v
NVM also lets you switch versions easily using nvm use <version>
whenever needed.
Step 3: Verify npm (Node Package Manager) Works Correctly
npm comes bundled with Node.js by default and handles package installations in JavaScript projects.
Run a quick test by installing a popular package globally, like http-server
:
sudo npm install -g http-server
Then launch it in any directory hosting static files:
http-server .
Visit http://localhost:8080
in your browser to confirm it serves files correctly.
Step 4: (Optional) Install Build Tools for Native Modules
If you plan to work with native addons or packages requiring compilation (like bcrypt), you’ll need:
sudo apt-get install build-essential libssl-dev -y
This ensures smooth installations without errors during npm package builds.
Bonus Tips for Smooth Development Workflow on Ubuntu
- Use
npx
to run packages without global installs — e.g.,npx create-react-app my-app
. - Use
.npmrc
files or environment variables to customize npm behaviors. - Regularly update Node.js within your preferred method (
sudo apt upgrade nodejs
for PPA ornvm install <new-version>
). - Pair your setup with VS Code on Ubuntu for an integrated debugging experience.
Troubleshooting Common Issues
- Command ‘node’ not found? Sometimes, older Ubuntu versions have a “node” alias conflict; ensure it's correctly installed by re-running setup or checking PATH variables.
- Permission errors during npm installs? Avoid using
sudo
globally by fixing permissions or using nvm. - If in doubt, uninstall old versions before reinstalling:
sudo apt-get remove nodejs npm -y && sudo apt autoremove -y
Then repeat installation steps.
Summary
By following this step-by-step guide, you've successfully installed a modern and reliable Node.js development environment on Ubuntu! Whether you chose the simplicity of the NodeSource PPA or the flexibility of nvm, you're now set up with a stable platform ready to power backend services, APIs, or full-stack apps—foundation stones for scalable projects ahead.
Master this setup once; future installations will be a breeze — freeing you up to focus on coding great applications rather than fighting tooling issues.
Happy coding!
If this guide helped you get started with Node.js on Ubuntu, share your experience below or suggest topics you'd like me to cover next!