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: From Basic Setup to Optimal Configuration

Node.js is a cornerstone of modern web development, powering everything from backend APIs to full-stack applications with lightning speed. But before you dive into coding, having a solid Node.js installation on your Linux machine is crucial. A robust and well-configured environment can drastically improve your development workflow, app performance, and overall stability.

Forget one-size-fits-all installation guides—this post will walk you through installing Node.js on Linux with practical, tailored steps. Whether you’re a newbie or looking to refine your environment for maximum efficiency, I’ll cover everything from the simplest setup to advanced, optimal configurations using real commands and examples.


Why Your Node.js Installation Matters on Linux

Linux is the preferred OS for many developers due to its powerful command-line interface and resource efficiency. However, a default Node.js installation may not be optimal for your specific needs—be it app deployment or local development.

Improper installations can lead to dependency hell, permission problems, version conflicts, and slower builds. That’s why customizing your setup based on your project requirements and system config will unlock hidden efficiencies that generic tutorials overlook.


Step 1: Verify Your Current Node.js Environment

Before installing or upgrading Node.js, start by checking what's already installed:

node -v
npm -v

If these commands return version numbers (e.g., v14.15.4), note them down. Older versions or missing tools mean it’s time for an upgrade or fresh install.


Step 2: Choose Your Installation Method

There are several ways to install Node.js on Linux:

  1. Using the distribution package manager (apt/yum/pacman)
  2. Download from NodeSource repository (recommended)
  3. Using nvm (Node Version Manager) for managing multiple versions

Why Not Just Use apt?

The default repositories often host outdated Node.js versions that lack latest features or security patches.


Step 3: Installing Node.js Using NodeSource Repository (Recommended)

NodeSource maintains up-to-date binaries for most Linux distros — providing a reliable way to get the latest stable release.

For Ubuntu/Debian based systems:

Run these commands in the terminal:

# Fetch and run the NodeSource setup script for Node 18 LTS (change version as needed)
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

# Install nodejs package
sudo apt-get install -y nodejs

# Optional build tools for compiling native addons
sudo apt-get install -y build-essential

Verify installation:

node -v
npm -v

You should see something like v18.x.x and a matching npm version.


Step 4: Using nvm for Flexible Version Management

If you need to work with multiple projects requiring different Node versions, nvm is a developer's best friend.

Install nvm:

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

Then reload your shell:

source ~/.bashrc  # or ~/.zshrc depending on your shell

To install and use a specific Node version:

nvm install 18
nvm use 18

Check current active version:

node -v

You can switch between versions easily as project demands evolve.


Step 5: Optimize Your Environment – Beyond Basic Installation

Set npm Global Packages Path Without Sudo

By default, installing global npm packages requires sudo, which can be risky or problematic.

To fix this:

  1. Create a directory in your home for global packages:

    mkdir "${HOME}/.npm-global"
    
  2. Configure npm to use this directory:

    npm config set prefix "${HOME}/.npm-global"
    
  3. Add this directory to your PATH; append the following line in ~/.bashrc or ~/.zshrc:

    export PATH="$HOME/.npm-global/bin:$PATH"
    
  4. Reload shell configuration:

    source ~/.bashrc  # or source ~/.zshrc
    

Now you can safely run global installs without sudo:

npm install -g nodemon pm2 typescript

Leverage pm2 for Process Management in Dev and Prod

Install pm2 globally using your new safe npm global directory:

npm install -g pm2

Start your app with pm2:

pm2 start app.js --name my-app
pm2 startup  # generate startup script so node runs on reboot 
pm2 save     # save process list 

Pm2 handles automatic restarts if your app crashes—a simple way to boost production stability.


Step 6: Bonus – Speed Up NPM Installs With Cache & Mirrors

  • Use npm’s cache effectively by running installs with verbose logs rarely.
  • On slower connections or regions where npm registry is slow, try alternate registries like Taobao:
npm config set registry https://registry.npmmirror.com/

This can cut down dependency install times considerably in some geographies.


Conclusion

Installing Node.js on Linux isn’t just about typing a single command—it’s about tailoring your environment to build faster, more reliable applications efficiently.

Here’s what we covered:

  • Checking existing installations.
  • Installing via up-to-date official repos.
  • Using nvm for flexible version handling.
  • Configuring npm global installs safely.
  • Introducing pm2 process management.
  • Speeding up package installs via mirrors.

With this step-by-step guide tuned specifically for Linux developers serious about performance and stability, your next Node.js project will have a rock-solid foundation right from the first command line entry!


Want more practical tips and deep dives tailored just for developers? Subscribe below and never miss an update!

Happy coding! 🚀