Install Node.js and npm using apt

Node.js is a JavaScript runtime that allows you to run JavaScript on the server side. npm is the default package manager for Node.js.

Step 1: Update package lists

First, update your package lists to ensure you have the latest information:

bash
sudo apt update

Step 2: Install Node.js and npm

Install Node.js and npm using the apt package manager:

bash
sudo apt install nodejs npm -y

This command installs both Node.js and npm together.

Step 3: Verify Node.js version

Check that Node.js was installed successfully:

bash
node -v

You should see output similar to v18.17.0.

Step 4: Verify npm version

Check that npm was installed successfully:

bash
npm -v

You should see output similar to 9.6.7.

Alternative: Install specific version

If you need a specific version of Node.js, you can:

  1. Install NodeSource repository:
bash
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
  1. Install Node.js:
bash
sudo apt-get install -y nodejs

Next Steps

Now that Node.js and npm are installed, you can:

  • Create new Node.js projects with npm init
  • Install packages with npm install
  • Run Node.js applications
  • Use npm scripts for automation