New users get $100 in free credits for 30 daysGet Started

Introducing local edge computing in 5 new African citiesLearn More

Join our webinar: Scaling your startup on African infrastructureRegister Now

How to Install Node.js and npm on Linux, Windows, and macOS

Alex Rodriguez
Alex Rodriguez
December 5, 2024
8 min read
Node.jsnpmJavaScriptDevelopmentBeginner

What is Node.js?

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows you to run JavaScript on the server side, enabling full-stack JavaScript development. Node.js comes with npm (Node Package Manager), which is the world's largest software registry.

Why Use Node.js?
  • Fast: Built on Google Chrome's V8 JavaScript engine
  • Scalable: Event-driven, non-blocking I/O model
  • Popular: Large ecosystem with millions of packages
  • Versatile: Build web servers, APIs, desktop apps, and more

Prerequisites

Before installing Node.js, make sure you have:

  • Administrative access to your computer
  • An active internet connection
  • Basic familiarity with command line interface

Installing on Linux

There are several ways to install Node.js on Linux. We'll cover the most common methods.

Method 1: Using Package Manager (Recommended)

Ubuntu/Debian

Terminal
# Update package index
sudo apt update

# Install Node.js and npm
sudo apt install nodejs npm

# Install build tools (optional but recommended)
sudo apt install build-essential

CentOS/RHEL/Fedora

Terminal
# For CentOS/RHEL 7 and older
sudo yum install nodejs npm

# For CentOS/RHEL 8+ and Fedora
sudo dnf install nodejs npm

Method 2: Using NodeSource Repository

For the latest version, use the NodeSource repository:

Terminal
# Download and run the setup script for Node.js 20.x
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

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

Method 3: Using Snap

Terminal
sudo snap install node --classic

Installing on Windows

The easiest way to install Node.js on Windows is to download the installer from the official website.

  1. Download the Installer

    Visit nodejs.organd download the Windows Installer (.msi) for the LTS version.

  2. Run the Installer

    Double-click the downloaded .msi file and follow the installation wizard. Make sure to check the box that says "Automatically install the necessary tools."

  3. Complete Installation

    The installer will automatically add Node.js and npm to your PATH. You may need to restart your computer for the changes to take effect.

Alternative: Using Chocolatey

If you have Chocolatey installed, you can use it to install Node.js:

PowerShell (Admin)
choco install nodejs

Alternative: Using Winget

PowerShell
winget install OpenJS.NodeJS

Installing on macOS

There are several ways to install Node.js on macOS. Here are the most popular methods:

Method 1: Using Homebrew (Recommended)

If you have Homebrew installed:

Terminal
# Install Node.js and npm
brew install node

Method 2: Download from Official Website

Visit nodejs.organd download the macOS Installer (.pkg) for the LTS version. Run the installer and follow the prompts.

Method 3: Using MacPorts

Terminal
sudo port install nodejs18 +universal

Verify Installation

After installing Node.js, verify that it's working correctly by checking the versions:

Terminal
# Check Node.js version
node --version

# Check npm version
npm --version

You should see output similar to:

Output
v20.10.0
10.2.3

Test Node.js

Create a simple test file to ensure Node.js is working:

Terminal
echo "console.log('Hello, Node.js!');" > test.js
node test.js

You should see "Hello, Node.js!" printed to the console.

Version Management

It's often useful to manage multiple versions of Node.js for different projects. Here are some popular version managers:

NVM (Node Version Manager)

NVM is the most popular Node.js version manager for Linux and macOS:

Terminal
# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

# Reload your shell configuration
source ~/.bashrc

# Install the latest LTS version of Node.js
nvm install --lts

# Use a specific version
nvm use 18.19.0

# List installed versions
nvm list

# Set default version
nvm alias default 20.10.0

NVM for Windows

For Windows users, there's a separate NVM implementation:

  1. Download nvm-windows from GitHub
  2. Run the installer
  3. Use similar commands as above

Volta (Alternative)

Volta is a modern alternative that works on all platforms:

Terminal
# Install Volta (Linux/macOS)
curl https://get.volta.sh | bash

# Install Node.js
volta install node

# Pin a specific version for a project
volta pin node@18

Your First Node.js Project

Now that Node.js is installed, let's create a simple project to get you started.

  1. Create Project Directory

    Terminal
    mkdir my-first-node-app
    cd my-first-node-app
  2. Initialize npm Project

    Terminal
    npm init -y

    This creates a package.json file with default settings.

  3. Create a Simple Server

    Create an index.js file with a basic HTTP server:

    index.js
    const http = require('http');
    
    const server = http.createServer((req, res) => {
      res.writeHead(200, { 'Content-Type': 'text/html' });
      res.end('<h1>Hello, Node.js!</h1><p>Your server is running successfully.</p>');
    });
    
    const PORT = process.env.PORT || 3000;
    server.listen(PORT, () => {
      console.log(`Server running at http://localhost:${PORT}/`);
    });
  4. Run Your Server

    Terminal
    node index.js

    Open your browser and navigate to http://localhost:3000to see your server in action!

  5. Install a Package

    Let's install Express.js, a popular web framework:

    Terminal
    npm install express
Congratulations!

You've successfully installed Node.js and created your first project. You're now ready to start building JavaScript applications!

Conclusion

You've successfully installed Node.js and npm on your system. Node.js opens up a world of possibilities for JavaScript development, from web servers to desktop applications and command-line tools.

Next steps you might consider:

  • Learn about npm packages and the Node.js ecosystem
  • Explore popular frameworks like Express.js, Next.js, or NestJS
  • Build REST APIs and web applications
  • Learn about asynchronous programming with Promises and async/await
  • Explore Node.js modules and the CommonJS/ES6 module systems

Useful npm Commands

Terminal
# Install a package locally
npm install package-name

# Install a package globally
npm install -g package-name

# Install development dependencies
npm install --save-dev package-name

# Update packages
npm update

# List installed packages
npm list

# Run scripts defined in package.json
npm run script-name

# Check for outdated packages
npm outdated

About the Author

Alex Rodriguez
Alex Rodriguez
Full-stack JavaScript developer with 6+ years of experience building web applications with Node.js and React.

Deploy Your Node.js App

Host your Node.js applications on our cloud platform with automatic scaling.

Explore Hosting