How To Use Pip Command in Linux

Python development relies heavily on external libraries to handle complex tasks like data analysis or web scraping. You might encounter a scenario where you try to run a script only to receive an “ImportError,” or you may need to deploy a web application that requires a specific version of a framework. Without the pip command, manually downloading and managing these dependencies across your Linux file system would be nearly impossible.

Pip is the standard package manager for Python, allowing you to install, update, and remove software from the Python Package Index (PyPI) instantly. Whether you are working on a local Ubuntu machine or a remote Red Hat server, mastering pip ensures your development environment stays organized and functional. This guide explains how to use the most common pip commands to manage your Python projects efficiently. Before you begin, it is a good idea to check your Python version to ensure compatibility with your environment.

Key Takeaways: Mastering Python Packages

  • PyPI → The central repository where thousands of open-source Python libraries are hosted.
  • pip install → The primary command used to fetch and set up new libraries on your system.
  • Requirements File → A text file (usually requirements.txt) that lists every dependency needed to replicate an environment.
  • Virtual Environments → Best practice for isolating projects to prevent library version conflicts.
  • Upgrade Flag → Used to ensure you are running the latest security patches for your installed tools.

Method 1: Installing New Packages

The most common use for pip is installing a library from the web. By default, pip connects to PyPI to find the latest version of the requested software.

Command: pip install requests

Expected Output:

Collecting requests
  Downloading requests-2.31.0-py3-none-any.whl (62 kB)
Installing collected packages: requests
Successfully installed requests-2.31.0

Note: Depending on your system configuration, you may need to use the sudo command if you are installing packages globally.

Method 2: Listing and Auditing Installed Packages

To maintain system health, you should regularly audit which libraries are currently taking up space on your storage. This is similar to how you would check file size in Linux to monitor disk consumption.

Command: pip list

Expected Output:

Package            Version
------------------ ---------
pip                24.0
requests           2.31.0
setuptools         68.0.0

This reveals every package installed in your current environment along with its specific version number.

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

Method 3: Uninstalling and Cleaning Up

If a library is no longer needed or if you are troubleshooting a slow environment, you can remove it to free up resources.

Command: pip uninstall requests

You will be asked to confirm the deletion. Type y and press Enter to finalize the removal. For a more thorough cleanup of your Linux machine, you can also use terminal commands to clean your system.


Step-by-Step Process: Replicating a Project Environment

If you are moving a project between servers, follow these steps to ensure all dependencies are perfectly matched:

  1. Open your terminal and navigate to your project folder.
  2. Generate a requirements list from your source machine: pip freeze > requirements.txt
  3. Transfer the file to your new Linux server.
  4. Verify connectivity by ensuring your Ubuntu IP address has internet access to reach PyPI.
  5. Install all dependencies at once using the list: pip install -r requirements.txt
  6. Confirm success by running pip list to see the new libraries active on the destination.

Summary Tables

GoalPip CommandKey Result
Installpip install <name>Downloads and installs a package.
Upgradepip install --upgrade <name>Updates an existing package.
Removepip uninstall <name>Deletes a library from the disk.
Auditpip listShows all installed software versions.
Detailspip show <name>Displays metadata and file paths.
FlagMeaningUse Case
-rRequirementsInstalling from a text list.
-UUpgradeMoving to the newest version.
-hHelpViewing all available flags.
-vVerboseSeeing detailed download progress.

FAQs

What is the difference between pip and pip3? On most modern Linux distributions, pip may refer to Python 2, while pip3 is explicitly for Python 3. Since Python 2 is end-of-life, it is recommended to use pip3 unless your environment is specifically alias-configured.

Can I install a specific version of a package? Yes. You can use the double-equals syntax: pip install requests==2.28.0. This is vital for maintaining application stability.

How do I fix “command not found: pip”? This means pip is not installed on your system. You can install it using your system package manager. For example, on Ubuntu: sudo apt install python3-pip.


Related Posts


David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 641

Leave a Reply

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