Nodejs safe workflow

First, let’s explain the two commands that developers commonly use when installing Nodejs.

A.

bash

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

This command downloads the NVM installer script and executes it as root.

1. sudo

  • Runs the command with root (administrator) privileges.
  • This means both curl and bash will run as root.

2. curl -o- <URL>

  • curl downloads files from the internet.
  • -o- tells curl to write the output to stdout (the terminal) instead of saving it to a file.
  • So this fetches the script at:
bash

https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh

and sends it directly to the next command via a pipe.

3. | sudo bash

  • The pipe (|) takes the script downloaded by curl and feeds it into bash.
  • Since it’s prefixed with sudo, the script runs as root.

4. What the script does

The URL points to the official NVM (Node Version Manager) installer.
That script:

  • Creates the ~/.nvm directory.
  • Adds lines to your shell config (.bashrc, .zshrc, etc.) so nvm loads automatically.
  • Prepares your system so you can run commands like nvm install node to manage multiple Node.js versions.

B.

bash

sudo curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash - && sudo apt-get install -y nodejs

This command installs Node.js 20.x system-wide via APT (the standard Debian/Ubuntu package manager). It makes Node.js available to all users on the system.

1. sudo curl -fsSL

  • curl downloads the setup script from NodeSource:
https://deb.nodesource.com/setup_20.x
  • Flags:
    • -f → fail silently on HTTP errors (no HTML error pages).
    • -s → silent mode (no progress bar).
    • -S → still show errors if they occur.
    • -L → follow redirects.
  • curl just fetches the script, but since it writes output to stdout it doesn’t touch your system yet.

2. | sudo bash –

  • Pipes (|) the downloaded script into bash with root privileges.
  • That script configures your APT (package manager) sources so Ubuntu/Debian knows where to get Node.js 20.x binaries from NodeSource.
  • Basically, it adds the NodeSource repo to /etc/apt/sources.list.d/.

3. && sudo apt-get install -y nodejs

  • && runs the next command only if the previous one succeeded.
  • Installs the official Node.js 20.x package (plus npm) from the NodeSource repo.
  • -y automatically answers “yes” to prompts.


Now, the difference

1. First command:

  • Installs NVM (Node Version Manager).
  • NVM lets you install and switch between multiple Node.js versions inside your user’s home directory.
  • More flexible for developers who need different Node.js versions per project.
  • Normally installed without sudo, just for your user.

2. Second command:

  • Installs Node.js 20.x directly using the system package manager.
  • Node.js is installed system-wide, same version for all users.
  • Easier for servers or production environments where you just want one stable Node.js version.

Quick comparison:

FeatureFirst command (NVM)Second command (NodeSource)
Tool installedNVM (manager)Node.js 20.x (direct)
ScopePer user (in ~/.nvm)System-wide (/usr/bin/node)
FlexibilityMultiple versions, switch anytimeFixed version from apt repo
Best forDevelopers, multi-version projectsServers, production setups

So the first command sets up a version manager, while the second command installs a specific Node.js version system-wide.


Let’s build a safe workflow depending on whether you’re:

  • A developer working on multiple projects.
  • An admin setting up a server/production environment.

1. For Developers (local machine, coding projects)

You often need different Node.js versions per project.

Use NVM (Node Version Manager).

bash

# Install NVM (no sudo!)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

Then reload your shell:

bash

source ~/.bashrc   # or ~/.zshrc

Install Node.js versions as needed:

bash

nvm install 20      # latest Node.js 20
nvm install 18      # also Node.js 18
nvm use 20          # switch to Node.js 20
nvm use 18          # switch to Node.js 18

Benefits:

  • Each project can use its own Node.js version.
  • No need for root privileges.
  • Safer, since you’re not modifying system-wide binaries.

2. For Servers / Production (stable deployments)

On servers, you usually want one stable Node.js version for all apps.

Use NodeSource repo (APT).

bash

# Add NodeSource repo for Node.js 20
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

# Install Node.js system-wide
sudo apt-get install -y nodejs

Check version:

bash

node -v
npm -v

Benefits:

  • Node.js available to all users.
  • Managed by APT → you can update with sudo apt-get upgrade.
  • Less overhead (no multiple versions floating around).


Golden Rule

  • Development (local) → Use NVM.
  • Production (server) → Use NodeSource APT install.

That way:

  • You get flexibility on your dev machine.
  • You get stability and easier system maintenance on servers.

By ice

Leave a Reply

Your email address will not be published. Required fields are marked *