How I can make apt-get install to my virtualenv?

Why would you want to do this? The whole point is to avoid doing stuff like that...

virtualenv whatever
cd whatever
bin/pip install dbus-python

You may also choose to specify --no-site-packages to virtualenv to keep it extra isolated.


If you really need to do it this way, you can just copy the files that get installed globally directly into your virtualenv. For example I couldn't get pycurl working since the required libraries weren't installing, but apt-get install python-pycurl did. So I did the following:

sudo apt-get install python-pycurl
cp /usr/lib/python2.7/dist-packages/pycurl* ~/.virtualenvs/myenv/lib/python2.7/site-packages/

The install said it was adding it to /usr/lib/python2.7. So I looked in that directory for a site-packages or dist-packages with pycurl, after looking at the files I copied them into my virtualenv. You'd have to also copy any executables from bin into your virtualenv's bin directory.

Also, running a pip install -r requirements.txt successfully found pycurl in there and just skipped over it as if I had installed it via pip.


An alternative solution is to install globally, then followed by allowing the virtualenv to be able to see it. As an example, let's say we want to install matplotlib for Python 3:

  1. sudo apt update # Update first
  2. sudo apt install python3-matplotlib # Install globally
  3. sudo pip3 install -U virtualenv # Install virtualenv for Python 3 using pip3
  4. virtualenv --system-site-packages -p python3 ./venv #the system-site-packages option allows venv to see all global packages including matplotlib
  5. source ./venv/bin/activate #activate the venv to use matplotlib within the virtualenv
  6. deactivate # don't exit until you're done using the virtualenv