How To Install Node In Linux

How To Install Node In Linux

Reading time1 min
#Development#JavaScript#Linux#Node.js#nvm#pm2

Mastering Node.js Installation on Linux: Practical Approaches from Baseline Setup to Robust Configuration

Node.js remains integral to service APIs, CI/CD tasks, and full-stack JavaScript applications. On Linux—preferred for its minimalism and process transparency—Node.js installation impacts runtime reliability, build speed, and even permission hygiene. Missteps here often rear their head as broken dependencies, outdated feature sets, or misaligned environments between dev and production.


Diagnosing Current Node.js State

An unexamined system can hide ghosts. Check which Node.js (and npm) version is currently installed and accessible on $PATH:

node -v
npm -v

If you see command not found, or versions below the LTS baseline (v18.x.x as of mid-2024), consider a complete reinstallation.


Approach Selection: Package Manager vs. NodeSource vs. nvm

Three practical installation paths:

MethodProsCons
system package managerSimplifies updates, integrates with systemdOften lags major releases
NodeSource repositoryContemporary LTS/support, easy to upgradeTied to package manager, static ver.
nvm (Node Version Mgr)Multiple Node versions per user/shellNot system-wide, relies on shell

Note

Many default system repositories (e.g., Ubuntu’s apt) ship Node.js several versions behind current releases. Relying on these can mean missing out on performance and security updates.


High-Fidelity Installation with NodeSource (Debian/Ubuntu Example)

NodeSource enables immediate access to recent Node releases, especially for LTS branches.

# Installs Node 18.x LTS — adjust '18.x' as needed for a different version
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs

# Optional: build-essential assists with native modules (e.g., bcrypt, sharp, sqlite3 bindings)
sudo apt-get install -y build-essential

Immediately verify functional install:

node -v      # should return v18.x.x or desired version
npm -v

Gotcha

If you see /usr/bin/env: ‘node’: No such file or directory, check whether your distro names the binary nodejs instead of node. Symlink if necessary:

sudo ln -s /usr/bin/nodejs /usr/bin/node

Managing Multiple Versions: nvm

Hybrid teams, legacy maintenance, or experimental features frequently mandate non-global Node installations. Enter nvm, a user-local Node version manager.

Installation (user-scoped, non-root):

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
source ~/.bashrc  # or ~/.zshrc if applicable

List available versions and install:

nvm ls-remote      # shows all official versions
nvm install 18     # installs latest v18.x LTS
nvm install 20     # (optional) install Node 20.x alongside
nvm use 18         # switch session to 18.x

Switch versions per project domain—use .nvmrc files for automated context switching in larger monorepos.

Known Issue

nvm does not integrate with systemd services out-of-the-box. Scripts executed by systemd may miss user-specific Node installations unless expressly set $PATH.


npm Global Installs: No More Sudo

Global npm installations via sudo can corrupt permissions, especially if system and nvm installations collide. Instead, point npm global prefix to a user directory:

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

No more root escalation for CLI utilities:

npm install -g pm2 typescript nodemon

Robust Process Management with pm2

pm2 is frequently used in production for orchestration and service resilience (auto-restart, monitoring, log rotation):

pm2 start dist/server.js --name user-api
pm2 save
pm2 startup  # outputs systemd/init command — run it *as root* if service is critical

List managed processes:

pm2 ls

Non-obvious tip

For clustered Node apps, let pm2 handle scaling:

pm2 start app.js -i max   # Spawns as many instances as CPU cores

Accelerate npm Installs: Mirrors and Caching

On suboptimal connections, npm install can bottleneck deployment pipelines. Use regional mirrors, e.g. for mainland China:

npm config set registry https://registry.npmmirror.com/

Also, enable npm’s local cache (default from npm v5+), but routinely clear it to avoid corrupted downloads:

npm cache clean --force

Imperfect Realities and Alternatives

Distribution containers (e.g. Alpine) present edge cases—Alpine’s musl libc and stripped-down build chains can trip native npm modules. Sometimes fall back to binary-only modules or adapt the build pipeline with custom Dockerfiles. YMMV.


Summary Table

StepCommand ExampleNotes
Verify Installationnode -v, npm -vIdentify current state
NodeSource Installsee aboveLTS releases, simple rollback
nvm Usagenvm install <ver> / nvm use <ver>User-scoped, ideal for polyglot
npm Global Prefixnpm config set prefix ~/.npm-globalNo sudo, fewer permission issues
pm2 Process Mgrpm2 start, pm2 save, pm2 startupRobust production process control
Registry Mirrornpm config set registry <URL>Faster CI, regional access

Shaving off seconds from builds or minimizing permission escalation may not look critical, but multiplied over CI builds, deployments, and escalated bug reports—these gains compound. Aim for flexible, reproducible, non-root node installations. And remember: revisit your node/nvm/pm2 versions quarterly. LTS is LTS until it isn’t.

Should a custom Docker base image, system-wide deployment, or ephemeral testing context be involved? That’s another operational playbook entirely.