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.txt7. Verifying Installation
import pandas as pd
print(pd.__version__)
8. Important Notes
Always run
pip installin a new cellRestart 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 installinside a notebook cellInstall multiple packages in one command
Use version pinning for consistency
Restart runtime if imports fail
Comments
Post a Comment