Logo

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

On Ubuntu (and many other Linux distributions), you often have multiple versions of Python installed—commonly Python 2.x and Python 3.x. To install packages specifically for Python 3, you can use either:

  1. pip3 (the dedicated command for Python 3), or
  2. python3 -m pip (calling pip as a module under Python 3).

Below are the most straightforward approaches.

1. Using pip3 Directly

Many Ubuntu systems provide a pip3 command for Python 3 by default or after installing the required packages:

sudo apt-get update sudo apt-get install python3-pip pip3 install <package_name>

For example, if you want to install requests:

pip3 install requests

After this, you can verify installation by launching Python 3:

python3 >>> import requests >>> requests.__version__

2. Using python3 -m pip

If you prefer explicit clarity or don’t have a direct pip3 command, you can invoke pip as a module under Python 3:

sudo apt-get update sudo apt-get install python3-pip python3 -m pip install <package_name>

This is also handy when you have multiple Python versions (like 3.7 and 3.10) and want to control precisely which interpreter is used.

3. Checking Your Installation

After installing, confirm which pip is being used:

which pip3 which python3

They should correspond to your system’s Python 3 binary (e.g., /usr/bin/python3). To see where a package was installed:

pip3 show <package_name>

This displays the version, location, and dependencies of the installed package.

4. Tips and Best Practices

  1. Use Virtual Environments
    For each project, consider using a virtual environment (venv) to isolate dependencies and Python versions:

    python3 -m venv venv source venv/bin/activate pip install <package_name>

    This avoids potential conflicts with system-level packages and keeps your project environment clean.

  2. Keep pip Updated

    python3 -m pip install --upgrade pip

    An up-to-date pip helps you avoid installation issues and ensures you get the latest package metadata.

  3. Uninstalling Packages
    If you ever need to remove a package, use:

    pip3 uninstall <package_name>

5. Level Up Your Python Skills

Installing packages is just the start. If you want to strengthen your Python knowledge, consider these resources from DesignGurus.io:

If you’re looking to build large-scale systems or aim for senior engineering roles, system design knowledge is crucial:

Key Takeaway:
To install Python 3 packages on Ubuntu, install python3-pip and then use pip3 install <package_name> or python3 -m pip install <package_name>. For a production-ready workflow, consider virtual environments to isolate your project dependencies. Happy coding!

TAGS
Python
CONTRIBUTOR
TechGrind