Pip Install Specific Version: A Guide

Pip allows users to install a specific version of a Python package using the pip install == command. Here’s how.

Written by Dario Radečić
Published on Nov. 08, 2024
Developer installing software on computer
Image: Shutterstock / Built In
Brand Studio Logo

You can install a specific version of a Python package using Pip by running the pip install <package_name>==<version> command. For example, to install version 1.3.4 of Pandas, execute the pip install pandas==1.3.4 command from the terminal.

2 Ways to Pip Install a Specific Version of a Python Package

  1. Uninstall and Install Method: First, run pip uninstall <package_name>, then execute pip install <package_name>==<version>.
  2. Overwrite Method: Install the specific Python package and add --ignore installed to overwrite the previous version.

That’s the short version, but sometimes you’ll want to take more control over package installation. That’s where the rest of the article comes in.

 

Why Would You Want to Install a Specific Version of a Python Package?

You might be wondering, why even bother with older versions of Python packages? Well, maybe you have a huge codebase that’s not compatible with the most recent package update. Maybe you wrote the code years ago and it still works in production, but updating the package might break it. Or maybe even the most recent package version isn’t compatible with your Python version.

In other words, whatever the case may be, there are valid reasons for installing older versions of a Python package.

Let’s now go over some practical examples of how to install a specific package version with Pip.

More on PythonHow to Check if a List Is Empty in Python

 

How to Check Which Version of a Python Package is Installed

There are two useful commands you must know before proceeding to install a specific Python package version. These are:

  • pip show <packagename>: This shows the currently installed version of the package, its summary, author, license, dependencies, and so on.
  • pip index versions <packagename>: This lists all the available package versions you can install.

Let’s check them out for the pandas Python package. The following shell command prints the currently installed version:

pip show pandas
Current version of the Pandas package
Current version of the Pandas package. | Screenshot: Dario Radečić

It seems like Pandas version 1.5.3 is installed, and it requires numpy, python-dateutil and pytz packages to run.

But is version 1.5.3 the latest, and which other versions can we install? That’s what the next shell command answers:

pip index versions pandas
Available Pandas package versions
Available Pandas package versions. | Screenshot: Dario Radečić

Version 2.0.0 is the most recent, but you can go far, far back, depending on the Python version you have installed on your system or inside a virtual environment.

You now know how to check for the currently installed package version and also how to list all the available versions. Next, let’s see how to pip install a specific version of a package.

 

How to Install Pip Specific Version of a Python Package

There are two main ways to install a specific version of a Python package. The first one requires two commands, one to uninstall the current version, and the other to install the version you want. The second approach packs all that functionality in one shell command, so that’s the one you should use in practice.

Method 1: Uninstall and Install

First, identify which version of Pandas you have installed, and which versions you can potentially install according to your current Python version.

In our case, we have version 1.5.3, but we want to go back to version 1.3.4. To do so, we first have to uninstall the current version:

pip uninstall pandas
Uninstalling a Python package with pip
Uninstalling a Python package with pip. | Screenshot: Dario Radečić

And now, pip install specific version of Pandas by running the following command:

pip install pandas==1.3.4
Installing a specific version of a Python package with Pip
Installing a specific version of a Python package with Pip. | Screenshot: Dario Radečić

It seems like everything went fine, but how can you make sure version 1.3.4 was installed? Spoiler alert: You already know the command:

pip show pandas
Currently installed Pandas version
Currently installed Pandas version. | Screenshot: Dario Radečić

Pandas 1.3.4 is successfully installed on our system, so we can mark our job here as done.

Method 2: Overwrite the Installed Package

The superior approach to the one from the previous section is to install a specific version of a Python package with a single shell command. You can do so by appending the --ignore-installed flag when installing a specific version of the package.

Here’s an example: It installs Pandas version 2.0.0 and overwrites version 1.3.4, which was installed moments ago:

pip install pandas==2.0.0 --ignore-installed
Overwriting the current package version
Overwriting the current package version. | Screenshot: Dario Radečić

And just to verify, you can use the pip show command:

pip show pandas
Currently installed Pandas version
Currently installed Pandas version. | Screenshot: Dario Radečić

And that’s two ways to pip install a specific version of a Pandas package. Will it always be that easy? Probably not, so continue reading for a more production-ready use case.

A tutorial on how to install a specific package with pip. | Video: Python Basics

More on PythonStop Using Pip Freeze For Your Python Projects

 

How to Install Multiple Python Packages with Specific Versions

Installing Python packages one by one is fine in your local environment, but it doesn’t scale in production. That’s where you’ll typically have a requirements.txt file, which contains all Python dependencies for the project alongside their specific version.

Wait, what is a trequirements.xt file? 

The requirements.txt is typically structured like this:

package_1==version_1
package_2==version_2
...

In short, there’s a list of Python dependencies with their respective versions. The question now is: Can you install all of them with a single command? And the answer is yes, you can.

Here’s what the file looks like on our end:

Contents of the requirements.txt file
Contents of the requirements.txt file. | Screenshot: Dario Radečić

And this is the command you should run if the requirements.txt file is in the same directory as your shell:

pip install -r requirements.txt
Installing multiple Python dependencies with a single command
Installing multiple Python dependencies with a single command. | Screenshot: Dario Radečić

If your requirements.txt file is located elsewhere, simply provide an absolute or relative path after -r. That’s it!

Now, let’s verify the correct package versions were installed:

Checking Python package versions
Checking Python package versions. | Screenshot: Dario Radečić

And that’s how you can install multiple Python packages at once. Let’s make a short recap next.

 

Pip Install Specific Version: Overview

Project dependency management doesn’t have to be difficult. You can use Python’s pipreqs module to keep track of the packages used within a project and to create a requirements.txt file, and then apply everything learned in this article to transfer the same set of dependencies to another machine or a production environment.

You can also pip install a specific version of a Python package one at a time, which is fine for a local development environment. A far better approach is to have a dedicated file for the dependencies, and install them all at once.

Frequently Asked Questions

There are two common ways to install a specific package version with pip:

  1. Uninstall and Install Method: First, run pip uninstall <package_name>, then execute pip install <package_name>==<version>.
  2. Overwrite Method: Install the specific Python package and add --ignore installed to overwrite the previous version.

You can install multiple specific package versions with pip using the requirements.txt file, which contains the specific package versions for a given project. If the requirements.txt file is in the same directory as your shell, enter: pip install -r requirements.txt. If it isn’t, provide an absolute or relative path after the -r.

Explore Job Matches.