Node Package Manager (npm) is the default package manager for Node.js, a JavaScript runtime environment that is essential for a wide range of modern web development tasks. Whether you’re building front-end applications with frameworks like React or Angular, developing back-end services with Express.js, or utilizing various command-line tools, npm plays a central role in managing your project’s dependencies. This guide will walk you through the process of installing npm on your Ubuntu system, ensuring you have the necessary tools to manage your Node.js projects effectively.
Understanding Node.js and npm
Before diving into the installation process, it’s beneficial to understand the relationship between Node.js and npm. Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. It allows developers to run JavaScript code outside of a web browser, making it suitable for server-side applications, command-line tools, and more.

npm, on the other hand, is bundled with Node.js. Its primary function is to manage packages, which are essentially reusable pieces of code that developers can incorporate into their projects. npm allows you to:
- Install Packages: Download and install libraries, frameworks, and tools from the npm registry.
- Manage Dependencies: Keep track of all the external packages your project relies on and their specific versions.
- Publish Packages: Share your own reusable code with the wider developer community.
- Run Scripts: Define and execute custom scripts for tasks like building, testing, and deploying your applications.
The npm registry is a vast collection of open-source JavaScript packages, making it an indispensable resource for any Node.js developer.
Installation Methods for npm on Ubuntu
There are several reliable ways to install npm on Ubuntu, each with its own advantages. The most common and recommended methods involve using the Ubuntu repositories, the NodeSource repository, or installing Node.js directly from the official website. We will explore each of these in detail.
Method 1: Using Ubuntu’s Default Repositories
Ubuntu’s default package repositories often contain a version of Node.js and npm. While this method is straightforward, the versions available might not always be the latest. For many projects, especially those that don’t require the absolute newest features, this can be a perfectly adequate solution.
Step 1: Update Package Lists
Before installing any new software, it’s crucial to update your system’s package lists to ensure you’re fetching the latest available versions of packages. Open your terminal and run the following command:
sudo apt update
This command downloads the latest package information from all configured sources.
Step 2: Install Node.js and npm
Once your package lists are updated, you can install Node.js and npm. The package name for Node.js in Ubuntu repositories is typically nodejs, and npm is usually installed as a dependency or can be installed separately.
To install both, execute:
sudo apt install nodejs npm
This command will install the Node.js runtime and the npm package manager. The npm package explicitly ensures that npm is installed, even if it wasn’t automatically included as a dependency of nodejs.
Step 3: Verify Installation
After the installation completes, it’s essential to verify that Node.js and npm have been installed correctly and to check their versions. Run the following commands in your terminal:
node -v
npm -v
These commands will output the installed versions of Node.js and npm, respectively. If you see version numbers, the installation was successful.
- Pros: Simple, uses system’s package manager, generally stable.
- Cons: Versions may be outdated, especially for newer Node.js features or security updates.
Method 2: Using NodeSource Repositories (Recommended for Latest Versions)
For developers who need more recent versions of Node.js and npm, using the official NodeSource repositories is highly recommended. NodeSource provides up-to-date Node.js packages for various Linux distributions, including Ubuntu. This method allows you to choose specific Node.js versions (e.g., LTS or the latest current version).
Step 1: Update Package Lists and Install curl
First, ensure your system is up-to-date and that you have curl installed, which is necessary to download the NodeSource setup script.
sudo apt update
sudo apt install curl
Step 2: Add the NodeSource Repository
NodeSource provides a convenient script to add their repository to your system. You’ll need to specify the Node.js version you want to install. For example, to install Node.js LTS (Long Term Support) version, which is generally recommended for stability and production environments, use the following command. Replace lts/* with a specific version like node_20.x if you need a particular LTS release.
For Node.js LTS (e.g., Node 20.x):
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
Alternatively, if you want the latest current version of Node.js (which includes the newest features but might be less stable for production), you can use:
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
The curl command downloads the script, and sudo -E bash - executes it with root privileges, adding the NodeSource repository and updating your package lists.
Step 3: Install Node.js and npm
Once the NodeSource repository has been added and your package lists updated by the script, you can install Node.js and npm using apt:
sudo apt install nodejs
When installing nodejs from the NodeSource repository, npm is typically included as a dependency.
Step 4: Verify Installation
As with the previous method, verify the installation:
node -v
npm -v
You should see the version numbers corresponding to the Node.js version you selected from the NodeSource repository.

- Pros: Provides the latest stable (LTS) and current versions of Node.js and npm, easy to update to newer versions.
- Cons: Requires adding an external repository, slightly more complex than using default repositories.
Method 3: Installing from Source or Using nvm (Node Version Manager)
While not as common for standard installations, you can also compile Node.js from source or use a version manager like nvm. nvm is particularly useful if you need to switch between different Node.js versions for various projects.
Using nvm (Node Version Manager)
nvm allows you to install and manage multiple Node.js versions on a single system without conflicts. This is ideal for developers who work on projects requiring different Node.js environments.
Step 1: Install nvm
You can install nvm by downloading and running its installation script. Visit the official nvm GitHub repository for the latest installation command, as it can change. A common command is:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
(Note: Replace v0.39.7 with the latest version number found on the nvm GitHub page.)
After running the script, you might need to close and reopen your terminal or run the following commands to load nvm into your current shell session:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
Step 2: Install a Node.js Version with npm
Once nvm is installed and loaded, you can install Node.js. To install the latest LTS version:
nvm install --lts
To install the latest current version:
nvm install node
You can also install a specific version, for example:
nvm install 20.10.0
After installation, nvm will automatically use the newly installed version. You can switch between installed versions using nvm use <version>.
Step 3: Verify Installation
Check the versions:
node -v
npm -v
- Pros: Excellent for managing multiple Node.js versions, easy to switch between them, useful for testing.
- Cons: Requires an additional tool (
nvm), installation process is slightly more involved.
Managing npm Packages
Once npm is installed, you can start using it to manage your project’s dependencies.
Initializing a Project
Navigate to your project directory in the terminal and initialize a new Node.js project using npm. This will create a package.json file, which is the manifest file for your project’s metadata and dependencies.
cd /path/to/your/project
npm init -y
The -y flag accepts all default options, quickly creating a package.json file.
Installing Packages
To install a package, use the npm install command followed by the package name. For example, to install the express framework:
npm install express
This will download express and its dependencies into a node_modules folder within your project directory and add express to the dependencies section of your package.json file.
To install a package as a development dependency (e.g., for testing or building tools, not needed in production):
npm install --save-dev jest
Updating Packages
To update a specific package to its latest compatible version according to your package.json constraints:
npm update <package-name>
To update all packages in your project:
npm update
Uninstalling Packages
To remove a package from your project:
npm uninstall <package-name>
This will remove the package from the node_modules folder and update your package.json.

Conclusion
Installing npm on Ubuntu is a fundamental step for any developer working with Node.js. By following the methods outlined above, you can ensure you have a correctly set up environment to manage your project’s dependencies efficiently. Whether you opt for the simplicity of Ubuntu’s default repositories, the up-to-date versions from NodeSource, or the flexibility of nvm, mastering npm is key to a productive Node.js development workflow. Remember to always verify your installation and consider the versioning needs of your projects when choosing an installation method.
