What pip install requirements.txt does
pip install -r requirements.txt reads a text file that lists Python packages and installs all of them at once on your computer. Instead of typing out each package name individually, you point pip to a file, and it handles the rest. This is the standard way to set up a Python project that someone else wrote, or to recreate the exact same environment on a different machine.
The requirements.txt file is just a plain text document — you can open it in Notepad or any text editor. Each line contains one package name, and optionally a version number. When you run the command, pip reads that file and installs everything listed.
Key Takeaways
- The command is pip install -r requirements.txt, where the -r flag tells pip to read from a file instead of installing a single package.
- The requirements.txt file must be in your current working directory, or you need to provide the full path to it.
- You need pip installed and Python added to your system PATH before the command will work.
- If the installation fails partway through, you can run the same command again and pip will skip packages already installed and continue with the rest.
- Version numbers in requirements.txt are optional — pip will install the latest version if none is specified.
Check that pip and Python are installed
Before you can install from a requirements file, you need pip on your system. Open your command prompt (Windows), terminal (Mac), or terminal (Linux) and type this command:
pip --version
If you see a version number like "pip 23.1.2 from /usr/lib/python3.11/site-packages/python", pip is installed and ready. If you see "command not found" or "pip is not recognized", you need to install Python first. Download it from python.org, and during installation make sure you check the box that says "Add Python to PATH" — this is the step most people miss, and it is what lets your terminal find pip.
After installing Python, close and reopen your terminal, then run pip --version again to confirm it works.
Navigate to the folder containing requirements.txt
Your terminal needs to be in the same folder as the requirements.txt file, or you need to tell pip where the file is. The easiest approach is to navigate there first. Use the cd command to change directories.
On Windows, if your requirements.txt is on your Desktop, type:
cd Desktop
On Mac or Linux, the path might look like:
cd ~/Documents/my-project
After you type the command, press Enter. Your terminal prompt should now show you are in that folder. You can verify by typing ls (Mac/Linux) or dir (Windows) to list the files in that folder — you should see requirements.txt in the list.
Run the pip install command
Once you are in the correct folder, type this command and press Enter:
pip install -r requirements.txt
The -r flag tells pip to read from a file. The filename comes right after. Pip will now read requirements.txt, parse each line, and start downloading and installing packages. You will see output scrolling past showing the progress — package names, version numbers, and "Successfully installed" messages.
This process can take anywhere from a few seconds to several minutes depending on how many packages are in the file and how large they are. Do not close the terminal while it is running.
If the installation fails partway through
Sometimes a package fails to install because of a missing dependency, a network interruption, or a version conflict. When this happens, pip stops and shows an error message. Read the error — it usually tells you what went wrong.
In many cases, you can simply run the same command again:
pip install -r requirements.txt
Pip remembers which packages it already installed successfully and skips them. It will retry the one that failed and continue with the rest. If the same package fails twice, the problem is usually that the version specified in requirements.txt does not exist, or that package has a dependency that cannot be installed on your system. You may need to edit requirements.txt and remove or change the version number for that package.
Understanding what goes in requirements.txt
A requirements.txt file is plain text with one package per line. The simplest format is just the package name:
requests flask numpy
You can also specify exact versions using ==, or minimum versions using >=:
requests==2.28.1 flask>=2.0.0 numpy
Blank lines and lines starting with # are ignored, so you can add comments to explain what each package does. When you run pip install -r requirements.txt, pip reads each line and installs that exact version if one is specified, or the latest available version if none is specified.
Installing from a requirements file in a different location
If you do not want to navigate to the folder first, you can give pip the full path to the file. On Windows:
pip install -r C:\Users\YourName\Documents\requirements.txt
On Mac or Linux:
pip install -r ~/Documents/my-project/requirements.txt
Replace the path with the actual location of your requirements.txt file. This command works from any folder on your system, so you do not have to navigate first. The rest of the installation process is identical.
Frequently Asked Questions
What if I get "No such file or directory" error?
This means pip cannot find the requirements.txt file. Check that you are in the correct folder by typing ls or dir to list files. If you do not see requirements.txt, navigate to the right folder using cd. If the filename is slightly different (like requirements-dev.txt), use that name instead in the pip command.
Can I install from multiple requirements files at once?
Yes. Run the command multiple times with different filenames, or list them all in one command: pip install -r requirements.txt -r requirements-dev.txt. Pip will install packages from both files.
Does pip install -r requirements.txt remove packages I already have?
No. It only installs what is listed in the file. If you have other packages installed that are not in requirements.txt, they stay on your system. Pip does not uninstall anything.
What if a package in requirements.txt is already installed?
Pip checks the version. If the installed version matches what is in requirements.txt, pip skips it. If the version is different, pip upgrades or downgrades to match the file. If no version is specified in the file, pip leaves the installed version alone.
Can I create a requirements.txt file from packages I already have installed?
Yes, using pip freeze > requirements.txt. This command lists all installed packages and their versions, then saves that list to a file called requirements.txt. You can then share that file with others or use it to recreate the same environment elsewhere.