How to Install requirements.txt in Python: A Complete Guide

Managing Python project dependencies becomes significantly easier once you understand requirements.txt — a plain-text file that lists every package your project needs to run. Whether you're setting up a colleague's project, deploying to a server, or recreating a development environment, knowing how to install from this file is a foundational Python skill.

What Is requirements.txt?

A requirements.txt file is a simple list of Python packages, one per line, that a project depends on. It typically looks something like this:

flask==2.3.0 requests>=2.28.0 numpy pandas==2.0.1 

Each line can specify an exact version (using ==), a minimum version (using >=), or no version at all — leaving pip to install the latest available release. This file is the standard way Python developers communicate dependencies across machines, teams, and environments.

The Core Command: pip install -r

The primary method for installing all packages listed in a requirements.txt file uses pip, Python's built-in package manager. The command is:

pip install -r requirements.txt 

The -r flag tells pip to read from a file rather than accepting a package name directly. Run this command from the directory containing the file, or provide the full path:

pip install -r /path/to/your/requirements.txt 

Pip reads each line, resolves dependencies, and installs everything in sequence. If a package is already installed at the correct version, pip skips it.

Using pip3 vs pip 🐍

On systems where both Python 2 and Python 3 are installed, pip may point to Python 2's package manager. To ensure you're installing packages for Python 3, use:

pip3 install -r requirements.txt 

Alternatively, calling pip through the Python interpreter directly avoids any ambiguity:

python -m pip install -r requirements.txt python3 -m pip install -r requirements.txt 

The python -m pip approach is generally the most reliable method because it ties the pip command explicitly to whichever Python interpreter you're invoking.

Installing Into a Virtual Environment (Strongly Recommended)

Installing packages globally affects your entire system's Python installation. Most developers install project dependencies into an isolated virtual environment instead, which keeps packages separated per project and avoids version conflicts.

Creating and activating a virtual environment:

On macOS/Linux:

python3 -m venv venv source venv/bin/activate 

On Windows:

python -m venv venv venvScriptsactivate 

Once activated, your terminal prompt changes to indicate the environment is active. Running pip install -r requirements.txt at this point installs everything into the isolated environment — not your system Python.

Common Variables That Affect Your Installation

Not all requirements.txt installations go smoothly. Several factors determine how the process behaves:

VariableHow It Affects Installation
Python versionSome packages only support specific Python versions
Operating systemCertain packages have OS-specific build requirements
pip versionOlder pip versions may struggle with modern dependency resolution
Compiler toolsPackages with C extensions require build tools (e.g., gcc, Visual C++)
Network accessPip fetches packages from PyPI by default; firewalls or proxies can block this
Existing packagesVersion conflicts with already-installed packages can cause failures

Keeping pip itself up to date reduces many installation issues:

pip install --upgrade pip 

Handling Installation Errors

Version conflicts are among the most common problems. If pip reports incompatible requirements, you may need to review the requirements.txt carefully or use a tool like pip-tools to resolve dependency trees more precisely.

Missing build tools typically appear as errors when installing packages like psycopg2, Pillow, or cryptography. These packages compile native code during installation. On Linux, installing build-essential or equivalent dev packages resolves most cases. On macOS, Xcode Command Line Tools are usually required. On Windows, the Microsoft C++ Build Tools package handles this.

Permission errors when not using a virtual environment can often be worked around with:

pip install --user -r requirements.txt 

This installs packages to your user directory rather than system-wide.

Generating requirements.txt From an Existing Environment

If you're working on a project and need to create a requirements.txt from your current environment:

pip freeze > requirements.txt 

This captures every installed package and its exact version. One caveat: pip freeze captures everything in the environment, not just direct dependencies — which can produce lengthy files with indirect packages. Tools like pipreqs or pip-tools generate leaner files based only on what your code actually imports. ⚙️

The Spectrum of Setups

A solo developer running a simple script on a personal machine has very different needs than a team deploying a Django application to a cloud server. For personal scripts, installing globally without a virtual environment may be perfectly acceptable. For production deployments, reproducibility matters — pinned exact versions (==) in requirements.txt ensure identical environments across machines.

Projects with many complex dependencies, native extensions, or strict version requirements will demand more careful environment management than lightweight utility scripts with only a handful of pure-Python packages.

How much of this applies to your situation depends entirely on what you're building, where it's running, and how many people or systems need consistent, reproducible environments. 🔧