19.6 C
New York

How to Install Node.js and npm on Ubuntu 20.04: A Comprehensive Guide

Published:

Node.js, a versatile JavaScript runtime environment based on Chrome’s V8 engine, is widely used for both server-side and full-stack web development. npm, Node.js’s default package manager, is integral for managing Node.js packages. In this guide, we will explore three methods to install Node.js and npm on Ubuntu 20.04, catering to different needs and preferences.

Method 1: Installing Node.js and npm from NodeSource

NodeSource, an enterprise-grade Node support company, maintains an APT repository with various Node.js versions. This method is ideal if you need a specific version of Node.js not available in the default Ubuntu repositories.

  1. Update Packages and Install Dependencies:
   sudo apt update
   sudo apt install ca-certificates curl gnupg
  1. Import NodeSource Repository GPG Key:
   sudo mkdir -p /etc/apt/keyrings
   curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
  1. Add NodeSource Repository:
    For Node.js version 20.x, execute:
   NODE_MAJOR=20
   echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
   sudo apt update
  1. Install Node.js and npm:
   sudo apt install nodejs

Verify the installation:

   node --version
   npm --version

For compiling native addons from npm:

   sudo apt install build-essential

Method 2: Using NVM (Node Version Manager)

NVM allows you to manage multiple Node.js versions. It’s particularly useful for development environments.

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

Reopen your terminal or run the commands provided by the installation script to add NVM to your PATH.

  1. Verify NVM Installation:
   nvm --version
  1. Install Node.js Versions:
    Install the latest Node.js version:
   nvm install node

For specific versions, such as the latest LTS or version 18.10.0:

   nvm install --lts
   nvm install 18.10.0

List installed Node.js versions:

   nvm ls

Switch between installed Node.js versions:

   nvm use 18.10.0

Set a default Node.js version:

   nvm alias default 18.10.0

Method 3: Installing from Ubuntu Repositories

The Ubuntu repository includes Node.js, but it might be an older version.

  1. Install Node.js and npm:
   sudo apt update
   sudo apt install nodejs npm

Verify the installation:

   nodejs --version

Conclusion

We’ve outlined three methods to install Node.js and npm on Ubuntu 20.04: using NodeSource for specific versions, NVM for managing multiple versions, and the default Ubuntu repositories for ease of installation. The choice depends on your specific needs, such as version requirements and environment setup. For managing application dependencies with Yarn, refer to a tutorial on installing and using Yarn on Ubuntu 20.04. Feel free to comment for any queries or assistance.

Related articles

Recent articles