Outline of the Article
- What is npm?
- Why use npm with Ubuntu?
- Key features of npm
- Checking system requirements
- Updating Ubuntu packages
- Choosing an installation method
- Method 1: Ubuntu's default repository
- Method 2: NodeSource repository (recommended for current versions)
- Method 3: NVM (recommended for multiple versions)
- Verifying the installation
- Installing packages locally and globally
- Updating packages
- Uninstalling packages
- Initializing a new project
- Understanding package.json
- Defining dependencies in package.json
- Installing project dependencies
- Utilizing devDependencies for development purposes
- npm search
- npm info
- npm outdated
- npm audit
- Understanding the npm registry
- Publishing your own packages
- Common issues and solutions
- Utilizing npm support resources
- Benefits of using npm with Ubuntu
- Potential challenges and how to overcome them
Mastering Ubuntu: A Step-by-Step Guide on How to Install npm (2026 Update)
Introduction to Ubuntu and npm
Understanding the npm Package Manager
What is npm?
Npm is a powerful package manager that comes bundled with Node.js, the JavaScript runtime environment. It allows developers to install, share, and distribute packages, which can include libraries, frameworks, and tools. Npm plays a crucial role in the JavaScript ecosystem, enabling developers to create efficient and scalable applications.
Why use npm with Ubuntu?
By using npm with Ubuntu, developers gain access to an extensive collection of open-source packages. This vast repository of packages saves time and effort, as developers can reuse existing solutions instead of building everything from scratch. Additionally, npm simplifies the management of dependencies, making it easier to keep projects up to date.
Key features of npm
- Easy package installation: Npm provides a straightforward way to install packages globally or within specific projects.
- Version management: Developers can easily manage package versions and update them when needed.
- Script execution: Npm allows the execution of scripts defined in the package.json file, streamlining development tasks.
- Package publishing: Developers can publish their own packages to the npm registry for others to use.
Preparing Your System for npm Installation
Before installing npm on Ubuntu, you need to ensure that your system meets the necessary requirements.
Checking system requirements
Verify that your Ubuntu system meets the minimum system requirements for Node.js and npm. Check the Ubuntu documentation or the Node.js website for specific hardware and software prerequisites.
Updating Ubuntu packages
Before proceeding with npm installation, update all the existing packages on your Ubuntu system:
sudo apt updateChoosing an installation method
This is the part where the old advice of just running apt-get install npm falls short: Ubuntu's default repositories often ship a Node.js and npm version that's several major releases behind current. There are three real options in 2026, and which one you pick depends on your needs:
- Ubuntu's default repository — simplest, but usually an older Node.js/npm version. Fine for quick testing, not ideal for active development.
- NodeSource repository — installs a current, upstream-maintained Node.js line (npm is bundled automatically). Good for servers running one consistent version.
- NVM (Node Version Manager) — installs into your home directory, lets you install and switch between multiple Node.js versions per project. This is the option most working developers prefer.
Installing Node.js and npm on Ubuntu
Method 1: Ubuntu's default repository
The quickest option, though typically not the most current version:
sudo apt install -y nodejs npmMethod 2: NodeSource repository (recommended for a current, single version)
NodeSource packages bundle npm together with Node.js — don't also run apt install npm after this, or you'll end up with a conflicting npm version. Replace 22 below with whichever current LTS line you want:
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejsMethod 3: NVM (recommended for multiple versions or active development)
NVM installs into your home directory, so it avoids permission issues entirely and lets you switch Node.js versions per project:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.6/install.sh | bash
source ~/.bashrc
nvm install --ltsVerifying the installation
Whichever method you used, confirm both Node.js and npm are available:
node -v
npm -vIf both commands print a version number, the installation was successful.
Managing npm Packages
With npm installed, you can now explore its various package management capabilities.
Installing packages locally and globally
Npm allows you to install packages either locally (within a specific project) or globally (system-wide). Local installations are useful for project-specific dependencies, while global installations are suitable for tools or utilities used across multiple projects.
To install a package locally, navigate to your project's directory and use the following command:
npm install package_nameTo install a package globally, use the -g flag. Note: if you're using NVM (Method 3 above), you never need sudo for this, since packages install into your own home directory:
npm install -g package_nameUpdating packages
Keeping packages up to date is essential for security and performance reasons. To update a package to its latest version, use the following command:
npm update package_nameUninstalling packages
If you no longer need a specific package, you can uninstall it using the following command:
npm uninstall package_nameCreating Your First npm Project
Now that you have a basic understanding of npm, let's create your first npm project.
Initializing a new project
Navigate to the directory where you want to create your project and run the following command:
npm initThis command will guide you through setting up your project, including creating a package.json file. To skip the prompts and accept the defaults, use npm init -y instead.
Understanding package.json
The package.json file is a vital part of any npm project. It contains metadata about your project, including its name, version, description, and dependencies.
Working with Dependencies and DevDependencies
Managing dependencies in your npm project is crucial for seamless functionality.
Defining dependencies in package.json
To specify project dependencies, add them to the dependencies section of your package.json file. For example:
{
"dependencies": {
"package_name": "version_number"
}
}In practice, you rarely edit this by hand — running npm install package_name automatically adds the correct entry for you.
Installing project dependencies
After defining dependencies in the package.json file, run the following command to install them:
npm installThis will read the package.json file and install all the listed dependencies.
Utilizing devDependencies for development purposes
Some packages are only needed during development and not in production. To install a package as a devDependency directly, use the --save-dev flag:
npm install --save-dev package_nameExploring npm Commands
Npm provides several useful commands to explore packages and projects.
npm search
To search for packages in the npm registry, use the following command:
npm search package_namenpm info
To get detailed information about a specific package, use the following command:
npm info package_namenpm outdated
To see which installed packages have newer versions available, run:
npm outdatednpm audit
To scan your project's dependencies for known security vulnerabilities:
npm auditAdd npm audit fix to automatically upgrade affected packages to a patched version where possible.
Navigating npm Registry
Understanding the npm registry is essential for leveraging npm's capabilities fully.
Understanding the npm registry
The npm registry is a vast collection of packages hosted on npm's servers. Developers can publish their packages to the registry and access existing packages when needed.
Publishing your own packages
If you have created a useful package, you can publish it to the npm registry to share it with the community. You'll need an npm account and to be logged in via npm login first, then run:
npm publishTroubleshooting npm Issues on Ubuntu
While npm is a powerful tool, you might encounter issues during its usage on Ubuntu.
Common issues and solutions
- Permission errors (EACCES) when installing global packages: avoid reaching for
sudoas the fix — this is a known source of deeper permission problems later. Switch to NVM (Method 3 above), which installs into your home directory and never needs elevated privileges for global installs. - Dependency conflicts: ensure that your project's dependencies are compatible with each other, and update them regularly to avoid conflicts.
npm outdatedandnpm auditboth help surface these early. - Slow package installation: if packages are downloading slowly, consider using a different npm registry mirror or an alternative package manager like pnpm or yarn.
- Conflicting npm versions after installing via NodeSource: don't run
apt install npmseparately after a NodeSource install — it installs Ubuntu's own npm package on top and causes version conflicts.
Utilizing npm support resources
Npm has an active community and provides various support resources. Check the npm documentation, forums, or GitHub repositories for assistance with specific issues.
Advantages and Disadvantages of npm on Ubuntu
Benefits of using npm with Ubuntu
- Access to a vast ecosystem of open-source packages.
- Simplified package management and version control.
- Seamless integration with Node.js projects.
- Active community support and continuous updates.
Potential challenges and how to overcome them
- Dependency management: Large projects with many dependencies might require careful version management to prevent conflicts.
- Network connectivity: Slow or unreliable internet connections can hinder package installations and updates.
Conclusion
Mastering npm on Ubuntu is a valuable skill for developers working with JavaScript-based projects. In 2026, the right installation method matters more than it used to — NVM or NodeSource will serve you far better than Ubuntu's default repository if you want a current, well-supported Node.js and npm setup. By understanding the package management process and exploring npm's powerful features, you can streamline your development workflow and build robust applications efficiently.