Python
pip
Ubuntu
package installation
Python3

How to install python3 version of package via pip on Ubuntu?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Installing Python 3 packages on Ubuntu is straightforward when interpreter, pip, and environment boundaries are clear. Most problems come from mixing system Python, user installs, and virtual environments. A consistent workflow prevents broken dependencies and permission issues.

Confirm Python and Pip Targets

On modern Ubuntu, pip3 maps to Python 3 package installation. Verify binary paths first.

bash
1python3 --version
2which python3
3pip3 --version
4which pip3

If pip3 is missing, install it:

bash
sudo apt update
sudo apt install -y python3-pip

This gives a system-managed pip for Python 3.

Install Package for Current User

For lightweight use, install to user site packages:

bash
python3 -m pip install --user requests

Using python3 -m pip is safer than plain pip3 because it binds pip to the exact interpreter you invoked.

Check installation:

bash
python3 -c "import requests; print(requests.__version__)"

Preferred Workflow: Virtual Environments

For application projects, use venv to isolate dependencies.

bash
1sudo apt install -y python3-venv
2python3 -m venv .venv
3source .venv/bin/activate
4python -m pip install --upgrade pip
5python -m pip install flask

Now package installs stay in project-local environment and do not affect system packages.

To leave environment:

bash
deactivate

Installing Specific Versions and Constraints

Pinning versions improves reproducibility.

bash
python -m pip install "numpy==1.26.4"
python -m pip install "pandas>=2.1,<2.3"

Use requirements files:

txt
flask==3.0.3
sqlalchemy==2.0.31
psycopg[binary]==3.2.1

Install from file:

bash
python -m pip install -r requirements.txt

Handling Multiple Python Versions

On Ubuntu systems with multiple interpreters, be explicit.

bash
python3.10 -m pip install -r requirements.txt
python3.11 -m pip install -r requirements.txt

This avoids confusion where one interpreter cannot import packages installed for another interpreter.

If you need per-project interpreter switching, tools such as pyenv can help, but standard venv is enough for most cases.

Troubleshooting Common Install Errors

Permission denied errors usually mean you attempted global install without root. Prefer virtual environments rather than sudo pip.

If SSL or build failures appear, ensure build tools and headers exist:

bash
sudo apt install -y build-essential python3-dev libssl-dev libffi-dev

For cache issues, clean pip cache:

bash
python3 -m pip cache purge

Apt Packages Versus Pip Packages

Ubuntu package manager and pip solve different problems. apt is ideal for system-level stability, while pip is the standard for Python ecosystem velocity. For application development, install interpreter prerequisites with apt, then manage Python dependencies with pip inside virtual environments.

A practical split looks like this:

  • apt installs python3, python3-venv, and compile dependencies
  • pip installs project libraries from requirements.txt
  • CI reproduces the same pip lock or pinned versions

This division avoids conflicts between OS-managed files and application dependency upgrades.

When packaging applications for deployment, freeze dependency versions and test installs in a clean environment before release.

This saves debugging time.

Common Pitfalls

  • Running sudo pip3 install and overwriting system-managed packages.
  • Mixing packages across system Python and virtual environments.
  • Using plain pip3 without checking which interpreter it targets.
  • Forgetting to activate virtual environment before install.
  • Not pinning versions for shared projects and CI consistency.

Summary

  • On Ubuntu, prefer python3 -m pip so installs target the correct interpreter.
  • Use virtual environments for project isolation and safer dependency management.
  • Verify installed packages with quick import checks.
  • Pin dependency versions for reproducible builds.
  • Avoid global sudo pip workflows unless you fully control system package impact.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.