Ubuntu – Installing software with python pip

python

I know that building software from source on my system using root privileges usually causes various problems and that the preferred way to install anything is to use a packet management system (apt-get first, dpkg second).

However, I need some python programs which can be only installed with pip. I have tried to use pip install --install-option="--prefix=/apps/", but it seems that also in that case the pip wants to write to /lib/python2.7/ which is a system folder.

Is there a way to install software with pip without the possibility of damaging or polluting my system with unmanaged files?

Best Answer

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.

  1. Install Python virtual environment creator (virtualenv):

    sudo apt install python-virtualenv virtualenv  
    
  2. 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 
  1. Install some packages.
    cd ~/PythonVirtualEnv  
    source bin/activate
    python -m pip install <insert-name-of-package-here> 
  1. 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>  
Related Question