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

Reference link:

<http://stackoverflow.com/questions/10763440/how-to-install-python3-version-of-package-via-pip-on-ubuntu>

The safest way to install the Python 3 version of a package with pip on Ubuntu is to use a Python 3 virtual environment. This keeps the package and its dependencies out of the system Python installation.

A typical workflow is:

python3 -m venv py3env
source py3env/bin/activate
python -m pip install package-name

After activation, python and pip refer to the versions inside the virtual environment. You can check this with:

python --version
python -m pip --version

When you are finished, leave the virtual environment with:

deactivate

On older Ubuntu systems, you may need to install the virtual environment support package first:

sudo apt update
sudo apt install python3-venv

If you already use virtualenv, the equivalent command is:

virtualenv -p /usr/bin/python3 py3env
source py3env/bin/activate
pip install package-name

Using python -m pip is generally preferable because it makes clear which Python interpreter owns the pip installation being used.

Leave a Reply