How To Install Node Js In Linux

How To Install Node Js In Linux

Reading time1 min
#JavaScript#Programming#Linux#Nodejs#NVM#WebDevelopment

Installing Node.js on Linux: Practical Approaches for Engineers

Node.js is a core dependency for backend JavaScript development, CI/CD scripts, and automation tasks. Production environments, build servers, and developer workstations often require different Node.js versions. Below is a technical breakdown of installation methods, their trade-offs, and a tested procedure for clean version management.

Critical Question: Which Node.js Version, and Why?

  • Distro package managers (apt, yum, dnf) ship LTS versions, often a release or two behind the official schedule. Suitable for stable environments with infrequent updates.
  • NodeSource repositories provide fresher binaries, but lock a single version globally—problematic if running multiple projects with legacy dependencies.
  • NVM (Node Version Manager) enables on-demand version switching per-shell. This is the default recommendation for any development environment dealing with more than one Node.js application.

Below, installation is demonstrated on Ubuntu 22.04 LTS, though the process generalizes to most Linux variants.


1. System Package Manager: The Fast Path, With Limitations

For minimal risk, using your distribution's package manager makes audits and rollbacks simple. But expect version lag.

sudo apt update
sudo apt install nodejs npm

Check installed versions:

node --version     # e.g., v12.22.9
npm --version      # e.g., 6.14.4

Note: On Ubuntu/Debian, node may resolve to an out-of-date release. Compatibility issues with recent npm packages are common.


2. NodeSource Binaries: Fresher, But System-Wide Only

Want a fresher LTS or current version without compiling? NodeSource maintains active build scripts:

# Example for Node.js 20.x LTS
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs

Check installation:

node --version  # e.g., v20.10.0
npm --version

Known issue: This method overwrites any previous Node.js installation. No side-by-side versioning is possible. Global node_modules can accumulate unwanted dependencies—clean up with sudo npm cache clean -f as needed.


3. Node Version Manager (nvm): Flexible and Isolated

Recommended for development and CI/CD containers. No sudo required, per-user installs.

a) Install nvm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

Gotcha: Always relaunch your shell or source the profile:

source ~/.bashrc    # or ~/.zshrc

b) Use nvm to Install Node.js (LTS Preferred)

nvm install --lts           # e.g., v20.12.2 LTS at time of writing
nvm install 18.19.1         # Example for legacy project
nvm ls                      # Show installed versions
nvm use 18.19.1

Non-obvious tip: To set a default Node.js for every new shell session:

nvm alias default 20

NVM manages npm as well. Switching Node.js versions automatically switches npm.


4. Sanity Check: Building a Minimal Node App

Testing context: ensure both node and npm invoke the expected binaries.

mkdir quick-test && cd quick-test
npm init -y
echo "console.log('Node.js installed: version', process.version);" > index.js
node index.js

Expected output:

Node.js installed: version v20.12.2

Real-world anomaly: Occasionally, npm symlinks become stale after upgrading via different methods. If npm is missing or broken, re-install via nvm or re-source shell.


Table: Installation Approaches at a Glance

MethodVersion controlMulti-versionSudo neededProduction use
apt/yumlownoyesyes
NodeSourcemediumnoyesyes
nvmhighyesnonot advised

Note: For system-level or multi-user environments (servers, production), avoid nvm—stick to apt/yum or managed containers.


Bottom Line

  • For isolated development, nvm is the practical choice.
  • For stable, OS-integrated deployments, use your distribution's package manager or NodeSource.
  • Verify node and npm resolve to intended versions after installation.
  • Beware path conflicts and stale symlinks—especially if switching methods midstream.

There are edge cases: some CI runners or Docker base images come with pre-baked Node.js versions. Always check with node -v at the start of your session.