Last modified: May 26, 2025 By Alexander Williams
Install Python Packages Globally vs Locally
Python packages can be installed in two ways: globally or locally. Each method has its use cases and trade-offs. This guide explains both approaches.
Table Of Contents
What Are Global and Local Installations?
A global installation places packages in your system's Python directory. All users and projects can access them.
A local installation places packages in a specific project's directory. Only that project can use them.
How to Install Packages Globally
Use the pip install
command without any flags to install globally. This requires admin rights on most systems.
# Install a package globally
sudo pip install package-name
Collecting package-name
Successfully installed package-name-1.0.0
Global installations are good for tools you use across projects. Examples include black
, flake8
, or pipx
.
For system-wide tools, consider pipx. It isolates global Python applications.
How to Install Packages Locally
Use the --user
flag or virtual environments for local installations. This doesn't require admin rights.
# Install for current user only
pip install --user package-name
The package installs in your user directory. Other users on the system won't see it.
Using Virtual Environments
Virtual environments are the best way to manage local packages. They create isolated Python environments.
# Create a virtual environment
python -m venv myenv
# Activate it (Linux/macOS)
source myenv/bin/activate
# Install packages locally
pip install package-name
This keeps packages separate for each project. It prevents version conflicts between projects.
When to Use Global vs Local Installations
Use global installations for:
- Command-line tools used system-wide
- Packages needed by multiple projects
- Development utilities like linters
Use local installations for:
- Project-specific dependencies
- Different package versions per project
- When you don't have admin rights
Checking Installed Packages
Use pip list
to see all installed packages. Add --user
to see user-local packages.
# List all installed packages
pip list
# List user-local packages
pip list --user
Managing Multiple Python Versions
If you need multiple Python versions, use pyenv. It helps manage different versions easily.
For WSL users, see our guide on installing Python on WSL.
Best Practices
Follow these tips for better package management:
- Use virtual environments for projects
- Only install globally what you need system-wide
- Document dependencies in requirements.txt
- Consider pipx for global applications
Conclusion
Global installations work for system tools. Local installations are better for project dependencies. Virtual environments offer the cleanest approach.
Choose the right method based on your needs. For most projects, local installations with virtual environments are recommended.