Here are two methods to install Node.js and npm (Node Package Manager) on Ubuntu:
Method 1: Using the Official Ubuntu Repositories
This method is the simplest and recommended approach for most users. The packages in the Ubuntu repositories might be slightly older versions, but they are well-tested and compatible with your Ubuntu system.
-
Update System Packages:
Bashsudo apt update
-
Install Node.js and npm:
Bashsudo apt install nodejs npm
This command will install the latest version of Node.js and npm available in the Ubuntu repositories.
Method 2: Using Node Version Manager (nvm)
This method offers more flexibility as it allows you to install and manage multiple Node.js versions on your system. It's recommended for developers who need to work with different project requirements.
-
Install curl (if not already installed):
Bashsudo apt install curl
-
Download and install the nvm installation script:
Bashcurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
Note: Replace
v0.39.3
with the latest nvm version number if needed (check the official nvm GitHub repository for updates: https://github.com/nvm-sh/nvm). -
Source the nvm configuration file (to make nvm commands available):
Bashsource ~/.bashrc
You might need to restart your terminal window for the changes to take effect.
-
List available Node.js versions:
Bashnvm ls-available
-
Install a specific Node.js version (e.g., 16.19.0):
Bashnvm install 16.19.0
-
Use the installed version (activate it):
Bashnvm use 16.19.0
You can verify the active Node.js version using
node -v
.
Verifying Installation:
Once you've installed Node.js and npm using either method, you can verify the installation by running the following commands:
node -v # This should print the installed Node.js version
npm -v # This should print the installed npm version