How to Install and Use npm on Ubuntu: A Complete Beginner’s Guide (2025)

目次

1. Introduction

Why Use npm on Ubuntu?

One of the essential tools for both frontend and backend development is npm (Node Package Manager). Widely used as the default package manager for Node.js, npm makes it easy to install and manage JavaScript libraries and tools.

Using npm on Ubuntu takes advantage of Linux’s speed and flexible package management, significantly improving development efficiency. Ubuntu is a popular distribution among developers, used for everything from server operations to local development environments.

When working with Node.js-based frameworks such as Vue.js, React, or Next.js, it’s common to use npm for package management. Setting up these tools on Ubuntu provides a more stable development environment with fewer issues compared to Windows or macOS.

Purpose of This Article

This article will guide you through installing npm on Ubuntu and mastering its basic usage. It’s especially helpful for the following readers:

  • Developers who are new to Ubuntu
  • Anyone struggling to set up Node.js and npm
  • Those who want to learn npm usage in a structured way

We’ll cover multiple installation methods, discussing the pros and cons of each. We’ll also address common errors, troubleshooting tips, and a collection of useful commands to help you get up and running with npm on Ubuntu smoothly.

年収訴求

2. How to Install Node.js and npm on Ubuntu

To use npm on Ubuntu, you first need to install Node.js. This is because npm is bundled with Node.js, so installing Node.js automatically makes npm available.

In this section, we’ll go over three main methods to install Node.js and npm on Ubuntu. Each method has its own characteristics, so it’s important to choose one that suits your development style and goals.

Method 1: Using Ubuntu’s Official Repository

Steps

Node.js is available in Ubuntu’s default repositories. This method is the easiest and most beginner-friendly.

sudo apt update
sudo apt install nodejs npm

After installation, you can check the installed versions with the following commands:

node -v
npm -v

Pros

  • Simple commands, easy to follow
  • Safe and stable version provided

Cons

  • The version of Node.js/npm may be outdated, so some newer features might not be available

Method 2: Using the NodeSource PPA

You can get newer versions of Node.js and npm by using the NodeSource repository, which is closely aligned with official Node.js support.

Steps (Example: Install Node.js 18.x)

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

npm will be installed automatically along with Node.js.

Pros

  • Access to newer, yet stable versions
  • Simple to install and works well with Ubuntu

Cons

  • As with any PPA, may require managing system dependencies manually

Method 3: Using nvm (Node Version Manager)

If you want to switch between multiple Node.js versions, using nvm is the most flexible and convenient method.

Steps

First, install nvm:

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

Then reload your shell and install Node.js using nvm:

source ~/.bashrc  # or ~/.zshrc
nvm install 18
nvm use 18

npm will also be installed automatically with Node.js.

Pros

  • Easily switch between different Node.js versions
  • Set version per project for better compatibility
  • Does not affect the system-wide environment, safe to use

Cons

  • A bit more complex to set up compared to other methods
  • You need to modify your shell configuration file

Which Method Should You Choose?

MethodDifficultyVersion FreshnessFlexibilityRecommended For
Official Repository★☆☆△ (Older)×Beginners, quick test runs
NodeSource★★☆○ (Fairly new)General developers
nvm★★★◎ (Fully customizable)Advanced users, multiple projects

If you plan to use npm on Ubuntu for long-term development, nvm is the most recommended method. However, if you want to get started quickly, the NodeSource PPA is also a solid choice.

3. Basic Usage of npm

Once you’ve set up Node.js and npm on Ubuntu, the next step is to start managing packages using npm. npm is a powerful tool used to install, update, and remove JavaScript packages.

In this section, we’ll introduce the most commonly used npm commands to help you get started.

Installing Packages

Local Installation

Packages that are only used within a specific project should be installed locally. This is the standard approach and installs the package into the node_modules directory, with details recorded in package.json.

npm install package-name

Example: To install axios

npm install axios

Only scripts within the same project will be able to use this package.

Global Installation

Tools that are used system-wide, like CLI apps, should be installed globally.

npm install -g package-name

Example: To install http-server globally

sudo npm install -g http-server

On Ubuntu, you may need to use sudo when using the -g option.

Uninstalling Packages

If a package is no longer needed, you can remove it using the following commands.

Remove a Local Package

npm uninstall package-name

Remove a Global Package

sudo npm uninstall -g package-name

Updating Packages

To update packages to the latest version, you can use these commands.

Update a Specific Package

npm update package-name

Update All Dependencies at Once

npm update

Note that this updates packages only within the version range specified in package.json, so always double-check version numbers.

Installing Development Packages (–save-dev)

Packages used only in development environments—such as testing or build tools—should be installed with the --save-dev option.

npm install --save-dev package-name

Example: Install jest as a development dependency

npm install --save-dev jest

This will add the package to the devDependencies section of your package.json.

Viewing Installed Packages

List Local Packages

npm list

List Global Packages

npm list -g --depth=0

By specifying --depth=0, only top-level packages are shown, making the list easier to read.

Managing Project Dependencies with package.json

The package.json file, located at the root of your project, plays a key role in npm workflows. It stores package names, versions, and custom scripts, serving as the project’s configuration file.

You can create a package.json file using the following command:

npm init

This runs an interactive setup. If you prefer to skip the prompts and use default values:

npm init -y

This will auto-generate a package.json file with default settings.

4. Common Issues and How to Fix Them

When using npm on Ubuntu, you may occasionally encounter errors or unexpected behavior. This section covers some of the most common problems—especially those beginners often face—and how to solve them.

Permission Errors

Symptoms

EACCES: permission denied

This error commonly appears when attempting a global npm install.

Cause

This happens when the current user does not have write access to the directory where npm is trying to install packages. On Ubuntu, for security reasons, system directories like /usr/lib/node_modules require sudo to modify.

Solutions

  • Run the install command with sudo:
sudo npm install -g package-name
  • Or change the global install directory to your user directory to avoid using sudo:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'

Then, add the following to your ~/.bashrc or ~/.profile to update your PATH:

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

To apply the changes, restart your terminal or run:

source ~/.bashrc

npm Command Not Found

Symptoms

command not found: npm

Even after installing, the terminal might not recognize the npm command.

Causes

  • Node.js and npm were not installed correctly
  • The binary location is not included in your PATH

Solutions

First, check if the npm binary is available:

which npm

If nothing is returned, reinstall npm or verify that your environment variables are set properly. If you’re using nvm, make sure your shell configuration file (e.g., .bashrc or .zshrc) includes the nvm initialization code:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

Package Won’t Install / Version Conflict

Symptoms

  • Installing a package results in version mismatch errors
  • npm install triggers a flood of warnings

Cause

npm displays errors or warnings when package dependencies conflict. This often occurs in older projects where deprecated packages are still listed.

Solutions

  • Try installing the latest version:
npm install package-name@latest
  • Check the package’s dependency tree:
npm ls package-name
  • Force install (not recommended unless necessary):
npm install --legacy-peer-deps
  • Clear your project and reinstall dependencies:
rm -rf node_modules package-lock.json
npm install

Other Useful Debugging Tips

  • npm doctor: Checks your system setup and environment
npm doctor
  • npm audit: Scans for security issues and suggests fixes
npm audit
npm audit fix

Most npm errors on Ubuntu are displayed in English, which may feel overwhelming at first. But by reading the messages carefully and responding step-by-step, you can usually resolve the issue quickly.

5. Frequently Asked Questions (FAQ)

This section answers common questions that new Ubuntu + npm users often have. Use it to prevent issues before they occur and to build a smoother development environment.

Q1. How can I update npm to the latest version on Ubuntu?

A1.

You can update npm to the latest version using the following command:

sudo npm install -g npm@latest

If you’re using nvm, you don’t need sudo:

npm install -g npm@latest

To verify the installed version:

npm -v

Q2. What’s the difference between global and local npm installations?

A2.

  • Local installation:
  • Used for managing dependencies within a specific project
  • Packages are installed in the node_modules directory
  • Easily shareable with teammates via package.json
  • Global installation:
  • Used for tools you want accessible system-wide (e.g., CLI tools)
  • Packages are usually installed in /usr/lib/node_modules on Ubuntu
  • sudo may be required for installation

Q3. What are the benefits of using nvm?

A3.

nvm (Node Version Manager) lets you switch between multiple Node.js versions easily. Here are the benefits:

  • Ideal for managing different versions for different projects
  • Does not affect system-wide settings, making it safer
  • npm is also managed per Node version, offering flexible environment setups

Q4. What should I do if npm dependencies get messed up?

A4.

First, try deleting node_modules and package-lock.json, then reinstall everything:

rm -rf node_modules package-lock.json
npm install

If that doesn’t help, try using the npm ci command for a clean reinstall (recommended for CI/CD pipelines).

Q5. Is it okay if I see “WARN” or “audit” in npm output?

A5.

Warnings (WARN) are not fatal, but they do signal potential issues such as outdated dependencies or deprecated features.

For security-related warnings, you can try fixing them automatically using:

npm audit fix

Ideally, review the changes and commit them with Git to maintain a safe and stable codebase.

Q6. What kind of projects can I build with npm on Ubuntu?

A6.

npm is your gateway into the JavaScript ecosystem. Here are just a few things you can build on Ubuntu:

  • Frontend apps using frameworks like React, Vue, or Svelte
  • Static site generators like Next.js or Nuxt
  • Backend APIs using Express or NestJS
  • Custom CLI tools
  • Testing environments with Jest or Mocha

By using npm on Ubuntu, you can leverage the full power of the open-source ecosystem to build efficient, scalable, and modern applications.

6. Summary: Mastering npm on Ubuntu

In this article, we covered everything you need to know to install and start using npm effectively on Ubuntu. From installation methods to basic commands, here’s a quick recap of the key points.

Key Takeaways

  • What is npm? – A package manager bundled with Node.js that significantly boosts development productivity
  • How to install npm on Ubuntu: – You can install it via the official repository, NodeSource PPA, or nvm depending on your needs
  • Core features: – Easy commands for installing, removing, updating, and managing dependencies
  • Troubleshooting: – Tips for resolving permission errors, version conflicts, and more
  • FAQ section: – Clear answers to common questions faced during real-world development

A Note to Beginners

Ubuntu and npm make a great pair for modern web development. While you may face some errors or confusion at the beginning, working through those issues will deepen your understanding and help you grow as a developer.

The most important thing is to try things out hands-on. Running each command, reading the output, and learning from mistakes will quickly build your confidence.

That wraps up our guide to using npm on Ubuntu. We hope it helps you build a solid development environment and accelerate your skill-building journey.

We’ll continue sharing more Linux tips and frontend development tutorials, so feel free to bookmark this site or share it on social media!