How to Install npm on Ubuntu: A Step-by-Step Guide
- by Susith Nonis
- in Tutorials
- View 9088
- Date 24 Jul, 23
If you’re working with JavaScript or building Node.js applications on Linux, you’ll realize the importance of npm (Node Package Manager). Ubuntu users often need to install npm Ubuntu, update, and manage packages. Here we’ll cover what npm is, why you need it, and multiple methods to install npm Ubuntu (including Ubuntu 22.04, 20.04, and older versions).
We’ll also walk through verification, updates, troubleshooting, and essential npm commands to get you started. Whether you’re a beginner exploring Node.js or a system administrator setting up a JavaScript environment, this guide will help you master npm installation on Ubuntu terminal step by step.
What is npm and Why Do You Need It?
Before we dive into the installation, let’s understand npm’s role in modern development.
npm (Node Package Manager) is the world’s largest software registry, containing millions of reusable JavaScript packages. When you’re working on a Node.js project, npm helps you:
- Install third-party libraries (like Express, React, or Vue).
- Manage dependencies between packages.
- Keep your applications up to date with the latest versions.
- Share your own packages with the developer community.
It’s important to note that npm comes bundled with Node.js. So, when you install Node.js on Ubuntu, npm is automatically included. If you’re new to Linux, you can also read our beginner-friendly guide: What is Ubuntu
Prerequisites Before Installing npm
Before we start the installation process, make sure you have:
Requirement |
Description |
Ubuntu System |
A working installation of Ubuntu (20.04, 22.04, or other supported versions). |
Sudo User Account |
A user account with sudo privileges to install and configure software. |
Internet Access |
Required to download packages, updates, and dependencies. |
If you already have Node.js installed, check whether npm is available by running:
npm --version
If this command returns a number, npm is already installed. If not, let’s move forward with the installation steps
Method 1 – Installing npm Using Ubuntu APT Package Manager
The APT (Advanced Package Tool) method is the simplest way to install npm on Ubuntu. It uses the default Ubuntu repositories.
Step 1: Update Package Lists
Always begin by updating your package lists to ensure you get the latest version available in the repository:
sudo apt update
Step 2: Install Node.js
Since npm is bundled with Node.js, install Node.js first:
sudo apt install nodejs
Step 3: Install npm
If npm didn’t come with Node.js, install it separately:
sudo apt install npm
Step 4: Verify Installation
Check the installed versions:
node --version
npm --version
You should now see the installed Node.js and npm version Ubuntu. If both commands return version numbers, you’ve successfully completed your npm installation Ubuntu terminal process using APT.
⚡ Tip: This method may not always provide the latest versions of Node.js and npm. If you need newer releases, consider the NodeSource or nvm methods below.
Method 2 – Installing npm via NodeSource Repository
If you want more control over the Node.js version you install, the NodeSource repository is a better choice. This method ensures you get the latest stable releases.
Step 1: Update Package Lists
sudo apt update
Step 2: Add NodeSource Repository
Download and execute the NodeSource setup script (replace 20.x with your desired version, like 18.x or 16.x):
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
Step 3: Install Node.js and npm
sudo apt install -y nodejs
This installs both Node.js and npm in one go.
Step 4: Verify the Installation
node --version
npm --version
With this method, you get the latest stable version of Node.js and npm Ubuntu packages directly from NodeSource, making it ideal for developers who need the newest features.
Method 3 – Installing npm Using nvm (Node Version Manager)
If you frequently switch between different Node.js projects, you’ll love nvm (Node Version Manager). It allows you to install and manage multiple Node.js and npm versions side by side.
We’ve covered a detailed tutorial here: How to Install NVM on Ubuntu. Below is the quick setup.
Step 1: Install nvm
Run the following command to download and install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
Load nvm into your current session:
source ~/.bashrc
Step 2: Install Node.js with nvm
For the latest LTS (Long-Term Support) version:
nvm install --lts
This also installs npm automatically.
Step 3: Switch Between Versions
nvm use 18
(replace 18 with your preferred version).
Step 4: Verify npm Installation
npm --version
Using nvm to install npm Ubuntu gives you flexibility. You can easily upgrade, downgrade, or switch Node.js/npm versions depending on your project’s needs.
How to Verify npm Installation
After using any of the above methods, confirm npm is installed correctly.
npm --version
This will return your current npm version Ubuntu. To check Node.js:
node --version
If both commands display version numbers, your npm installation Ubuntu terminal setup is complete.
For additional details, you can refer to our separate guide on How to Check NPM Version
Updating npm to the Latest Version
Keeping npm updated ensures you benefit from the latest features, bug fixes, and security patches.
To update npm Ubuntu, use:
sudo npm install -g npm@latest
Or, if using nvm:
nvm install-latest-npm
Always verify after updating:
npm --version
This way, you ensure your install node.js and npm Ubuntu setup remains secure and up to date.
Troubleshooting Common npm Installation Issues on Ubuntu
Even though installing npm on Ubuntu is usually straightforward, you might run into errors. Here are the most common problems and how to fix them.
1. “npm command not found” error
This happens when npm isn’t installed or isn’t included in your system’s PATH.
Fix:
Verify Node.js installation:
node --version
If npm is missing, reinstall using nvm:
nvm install --lts
Update PATH in .bashrc:
export PATH="$HOME/.npm-global/bin:$PATH"
source ~/.bashrc
2. Missing dependencies while installing packages
Some npm packages require compilation tools.
Fix:
Install build tools and Python:
sudo apt install build-essential python3
Then retry your package installation
3. bcrypt installation errors
The bcrypt package often fails without proper dependencies.
Fix:
sudo apt-get install build-essential python3
npm install bcrypt
If issues persist, clear npm cache:
npm cache clean --force
4. Permission errors with global packages
Using sudo npm install -g can sometimes cause permission problems.
Fix:
Set a custom global directory:
npm config set prefix ~/.npm-global
Add it to PATH:
export PATH="$HOME/.npm-global/bin:$PATH"
Then reload your shell:
source ~/.bashrc
5. Slow or blocked npm installation (proxy issues)
If you’re behind a proxy, configure npm:
npm config set proxy http://<proxy-url>:<port>
npm config set https-proxy http://<proxy-url>:<port>
Basic npm Commands to Get Started
Once you’ve managed to install npm Ubuntu, here are a few essential commands to kick off your workflow.
Check npm version Ubuntu:
npm --version
Initialize a new project:
npm init -y
Install a local package (saved in package.json):
npm install <package-name>
Install a package globally (accessible everywhere):
npm install -g <package-name>
Update all outdated packages:
npm update
Remove a package:
npm uninstall <package-name>
Check outdated packages with npm-check-updates (ncu):
ncu -u
npm install
These commands form the backbone of everyday Node.js development on Ubuntu.
Expert Tips for Using npm on Ubuntu
- Use nvm for flexible version management instead of relying only on apt.
- Always keep npm updated (update npm Ubuntu) to benefit from bug fixes.
- Avoid using sudo npm install -g whenever possible; configure a global prefix.
- Check your npm version Ubuntu after every update or reinstall.
- For server deployments, consider running npm on a virtualized environment. You can explore our Linux VPS Hosting to create a stable and isolated setup for your projects.
Conclusion
In this guide, we covered everything you need to know about how to install npm on Ubuntu 20.04:
- Different methods (APT, NodeSource, nvm).
- Step-by-step commands for npm installation Ubuntu terminal.
- How to install Node.js and npm Ubuntu.
- Verifying installation and update npm Ubuntu.
- Troubleshooting common errors.
- Basic npm commands and FAQs.
Now, whether you’re running Ubuntu 22.04, 20.04, or an older version, you have a complete roadmap for setting up npm and starting your Node.js projects confidently.
🚀 Ready to scale your development environment? Boost performance with Linux VPS Hosting from MonoVM and run npm with full control and blazing speed.
Category: Tutorials