How to Install Node.js on Linux: A Simple Step-by-Step Guide
If you're diving into web development, JavaScript is often a key language to learn. And for running JavaScript outside the browser, Node.js is the runtime environment you need. Installing Node.js on Linux might seem daunting if you're new to the terminal—but don’t worry! This guide will walk you through every step in a simple, practical way.
Why Install Node.js on Linux?
Node.js allows you to build server-side applications with JavaScript—perfect for developing APIs, real-time apps, or scripts. Installing it on your Linux machine ensures you can run these applications locally and prepare for production environments.
How to Install Node.js on Linux: Overview
There are several ways to install Node.js on a Linux system:
- Using your distro’s package manager (e.g.,
apt
for Ubuntu/Debian) - Downloading precompiled binaries from the official website
- Using Node Version Manager (nvm) – recommended for managing multiple versions of Node.js
For beginners and developers who want flexibility, using nvm is often the best choice.
Method 1: Install Node.js Using Your Package Manager
This method uses your Linux distribution’s default package manager.
For Ubuntu/Debian:
Open terminal and update your local package index:
sudo apt update
Install Node.js:
sudo apt install nodejs
To check if Node.js installed correctly:
node -v
You should see something like:
v10.x.x
Note: This version may be outdated depending on your distro’s repositories.
To install npm (Node package manager):
sudo apt install npm
Check npm version:
npm -v
Method 2: Install Latest Node.js Using Nodesource Repository
For newer versions than those available in default repos:
- Add Nodesource PPA:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
(This example uses version 18; replace 18.x
with desired version.)
- Install Node.js:
sudo apt-get install -y nodejs
Verify installation:
node -v
npm -v
Method 3: Install and Manage Node Versions Using nvm (Recommended)
nvm helps you easily install, switch between, and manage multiple node versions.
Step 1: Install nvm
Run the nvm installation script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
After installation completes, reload your terminal or run:
source ~/.bashrc # or source ~/.zshrc if using Zsh
Check that nvm installed correctly:
command -v nvm
# Should output 'nvm'
Step 2: Install Latest Node.js Version with nvm
List available stable versions:
nvm ls-remote --lts
To install latest LTS (Long Term Support) release, run:
nvm install --lts
After installation finishes, verify by checking node version:
node -v # e.g., v18.16.0
npm -v # npm gets installed automatically along with node via nvm
Step 3: Use Multiple Versions (Optional)
To switch between versions you've installed:
nvm use 16 # switches to version 16 if installed previously.
Set default version to use in new terminals:
nvm alias default 18 # sets node v18 as default version.
Bonus: Verify npm works & create a simple test script
Let’s check that npm functions correctly by initializing a sample project.
- Create a new folder and navigate into it:
mkdir my-node-test && cd my-node-test
- Initialize node project (this creates
package.json
):
npm init -y
- Create a simple
index.js
file using your favorite editor (e.g., nano):
nano index.js
Paste this code inside index.js
to print “Hello World” with node:
console.log('Hello World from Node.js!');
Save (Ctrl+O
) and exit (Ctrl+X
).
- Run script with node:
node index.js
Output should be:
Hello World from Node.js!
Summary
- Use
apt
for quick installs but expect older versions. - Use Nodesource repo for newer official binary releases.
- Use nvm for flexible management of multiple node versions—recommended for developers.
- Verify installation by running simple commands and scripts.
Now that you’ve got Node.js up and running on your Linux machine, you’re ready to explore powerful JavaScript development beyond the browser!
If you found this guide helpful or want tutorials on using npm packages or building server apps with ExpressJS next, drop a comment below!
Happy coding! 🚀