How to Install NPM on Ubuntu Step-by-Step ?

how to install npm on ubuntu

If you’re a developer or looking to start working with JavaScript and Node.js on your Ubuntu system, installing npm (Node Package Manager) is a crucial first step. In this guide, we’ll walk you through the process of install npm on Ubuntu, from ensuring your system is ready for installation to troubleshooting common issues you might face along the way.

npm is the most widely used package manager for JavaScript, and it plays a vital role in managing dependencies, packages, and libraries for your Node.js projects. Whether you’re using a fresh installation of Ubuntu or working on an existing project, this guide will help you set up npm correctly.

What is npm and Why Do You Need It?

npm stands for Node Package Manager, and it’s a tool that helps you manage JavaScript packages for your projects. With npm, you can:

  • Install and manage dependencies: npm makes it easy to install external packages for your project, ensuring your project has the tools it needs to run.
  • Share your code: You can also use npm to share your own packages with the community, allowing you to contribute to open-source projects.
  • Run scripts: npm can be used to run automation scripts, such as running tests, building your app, or starting a development server.

In simple terms, npm is your go-to tool for managing JavaScript libraries and automating tasks for your projects. It’s crucial for every developer working with Node.js.

Prerequisites for Install npm on Ubuntu

Before you start installing npm, there are a few things to check and prepare:

1. Ensure Node.js is Installed

npm comes bundled with Node.js, so you need to have Node.js installed first. Without Node.js, npm cannot function.

To check if Node.js is already installed on your system, open the terminal and run the following command:

bashCopy codenode -v

If you see a version number (e.g., v16.14.0), Node.js is already installed, and npm should be available too. To check for npm, run:

bashCopy codenpm -v

If both commands return version numbers, you’re all set! If not, follow the installation steps below to install both Node.js and npm.

2. Update Your Ubuntu System

It’s always good practice to ensure your system is up to date before installing any software. Run the following commands to update your Ubuntu system:

bashCopy codesudo apt update
sudo apt upgrade

This will ensure you have the latest security updates and bug fixes before proceeding with the installation of Node.js and npm.

How to Install npm on Ubuntu

How to Install npm on Ubuntu?

Now that your system is ready, let’s dive into the installation process. There are several methods to install npm on Ubuntu. The easiest and most reliable method is through Ubuntu’s package manager, APT. However, if you want to install the latest version of Node.js and npm, we’ll also cover how to use the NodeSource repository.

Method 1: Install npm via Ubuntu’s Default Repositories

Ubuntu’s default repositories provide a version of Node.js and npm, though it may not always be the latest. To install npm using APT, follow these steps:

Step 1: Install Node.js and npm
  1. Open the terminal on your Ubuntu system.
  2. Update your package list to ensure you have the latest information from the repositories:bashCopy codesudo apt update
  3. Install Node.js and npm using the following command:bashCopy codesudo apt install nodejs npm
  4. After the installation is complete, verify the installation by checking the versions of Node.js and npm:bashCopy codenode -v npm -v If you see version numbers for both Node.js and npm, the installation was successful!
Step 2: Updating npm (Optional)

The version of npm installed via Ubuntu’s default repositories may not be the latest. To update npm to the latest version, you can use the following command:

bashCopy codesudo npm install -g npm

This will install the latest stable version of npm globally on your system.

Method 2: Install npm Using NodeSource Repository (For Latest Version)

If you want to ensure you are installing the latest version of Node.js and npm, you can use the NodeSource repository. This method provides more up-to-date versions of both Node.js and npm compared to the default Ubuntu repositories.

Step 1: Install Node.js from NodeSource
  1. First, install curl if it’s not already installed. Curl is needed to fetch the setup script from NodeSource:bashCopy codesudo apt install curl
  2. Now, download and run the NodeSource installation script. For Ubuntu, you can choose from different versions of Node.js. As of 2024, the latest stable version is Node.js 16.x, but you can replace 16.x with any version you prefer:bashCopy codecurl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash - This command fetches the setup script for Node.js 16.x and runs it. If you want to install a different version (e.g., Node.js 18.x), replace 16.x with the desired version number.
  3. After running the script, install Node.js and npm:bashCopy codesudo apt install nodejs
  4. Verify the installation by checking the versions:bashCopy codenode -v npm -v
Step 2: Install build-essential (Optional)

To ensure that you can compile native add-ons (which are required by some npm packages), install the build-essential package:

bashCopy codesudo apt install build-essential

How to Update npm to the Latest Version on Ubuntu

Once you’ve installed npm, it’s important to keep it up-to-date. The package manager releases frequent updates with new features, security patches, and bug fixes. Here’s how to ensure you’re using the latest version:

  1. Run the following command to update npm globally:bashCopy codesudo npm install -g npm
  2. After the update is complete, verify the version:bashCopy codenpm -v

This ensures you are using the latest stable version of npm on your Ubuntu system.

Troubleshooting Common npm Installation Issues on Ubuntu

While npm installation is usually straightforward, you may encounter some issues. Here are a few common problems and their solutions:

1. “npm command not found” Error

If you get an error stating that npm is not found, it usually means that npm wasn’t installed correctly or the installation wasn’t added to your PATH. Try the following:

  • Check if Node.js is installed: Run node -v to ensure Node.js is installed. If it’s not, follow the installation steps again.
  • Reinstall npm: You can reinstall npm globally with the command:bashCopy codesudo apt install npm

2. Permissions Issues

If you encounter permission issues when installing global packages with npm (such as “EACCES” or “Permission denied”), it’s likely because npm tries to write files to directories that require elevated permissions.

Solution: You can resolve this by either running npm commands with sudo or changing the directory permissions:

bashCopy codesudo chown -R $(whoami) ~/.npm

This command will change ownership of the npm cache directory to your user account, preventing permission errors when installing packages globally.

3. Old Version of npm

As mentioned earlier, the version of npm installed via Ubuntu’s default repositories may be outdated. To update to the latest version, simply run:

bashCopy codesudo npm install -g npm

Conclusion

npm is an essential tool for JavaScript developers, and install npm on Ubuntu is a straightforward process. Whether you choose to install npm using Ubuntu’s default repositories or the NodeSource repository for the latest version, this guide has you covered.

Once installed, npm allows you to efficiently manage your project dependencies, share code with the open-source community, and automate various development tasks. Keeping npm up-to-date ensures you have access to the latest features and security updates.

If you encounter any issues during installation, the troubleshooting steps provided will help you resolve them quickly. With npm set up on your Ubuntu system, you’re now ready to dive deeper into JavaScript development!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top