How to install Python libraries in Google Colab with pip

Learn how to install Python libraries in Google Colab using pip with simple, step-by-step commands for fast setup and dependency management.

Google Colab comes with many libraries pre-installed, but you will often need to install additional ones. Use !pip directly inside a notebook cell.


1. Basic Installation

Run this in a cell:

!pip install package_name

Example:

!pip install pandas



2. Installing Multiple Libraries

!pip install numpy pandas matplotlib




3. Installing a Specific Version

!pip install pandas==2.2.2


Use this when:

  • you need compatibility

  • your code depends on a fixed version



4. Upgrading a Library

!pip install --upgrade pandas



5. Installing Quietly (Cleaner Output)

!pip install package_name --quiet



6. Install from a Requirements File

A requirements file (commonly named requirements.txt) is a text file in Python projects that lists all external dependencies, packages, and their specific versions required to run an application. 

It enables consistent environments and easy installation using pip install -r requirements.txt

If you have a requirements.txt file:

!pip install -r requirements.txt

Example Content of requirements.txt
text
# Example requirements.txt file
requests==2.25.1
numpy>=1.19.0
-r other-requirements.txt

7. Verifying Installation

It is always a best practice to confim that any of your installations have been successful. 
import pandas as pd
print(pd.__version__)



8. Important Notes

  • Always run pip install in a new cell

  • Restart runtime if the library does not load:

    • Runtime → Restart runtime



  • Colab environments are temporary

    • You must reinstall libraries or Run All cells again in every session


Key Takeaways

  • Use !pip install inside a notebook cell

  • Install multiple packages in one command

  • Use version pinning for consistency

  • Restart runtime if imports fail




Comments