How to Install npm on Ubuntu: A Step-by-Step Guide

Learn to install npm on Ubuntu using apt, apt-get, and the command line. Troubleshoot common issues and explore helpful tools like npm-check-updates. Enhance your JavaScript development workflow on Ubuntu with the power of npm.

Updated: 05 Dec, 23 by Susith Nonis 19 Min

List of content you will read in this article:

If you're a developer or someone who frequently works with JavaScript (JS), chances are you've encountered npm (Node Package Manager). npm is a powerful tool that allows you to easily manage and install JavaScript packages and dependencies for your projects. In this article, we'll walk you through installing npm on Ubuntu, one of the most popular Linux distributions. You can also learn How to Install Python on Ubuntu.

Install Node.js and npm from the Ubuntu

Before we begin, ensure you have a working Ubuntu system and a user account with sudo privileges. This will enable you to install packages and make system-wide changes.

Step 1: Update Package Lists

The first step is to update the package lists on your Ubuntu system. Open a terminal and enter the following command:

sudo apt update

This command will fetch the latest information about available packages and their versions from the Ubuntu repositories.

Step 2: Install Node.js

npm is bundled with Node.js, so you'll need to install Node.js first. Fortunately, Node.js is available in the default Ubuntu repositories. To install it, run the following command:

sudo apt install nodejs

This command will install Node.js and its dependencies on your system. During installation, you may be prompted to confirm your action by typing 'Y' and pressing Enter.

Step 3: Verify the Installation

Once the installation is complete, you can verify that Node.js and npm are installed correctly. To check the version of Node.js, run the following command:

node --version

This command will display the installed version of Node.js. Similarly, to check the version of npm, enter the following command:

npm --version

If both commands return the version numbers without errors, you have successfully installed Node.js and npm on your Ubuntu system.

Step 4: Manage npm Versions (Optional)

Sometimes, you may need to manage multiple versions of npm on your system. Fortunately, tools are available to help you switch between different npm versions. One such tool is `nvm` (Node Version Manager). To install `nvm`, you can use the following commands:

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

This command will download and execute the `install.sh` script, which will install `nvm` on your system. After the installation is complete, close and reopen your terminal, or run the following command to start using `nvm`:

source ~/.bashrc

Now, you can install and manage different versions of npm using the `nvm` command. For example, to install the latest stable version of npm, run:

nvm install stable

To switch between different npm versions, use the following command:

nvm use <version>

Replace `<version>` with the desired version of npm.

Step 5: Install Global Packages (Optional)

Once you have npm installed, you can start installing global packages that can be used across different projects. To install a global package, use the following command:

sudo npm install -g <package-name>

Replace `<package-name>` with the package name you want to install. For example, to install the popular `webpack` package, run:

sudo npm install -g webpack

Step 6: Update the npm

Regularly updating npm is important to get the latest features, bug fixes, and security patches. To update npm to the latest version, use the following command:

sudo npm install -g npm@latest

This command will update npm to the latest stable version available.

Installing npm on Ubuntu 22.04 (Focal Fossa) and 20.04 (Bionic Beaver) is similar to the general installation steps outlined in the previous sections. However, there are slight differences in the package availability and versioning. Let's explore how to install npm on these specific Ubuntu versions.

Step 1: Update Package Lists

Begin by updating the package lists on your Ubuntu 22.04 or 20.04 system. Launch the terminal and execute the following command:

sudo apt update

This command will fetch the latest information about available packages and their versions from the Ubuntu repositories.

Step 2: Install Node.js

To install Node.js on Ubuntu 22.04 and 20.04, use the following command:

sudo apt install nodejs

This command will install the Node.js runtime environment and its dependencies on your system.

Step 3: Verify the Installation

Once the installation is complete, you can verify that Node.js and npm are installed correctly. Open the terminal and check the version of Node.js by running:

node --version

To verify the npm version, execute the following command:

npm --version

If both commands display the version numbers without errors, you have successfully installed Node.js and npm on your Ubuntu 22.04 or 20.04 system.

Step 4: Optional Steps

If you want to manage multiple npm versions or install global packages, you can follow the optional steps mentioned in the previous sections of this article. The process remains the same regardless of the Ubuntu version you are using.

While installing npm on Ubuntu is usually a smooth process, there may be instances where you encounter issues or errors during the installation. Here, we will address some common problems and provide solutions to help you overcome them.

1. Missing Dependencies

If you encounter errors related to missing dependencies during the installation process, you can try running the following command to install the required dependencies:

sudo apt install build-essential

This command installs essential build tools and libraries often needed for compiling and installing certain npm packages.

2. Permission Errors

Sometimes, you may encounter permission errors when installing npm packages globally or using sudo with npm. To resolve this, avoiding using sudo with npm commands is recommended. Instead, you can configure npm to install global packages in your user directory by executing the following command:

npm config set prefix ~/.npm-global

After running this command, make sure to add the following line to your ~/.bashrc or ~/.profile file:

export PATH="$HOME/.npm-global/bin:$PATH"

Save the file and then run the command `source ~/.bashrc` or `source ~/.profile` to apply the changes. This ensures that npm can be used globally without requiring sudo privileges.

3. Proxy Configuration

If you are behind a proxy server, you may encounter connection issues while installing npm packages. To configure npm to work with your proxy, you can use the following commands, replacing `<proxy-url>` with the URL of your proxy server:

npm config set proxy <proxy-url>

npm config set https-proxy <proxy-url>

After setting the proxy configuration, npm should be able to connect to the internet through the specified proxy.

4. Clearing Cache

In some cases, issues can arise due to a corrupted npm cache. To clear the npm cache, run the following command:

npm cache clean --force

This command clears the npm cache and helps resolve any caching-related issues that may be affecting the installation process.

If you prefer a command-line approach to installing npm on Ubuntu, you can follow these steps to set it up quickly and efficiently.

Step 1: Update Package Lists

Begin by updating the package lists on your Ubuntu system. Open the terminal and execute the following command:

sudo apt update

This command fetches the latest information about available packages and their versions from the Ubuntu repositories.

Step 2: Install Node.js and npm

To install Node.js and npm in one go, you can use the following command:

sudo apt install nodejs npm

This command installs Node.js and npm and their respective dependencies on your system.

Step 3: Verify the Installation

Once the installation is complete, verify that Node.js and npm are installed correctly. Open the terminal and check the version of Node.js by running:

node --version

To verify the npm version, execute the following command:

npm --version

If both commands display the version numbers without errors, you have successfully installed Node.js and npm on your Ubuntu system via the command line.

Step 4: Optional Steps

If you wish to manage multiple npm versions or install global packages, follow the optional steps mentioned in this article. The process remains the same, regardless of whether you installed npm via the command line or other methods.

Installing Node.js on Ubuntu using the apt package manager is a convenient method that ensures you have the latest stable version available in the official Ubuntu repositories. Here's how you can install Node.js using apt.

Step 1: Update Package Lists

Before installing Node.js, updating the package lists on your Ubuntu system is essential. Open a terminal and execute the following command:

sudo apt update

This command fetches the latest information about available packages and their versions from the Ubuntu repositories.

Step 2: Install Node.js

To install Node.js using apt, use the following command:

sudo apt install nodejs

This command installs the Node.js runtime environment and its dependencies on your system. During installation, you may be prompted to confirm your action by typing 'Y' and pressing Enter.

Step 3: Verify the Installation

Once the installation is complete, you can verify that Node.js is installed correctly. Open a terminal and run the following command to check the version of Node.js:

node --version

If the command displays the installed version of Node.js without any errors, you have successfully installed Node.js using apt on your Ubuntu system.

Step 4: Install npm (Optional)

By default, the Node.js installation from the Ubuntu repositories includes npm (Node Package Manager). However, if you find that npm is not installed, you can install it separately using the following command:

sudo apt install npm

This command installs npm and its dependencies on your system.

Step 5: Verify the npm Installation (Optional)

To verify that npm is installed correctly, open a terminal and run the following command:

npm --version

If the command displays the installed version of npm without errors, you have successfully installed npm on your Ubuntu system.

For Ubuntu 16.04 (Xenial Xerus), the apt-get package manager is commonly used to install npm. While the process is similar to the apt installation method, there are a few differences. Let's walk through the steps to install npm on Ubuntu 16.04 using apt-get.

Step 1: Update Package Lists

Before installing, update the package lists on your Ubuntu 16.04 system. Open a terminal and execute the following command:

sudo apt-get update

This command fetches the latest information about available packages and their versions from the Ubuntu repositories.

Step 2: Install Node.js and npm

To install Node.js and npm using apt-get on Ubuntu 16.04, use the following command:

sudo apt-get install nodejs npm

This command installs both Node.js and npm, along with their respective dependencies, on your system.

Step 3: Verify the Installation

Once the installation is complete, verify that Node.js and npm are installed correctly. Open a terminal and check the version of Node.js by running:

node --version

To verify the npm version, execute the following command:

npm --version

If both commands display the version numbers without errors, you have successfully installed Node.js and npm on your Ubuntu 16.04 system using apt-get.

Step 4: Optional Step

If you wish to manage multiple npm versions or install global packages, follow the optional steps mentioned in this article. The process remains the same, regardless of the Ubuntu version or the package manager.

🚀 Elevate your online presence to new heights with Monovm VPS! 🌐💻 Unleash the power of virtualization for blazing speed and control. 🔥🚀

Linux  VPS
Need Affordable VPS Hosting

Starting From $5.99/Monthly

🏅 Fast Delivery
🏅 25+ Data Centers

If you encounter an error while installing the bcrypt package through npm on Ubuntu, it is likely due to missing system dependencies required by bcrypt. The bcrypt package relies on native C++ code and needs some prerequisites to compile successfully. Here's how you can troubleshoot and resolve this issue.

Step 1: Install Required Dependencies

To resolve the bcrypt installation error, you must install the necessary system dependencies on your Ubuntu system. Open a terminal and execute the following command:

sudo apt-get install build-essential python

This command installs the build-essential package, which includes the essential build tools and libraries required for compiling native code, and Python, which is needed by bcrypt during the installation process.

Step 2: Retry the Installation

After installing the dependencies, you can retry installing bcrypt through npm. In the terminal, run the following command:

npm install bcrypt

This command will attempt to install the bcrypt package again.

Step 3: Clear npm Cache (Optional)

If you continue to encounter issues, it may be helpful to clear the npm cache. Run the following command in the terminal:

npm cache clean --force

This command clears the npm cache, sometimes resolving caching-related problems affecting package installations.

Step 4: Additional Troubleshooting

If the error persists, there may be other factors causing the issue. Here are a few additional troubleshooting steps you can try:

  • - Ensure that you have the latest version of npm installed by running `npm install -g npm@latest`.
  • - Update your Node.js version to the latest stable release.
  • - Verify that your system meets the minimum requirements for bcrypt and Node.js.
  • - Check for any conflicting packages or global installations that may interfere with the installation process.

If none of the above steps resolves the issue, consider searching for specific solutions related to your Ubuntu version or posting a question on relevant forums or community platforms where experts can provide further assistance.

If you receive a "npm install command not found" error on Ubuntu, it indicates that the npm executable is not recognized or not available in your system's PATH. This can happen due to various reasons. Here's how you can troubleshoot and resolve this issue.

Step 1: Check Node.js Installation

Before proceeding, verify that Node.js is installed correctly on your Ubuntu system. Open a terminal and run the following command:

node --version

If this command displays the installed version of Node.js without any errors, it means Node.js is installed properly. Otherwise, you'll need to install Node.js first.

Step 2: Reinstall npm

If npm is missing or unrecognized, you can reinstall it using the Node Version Manager (nvm) or the official npm installation script.

To reinstall npm using nvm, follow these steps:

  •  Install nvm by running the following command in your terminal:

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

  • Close and reopen your terminal, or run the following command to load nvm:

source ~/.bashrc

  •  Install the latest LTS version of Node.js and npm by executing the following command:

nvm install --lts

  • Verify that npm is installed by running the following:

npm --version

If npm is still not recognized, proceed to the next step.

Step 3: Update Environment Variables

Sometimes, the PATH variable may not be properly set to include the directory where npm is installed. To update the environment variables, follow these steps:

  • Open your terminal and run the following command to open the `.bashrc` file:

nano ~/.bashrc

  • Add the following line at the end of the file:

export PATH="$HOME/.npm-global/bin:$PATH"

  • Save the file by pressing `Ctrl + O`, then exit nano by pressing `Ctrl + X`.
  • Run the following command to apply the changes:

source ~/.bashrc

  • Verify that npm is now recognized by running:

npm --version

If npm is still not found, try restarting your terminal and repeating the steps.

Step 4: Verify Node.js and npm Installation

To ensure that Node.js and npm are installed correctly and accessible, you can execute the following commands in your terminal:

node --version

npm --version

If both commands display the respective version numbers without errors, you have resolved your Ubuntu system's "npm install command not found" issue.

npm-check-updates is a useful tool that allows you to check for and update outdated npm packages in your project. Here's how you can install and use npm-check-updates on Ubuntu.

Step 1: Install npm-check-updates

To install npm-check-updates, open a terminal and run the following command:

sudo npm install -g npm-check-updates

This command installs npm-check-updates globally on your Ubuntu system, making it available as a command-line tool.

Step 2: Run npm-check-updates

Once npm-check-updates is installed, navigate to your project directory in the terminal. To check for outdated packages in your project, execute the following command:

ncu

This command scans your project's package.json file and displays a list of outdated packages along with their available updates.

Step 3: Update Packages

To update your project's packages to their latest versions, use the following command:

ncu -u

This command updates the package.json file with the latest versions of the packages. After running this command, you can run `npm install` to install the updated packages.

Step 4: Additional Options

npm-check-updates provide additional options to customize its behavior. For example:

- You can pass their names as arguments to check for updates of only specific packages. For instance: `ncu package1 package2`.

- To update only devDependencies, use the `-D` flag: `ncu -u -D`.

- To update packages interactively, use the `-i` flag: `ncu -i`.

To explore more options, refer to the npm-check-updates documentation or run `ncu --help` in the terminal.

This article explored various methods to install and troubleshoot npm on Ubuntu. Whether you prefer using the apt or apt-get package managers, or the command line for installation, the steps provided will help you set up npm successfully on your Ubuntu system. We covered the installation process for different Ubuntu versions, including Ubuntu 22.04, 20.04, 16.04, and addressed common issues such as the "bcrypt" and "npm install command not found" errors. If you need Ubuntu VPS Hosting services, you can find them here.

Linux  VPS
Debian VPS Server

Starting From $5.99/Monthly

Following the installation instructions, updating package lists, and verifying the installation, you can ensure that Node.js and npm are properly installed on your Ubuntu system. We also discussed additional tools like npm-check-updates, which can be used to manage and update outdated packages in your projects.

Susith Nonis

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.

user monovm

Ena Fadel

2024, Aug, 24

This article provides a comprehensive and easy-to-follow guide for installing npm on Ubuntu. The step-by-step instructions make it accessible for both beginners and experienced developers. I appreciate the troubleshooting sections which address common issues like missing dependencies and permission errors. Also, the optional steps for managing npm versions offer great flexibility. Overall, this is an invaluable resource for anyone looking to set up npm on an Ubuntu system!

user monovm

Buford Jakubowski

2024, Oct, 24

Great guide! It's really handy to have a step-by-step walkthrough like this for installing npm on Ubuntu. Your detailed instructions cover all the essentials, and the troubleshooting section is a lifesaver. Many of us can run into snags when dealing with package managers, so knowing how to handle errors and manage multiple versions is super helpful. Definitely bookmarking this for future reference. Thanks for putting this together!

user monovm

Ms. Eva Osinski Jr.

2024, Oct, 24

Great guide on installing npm on Ubuntu! It's thorough and covers all the essential steps. I appreciate the detailed troubleshooting sectionβ€”it’s super helpful for beginners and those who might run into issues. Including commands for checking versions and managing multiple npm versions is a nice touch. This is an excellent resource for any developer working with JavaScript on Ubuntu. Thanks for sharing such valuable insights!