How to Install Node.js on Ubuntu

  • Post category:Ubuntu

Node.js has become an essential tool for modern web development, enabling developers to run JavaScript code server-side. In this guide, we’ll explore how to install Node.js on Ubuntu using three common methods: APT (default package manager), NodeSource PPA (for specific versions), and NVM (Node Version Manager) for managing multiple versions of Node.js.

Method 1: Installing Node.js via APT

This is the easiest method, as Ubuntu includes Node.js in its official repositories.

Step 1: Open Terminal

Press Ctrl + Alt + T to open your terminal.

Step 2: Update Package Repository

Ensure your package list is updated:

sudo apt update

Step 3: Install Node.js

To install Node.js, run:

sudo apt install nodejs

Step 4: Install npm (Node Package Manager)

Install npm using the following command:

sudo apt install npm

Step 5: Verify Installation

After installation, check if Node.js and npm are correctly installed:

node -v
npm -v

You should see the installed versions displayed in the terminal.

Method 2: Installing Node.js via NodeSource PPA

If you need a specific version of Node.js (e.g., 16.x or 18.x), using NodeSource PPA is the recommended method.

Step 1: Open Terminal

As usual, press Ctrl + Alt + T to open the terminal.

Step 2: Add NodeSource PPA

Replace 18.x with your preferred Node.js version (e.g., 16.x, 20.x):

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

Step 3: Install Node.js

Run the following command to install Node.js:

sudo apt install nodejs

Step 4: Verify Installation

Check the version of Node.js to ensure the correct version is installed:

node -v

Method 3: Installing Node.js Using NVM (Node Version Manager)

NVM is the ideal choice if you want to manage multiple versions of Node.js on your system.

Step 1: Open Terminal

Open the terminal as before.

Step 2: Install NVM

Install NVM by running:

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

Step 3: Activate NVM

Load NVM into your current terminal session:

source ~/.nvm/nvm.sh

Step 4: Install the Latest LTS Version of Node.js

Once NVM is installed, you can install the latest LTS (Long Term Support) version of Node.js:

nvm install --lts

Step 5: Verify Installation

Check the installed version of Node.js to confirm the installation:

node -v

Conclusion

Whether you prefer the convenience of APT, need to install a specific version with NodeSource PPA, or want to manage multiple Node.js versions with NVM, this guide has you covered. Choose the installation method that best suits your workflow and start developing with Node.js on your Ubuntu system today!

For more tutorials on web development, visit CodeAllow.