Fixing Virtualenv PIP Installation Issues: When Django Installs to Global Site-Packages Instead of Virtual Environment


2 views

When working with Python virtual environments, a common frustration occurs when packages install to the global site-packages directory instead of the isolated virtualenv. The error message you're seeing:

creating /usr/local/lib/python2.7/dist-packages/django
error: could not create '/usr/local/lib/python2.7/dist-packages/django': Permission denied

indicates that pip is attempting to install Django system-wide rather than within your activated virtual environment.

Several factors could cause this behavior:

$ which pip
/usr/local/bin/pip

$ which python
/var/django/myenv.env/bin/python

If these paths don't match your virtualenv's bin directory, you have a path conflict. Common causes include:

  • Virtualenv wasn't created with --system-site-packages=false
  • PATH environment variable has incorrect ordering
  • Global pip installation taking precedence
  • Virtualenv activation didn't properly modify environment variables

1. Full Virtualenv Reinitialization

First, try recreating your virtualenv:

deactivate
rm -rf myenv.env
python -m venv myenv.env --clear --system-site-packages=false
source myenv.env/bin/activate
python -m pip install --upgrade pip setuptools wheel
pip install django

2. Explicit Virtualenv Pip Usage

Instead of relying on PATH resolution:

./myenv.env/bin/pip install django

3. Environment Verification Script

Create a verification script to check your environment:

#!/bin/bash
echo "Python path: $(which python)"
echo "Pip path: $(which pip)"
echo "PYTHONPATH: ${PYTHONPATH:-not set}"
echo "Virtualenv: ${VIRTUAL_ENV:-not in a virtualenv}"

For persistent cases, check these deeper issues:

# Check for pip configuration files
ls -la ~/.config/pip/
ls -la /etc/pip.conf

# Verify no PYTHONPATH interference
unset PYTHONPATH

# Check for symlinked Python binaries
ls -la $(which python) | grep -q '\->'

To avoid future issues:

# Always use explicit paths when scripting
/path/to/venv/bin/python -m pip install package

# Create virtualenvs with clear isolation
python -m venv --clear --without-pip new_venv

# Verify installation locations
pip show django | grep Location

When working with Python virtual environments, a common frustration occurs when pip tries to install packages globally instead of within the activated virtualenv. The error message typically looks like this:

creating /usr/local/lib/python2.7/dist-packages/django
error: could not create '/usr/local/lib/python2.7/dist-packages/django': Permission denied

First, confirm that your virtual environment is properly activated. The shell prompt should show the environment name in parentheses:

(myenv.env) user@server:~$

Check your Python and pip paths after activation:

which python
which pip

Both should point to paths within your virtualenv directory, e.g.:

~/myenv.env/bin/python
~/myenv.env/bin/pip

1. Environment Variables Issue

Check if VIRTUAL_ENV is set correctly:

echo $VIRTUAL_ENV

If missing, reactivate your environment:

source ~/myenv.env/bin/activate

2. PATH Configuration Problem

Your PATH variable should prioritize the virtualenv's bin directory:

echo $PATH

If virtualenv paths aren't first, modify your activation script or .bashrc:

export PATH=$VIRTUAL_ENV/bin:$PATH

3. Pip Cache Conflict

Try clearing pip's cache and reinstalling:

pip install --no-cache-dir django

Using --target Flag

Force installation to specific directory:

pip install --target=~/myenv.env/lib/python3.8/site-packages django

Virtualenv Re-creation

Sometimes recreating the environment helps:

deactivate
rm -rf ~/myenv.env
python -m venv ~/myenv.env
source ~/myenv.env/bin/activate
pip install django

Get detailed installation logs:

pip install -vvv django

Add this check to your .bashrc:

if [[ -z "$VIRTUAL_ENV" ]]; then
    echo "Warning: No virtualenv active"
fi