Essential Python Pip Commands for Linux Administrators

Pip is the Python package/module installer for Linux and Windows.

Installing Pip

On Ubuntu, use the command shown below to install pip via apt. This makes pip available system wide.

$ sudo apt install python3-pip

When you are working in a Python virtual environment, pip is automatically installed. More details on Python virtual environments later.


Installing & Upgrading Packages

Upgrading pip

sudo python3 -m pip install --upgrade pip

Install a package via cli, specifying the package name.

$ pip install <package name>

Install a package via cli, use the -r flag and a file listing package names.

$ pip install -r <filename>

Install a file from a specific repository, use the -i flag and the repository URL.

$ pip install <package name> -i https://my-favorite-repo:port

Upgrade an existing package with the -U flag.

$ pip install numpy -U

List all installed packages that have newer versions available.

$ pip list --outdated

Uninstalling Dependencies with Autoremove

NOTE: When you install a package using pip, it automatically installs all required dependencies. However, when you uninstall the package, pip does not remove its dependencies. At least not by default. For this task you can use pip-autoremove.

Install pip-autoremove as shown below.

$ pip install pip-autoremove

Then use pip-autoremove to uninstall.

$ pip-autoremove <package-name> -y


Working with Package Versions

Install a specific version of a package (upgrade or downgrade)

$ pip install numpy==1.21.0

List all available versions of a package

$ pip index versions numpy


Removing Packages

Remove an installed package, specifying the package name

$ pip uninstall numpy

Remove a list of packages in a file using the -r flag

$ pip uninstall -r <filename>

Listing Installed Packages

Use pip list to show all installed packages and their versions

$ pip list

Use pip freeze to list all installed python packages, along with their version. Freeze can be used to create a requirements file in order to re-create and environment with specific package versions.

$ pip freeze

Use the show command line argument to find out more information regarding a package, installed or not. Output will show version, summary, author, etc.

$ pip show <package name>

Searching for Available Packages

In order to search for packages for install, you need to search on https://pypi.org/search directly.


Working with PIP in virtual Environments

A python virtual env is a self-contained directory that isolates a specific set of python packages and their versions. Used mainly in the context of a project.

Use the command below to create a virtual environment. In this example we are creating virtual environment foo

$ python3 -m venv foo

Then source the newly created environment file to activate

$ source foo/bin/activate

To exit the environment without closing your shell, use deactivate

$ deactivate

The command below will uninstall packages in a virtual env.

$ pipenv uninstall <packagename>

PipX

Pipx is a tool for installing and running Python applications. It is particularly useful for installing command-line tools written in Python without polluting your system-wide Python environment.. Pipx is built on top of pip.

Installing pipx on Ubuntu

$ sudo apt install pipx

Once installed, run the command below.

$ pipx ensurepath

Output below

When to Use pipx

  • When you need CLI tools written in Python without installing them system-wide.
  • When you want to avoid dependency conflicts between different Python tools.
  • When you frequently install and remove Python CLI applications.

Pip vs pipx

Featurepippipx
PurposeInstalls Python packages globally or in a virtual environment.Installs and runs Python CLI applications in isolated environments.
Installation LocationInstalls packages system-wide or in a virtual environment (venv).Each package is installed in its own isolated virtual environment.
Dependency ManagementCan cause dependency conflicts if installed system-wide.Avoids conflicts by keeping each package separate.
Best Use CaseInstalling Python libraries for development (e.g., numpy, pandas, requests).Installing CLI tools (e.g., black, poetry, httpie).
Uninstalling PackagesMay leave dependencies behind if installed globally.Removes entire package environments cleanly.
Running Without InstallingRequires installation before use.Can run commands without installing (pipx run).

When to Use pipx vs. venv

  1. Use pipx when you need standalone CLI tools that should work globally but remain isolated (e.g., pipx install black).
  2. Use venv when you need an isolated development environment for a specific project (e.g., python -m venv env).

Resources

  1. https://pip.pypa.io/en/stable/cli/pip_list/
  2. https://bootvar.com/5-useful-python-pip-commands/
  3. https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/
  4. https://daily.dev/blog/pip-essentials-for-python-developers

Leave a Reply