How To Install Node Ubuntu

How To Install Node Ubuntu

Reading time1 min
#Node.js#Ubuntu#JavaScript#NVM#Node

Mastering Node.js Installation on Ubuntu: From Basic Setup to Optimized Development Environment

A misconfigured Node.js installation on Ubuntu reliably triggers headaches: version drift, broken global modules, and intrusive “permission denied” errors with npm install -g. Even production deployments get tripped up if the runtime or package manager lags behind supported LTS releases.

Here’s a proven workflow for Node.js installation on Ubuntu—practical, secure, and suited for both local development and server deployment.


Baseline: System Preparation

No environment upgrade is complete without verifying OS currency. Start simple:

sudo apt update && sudo apt upgrade -y
sudo apt install build-essential curl -y

This avoids classic “missing make/g++” build failures while compiling native modules (for example, when building bcrypt or sqlite3).


Decision Point: Installation Methods Compared

Ubuntu offers three main approaches to install Node.js:

MethodNode versionUpdatesRoot NeededSuitable For
apt (default repo)Old (e.g. 12.x, 14.x)RareYesScripting, legacy projects
Nodesource PPALTS/currentYesYesProduction, stable dev
NVMAny (full control)UserNoMulti-project development

Critical: System-wide installs (apt, Nodesource) require root for upgrades. NVM—installation per-user, no need for sudo, greatly reducing risk of clobbering system binaries.


Example: Apt Repository (Rarely Recommended)

sudo apt install nodejs npm -y
node -v    # Typically v12.x or v14.x as of Ubuntu 22.04

Notable issue: Security patches lag official releases by weeks or more.


Example: Nodesource PPA (Most Production Setups)

Fetch the LTS v18.x installer for compatibility (v20.x has minor ecosystem breakages as of Q2 2024):

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
node -v   # Should output v18.20.x or similar
npm -v

Gotcha: Mixing this with manually installed npm or older globally-installed modules leads to hard-to-diagnose version mismatches. Always use the bundled npm unless you have a reason not to.


Example: NVM (Node Version Manager) – Dev Environments with Multiple Projects

You want to hop between LTS and bleeding-edge Node? NVM remains the real solution.

Install:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
# Activate (for bash, otherwise .zshrc or .profile)
source ~/.bashrc

Install Node LTS and set as default:

nvm install 18
nvm alias default 18
nvm use 18

Switch to a specific version for a legacy app:

nvm install 14
nvm use 14

No system-wide pollution. Node and global npm modules remain within ~/.nvm.


Smoke Test: Isolated Node Execution

Standard check:

// hello.js
console.log("OK: " + process.version);
node hello.js
# Output: OK: v18.20.2

If you see something like “/usr/bin/env: ‘node’: No such file or directory”, your $PATH is likely misconfigured. With NVM, verify which node points to a path in your home directory, not /usr/bin.


Optimizing for Development: Avoiding Permission Issues with Global Packages

When using NVM, this headache disappears—globals are always user-writable. With a system install, you will otherwise see errors like:

npm ERR! Error: EACCES: permission denied, access '/usr/lib/node_modules'

Fix (for system installs):

mkdir "${HOME}/.npm-global"
npm config set prefix "${HOME}/.npm-global"
echo 'export PATH=$HOME/.npm-global/bin:$PATH' >> ~/.profile
source ~/.profile

You may need to re-login for shells/terminals to see the updated $PATH.


Project-Level Version Pinning

In poly-repo scenarios, aligning Node versions across all contributors is mandatory. Simple fix:

echo "18" > .nvmrc

Now, nvm use auto-selects the required runtime.

Side note: Many CI/CD systems (e.g., GitHub Actions with actions/setup-node) respect .nvmrc by default, simplifying pipeline config.


Upgrades & Maintenance

Updating Nodesource iterations:

sudo apt update && sudo apt upgrade nodejs npm -y

For NVM users:

nvm install 20    # Try new LTS (non-breaking for most apps after v18)
nvm uninstall 18  # After testing migration

Tip: Always retest native module build steps after major Node upgrades—modules like sharp or node-gyp can break silently due to ABI mismatches.


Real-World Issue: Out-of-Tree Node Installs

Sometimes system images (especially cloud VMs built from old snapshots) have embedded Node binaries at /usr/local/bin/node or even /opt/nodejs. Diagnose with:

find / -type f -name 'node' 2>/dev/null

If multiple binaries appear, remove or symlink carefully; stray versions can cause unpredictable failures in CI pipelines.


Summary Table: When to Use Each Method

ScenarioInstallation Approach
Dedicated production VMNodesource PPA LTS
Multi-app dev laptopNVM per-user
Disposable CI containerNodesource or NVM
Legacy bash scriptsSystem apt packages

Stability and long-term maintainability require periodic checks: always audit $PATH, node -v, npm -v, and verify build tooling (build-essential). No install is ever truly “set and forget.”


Questions, real-world corner cases, or regressions after a major Node update? Pin the problem and compare your $PATH and Node binary with nvm which current or which node—misalignment is almost always the culprit.