Python virtual environment creator (python-virtualenv) allows you to create a sandboxed and isolated environment where Python packages can be installed without interfering with other packages on the same machine. With several virtualenvs, many different pieces of Python software with different and even mutually exclusive dependencies can coexist together. You can install python-virtualenv from the default Ubuntu repositories in all currently supported versions of Ubuntu.
Set up a virtual environment for Python anywhere in your home directory, activate your Python virtual environment from the terminal, and then install whatever packages you need to be installed by pip locally in your virtual environment as a normal user using pip install
.
Install Python virtual environment creator (virtualenv):
sudo apt install python-virtualenv virtualenv # still works in 22.04
Make a new directory (I'll call it PythonVirtualEnv in this example) for the Python virtual environment and setup the Python virtual environment with Python and pip in it.
cd ~
mkdir PythonVirtualEnv
virtualenv PythonVirtualEnv
- Install some packages.
cd ~/PythonVirtualEnv
source bin/activate
python -m pip install <insert-name-of-package-here>
Deactivate the Python virtual environment before leaving it.
deactivate
###Creating an environment with a custom Python interpreter
sudo apt install python3-virtualenv
cd ~
mkdir Python3VirtualEnv
virtualenv --python=/usr/bin/python3 Python3VirtualEnv # /usr/bin/python3 is the default location of the python3 executable
cd ~/Python3VirtualEnv
source bin/activate
python3 -m pip install <insert-name-of-package-here>