Skip to content

Ubuntu - Node.js

Node.js is a popular JavaScript runtime environment for building server-side and networking applications. npm (Node Package Manager) is its default package manager. The recommended way to install Node.js and npm on Ubuntu is using Node Version Manager (NVM), which allows you to install and manage multiple Node.js versions.

Step 1: Install NVM (Node Version Manager)

Section titled “Step 1: Install NVM (Node Version Manager)”

NVM is a script that needs to be downloaded and run. Open your terminal and use curl or wget to download and execute the NVM installation script. Always check the official NVM GitHub repository (github.com/nvm-sh/nvm) for the latest installation command.

A typical command looks like this (version number may vary):

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

After running the script, you may need to close and reopen your terminal or source your shell configuration file (e.g., source ~/.bashrc, source ~/.zshrc) for the nvm command to become available.

Verify NVM installation:

nvm --version

With NVM installed, you can now install Node.js. To install the latest LTS (Long-Term Support) version of Node.js (recommended for most users):

nvm install --lts

This command will download and install the latest LTS version of Node.js, and npm will be included automatically. NVM will also set this version as the default.

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

nvm install 18.17.0 # Example: installs Node.js version 18.17.0

You can list available Node.js versions with nvm ls-remote and switch between installed versions using nvm use <version>.

Step 3: Verify Node.js and npm Installation

Section titled “Step 3: Verify Node.js and npm Installation”

Check the installed versions of Node.js and npm:

node -v
npm -v

This should output the versions you installed, for example:

v18.17.1
9.6.7

You are now ready to develop Node.js applications on your Ubuntu system. Using NVM provides flexibility in managing different Node.js versions for various projects.

Alternative: Using NodeSource Repositories

Section titled “Alternative: Using NodeSource Repositories”

If you prefer not to use NVM, you can install Node.js from the NodeSource repositories, which provide more up-to-date versions than the default Ubuntu repositories. Instructions can be found on the NodeSource GitHub page. This typically involves adding their PPA and then using apt to install nodejs.