What pip install requirements.txt does
pip install -r requirements.txt reads a text file that lists Python packages your project needs, then downloads and installs all of them at once. Instead of typing each package name individually, you give pip a single file that contains the whole list. This is how most Python projects share their dependencies — the requirements.txt file travels with the code, and anyone who clones the project can install the exact same packages in seconds.
The file itself is plain text. Each line holds one package name, and optionally a version number. When you run the command, pip checks what you already have installed, downloads what's missing, and puts everything in the right place on your computer.
Key Takeaways
- The command is pip install -r requirements.txt, where the -r flag tells pip to read from a file instead of accepting package names as arguments.
- A requirements.txt file lists one package per line, with optional version numbers like requests==2.28.0 or flask>=2.0.
- You must be in the same folder as requirements.txt when you run the command, or provide the full path to the file.
- If you are using a virtual environment (which you should be), set up it before running pip install so packages go into that environment, not your system Python.
Setting up before you run the command
Before you install from requirements.txt, make sure you are in the right folder. Open your terminal or command prompt and navigate to the directory where requirements.txt lives. If you cloned a project from GitHub, that file is usually in the root folder — the top level of the project.
If you are using a virtual environment (which keeps your project's packages separate from your system Python), set up it now. On macOS or Linux, run source venv/bin/set up. On Windows, run venv\Scripts\set up. Your terminal prompt will change to show the environment name in parentheses, like (venv). This step matters because pip will install packages into that environment, not into your system Python, which keeps your computer clean and prevents version conflicts between projects.
If you do not have a virtual environment yet, create one first. Run python -m venv venv to create a folder called venv, then set up it using the commands above. This takes a few seconds and only needs to happen once per project.
Running the install command
Once you are in the right folder and your virtual environment is active, type the command exactly as it appears: pip install -r requirements.txt. The -r flag tells pip to read from a file. The filename comes after it. Press Enter and pip will start downloading and installing packages.
You will see output in your terminal showing each package as it installs. Lines will say things like "Collecting flask" and "Installing collected packages: flask, werkzeug, jinja2". This is normal. When it finishes, you will see a line that says "Successfully installed" followed by a list of all the packages and their versions.
If the file is in a different folder, provide the path to it. On macOS or Linux, you might run pip install -r ../config/requirements.txt. On Windows, use backslashes: pip install -r ..\config\requirements.txt. The path can be relative (starting from where you are now) or absolute (starting from the root of your drive).
Understanding what goes in requirements.txt
A requirements.txt file is straightforward — one package name per line. The most basic version lists just the name: requests, flask, pandas. When you do this, pip installs the latest version available.
Most projects pin specific versions to avoid surprises. A line like requests==2.28.0 means "install exactly version 2.28.0, nothing else". The double equals sign is strict. A line like flask>=2.0 means "install version 2.0 or newer". You can also use <= for "this version or older" and ~= for "compatible versions" — django~=4.0 means 4.0 through 4.9, but not 5.0.
You can add comments to requirements.txt by starting a line with #. This is useful for explaining why a package is there or what it does. Blank lines are ignored, so you can space things out for readability. Here is what a real requirements.txt might look like:
# Web framework flask==2.3.0 # Data handling pandas>=1.5.0 numpy # API requests requests==2.28.0
Troubleshooting common problems
If you see an error like "No such file or directory: requirements.txt", you are in the wrong folder. Check that requirements.txt actually exists in your current directory by typing ls (macOS or Linux) or dir (Windows). If it is not there, navigate to the right folder or provide the correct path to the file.
If pip says "command not found" or "is not recognized", pip is not installed or not in your system path. On most machines, pip3 install -r requirements.txt works better than pip alone, especially on macOS where both Python 2 and Python 3 might be present. Try that first.
If you see errors about packages that cannot be found, the package name in requirements.txt might be misspelled, or the package might not exist on PyPI (the Python Package Index where pip downloads from). Check the spelling against the official package name. Some packages have hyphens in their display name but underscores in their code name, or vice versa — the PyPI page for the package shows the correct name to use.
If installation fails partway through, you can run the command again. pip will skip packages it already installed and continue from where it left off. You do not need to start over.
Creating your own requirements.txt
If you are starting a new project and want to create a requirements.txt file, you can do it by hand — just open a text editor and type the package names. But the easier way is to let pip generate it from what you already have installed.
Run pip freeze > requirements.txt. This command lists every package in your current environment with its exact version number, then writes it all to a file called requirements.txt. The > symbol redirects the output to a file instead of printing it to your screen. When you are done, open requirements.txt and delete any packages you do not actually need — pip freeze includes everything, even packages that other packages depend on.
If you want to be more selective, you can edit requirements.txt by hand after creating it. Remove packages you do not use directly, loosen version pins if you want flexibility (change == to >=), and add comments explaining what each package does. This makes it easier for other people (or you, months later) to understand why each package is there.
Frequently Asked Questions
What is the difference between pip install and pip install -r?
Without the -r flag, pip expects package names as arguments: pip install flask requests pandas. With -r, pip reads from a file: pip install -r requirements.txt. The -r version is better for projects because the file travels with your code and everyone installs the same packages.
Do I have to use a virtual environment?
You do not have to, but you should. A virtual environment keeps your project's packages separate from your system Python, which prevents version conflicts when you work on multiple projects. If you install everything into your system Python, upgrading a package for one project can break another project that needs an older version.
Can I install from a requirements.txt file with a different name?
Yes. The -r flag works with any filename. You could run pip install -r dev-requirements.txt or pip install -r production.txt. Some projects have multiple requirements files for different purposes — one for development, one for testing, one for production.
What does it mean if pip says a package is already satisfied?
It means you already have that package installed at the version specified in requirements.txt (or a compatible version if you used >= or ~=). Pip skips it and moves to the next package. This is normal and saves time on reinstalls.
How do I update all packages in my requirements.txt to their latest versions?
Run pip install --upgrade -r requirements.txt. The --upgrade flag tells pip to fetch the newest version of each package, even if you already have one installed. After upgrading, run pip freeze > requirements.txt again to save the new version numbers.