troubleshooting2025-06-01·7·48/348

Fixing Python Virtual Environment Creation Errors on Linux

Troubleshooting guide for common Python virtual environment creation errors on Linux, including venv module issues, permission errors, and path problems.

Fixing Python Virtual Environment Creation Errors on Linux

Virtual environments are essential for Python development, but creation failures can be frustrating. During a deployment on my Lisbon server, I encountered multiple venv errors that required different solutions. This guide covers the most common issues.

Environment

  • OS: Ubuntu 22.04 LTS
  • Python: 3.10.12
  • pip: 22.0.2
$ python3 --version
Python 3.10.12

$ pip3 --version
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)

The Problem

$ python3 -m venv myenv
Error: [Errno 13] Permission denied: '/home/joel/myenv'

# Alternative attempt
$ virtualenv myenv
bash: virtualenv: command not found

Analysis

Step 1: Check Python Version and Modules

# Check if venv module is available
$ python3 -c "import venv; print(venv.__file__)"
/usr/lib/python3.10/venv/__init__.py

# Check if ensurepip is available
$ python3 -m ensurepip --version
pip 22.0.2 from /usr/lib/python3.10/ensurepip/_pip (python 3.10)

Step 2: Check Permissions

# Check current directory permissions
$ ls -la /home/joel/
drwxr-xr-x  3 joel joel 4096 Jun  1 10:00 .
drwxr-xr-x  3 root root 4096 Jun  1 08:00 ..

# Check if directory is writable
$ test -w /home/joel && echo "Writable" || echo "Not writable"
Writable

Step 3: Check Disk Space

$ df -h /home
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       50G   50G     0 100% /
# No space left!

Solution

Fix 1: Install venv Package

# Ubuntu/Debian - venv is a separate package
$ sudo apt update
$ sudo apt install python3-venv

# Verify
$ python3 -m venv --help | head -3
usage: venv [-h] [--system-site-packages] [--symlinks] [--copies]

Fix 2: Fix Permission Issues

# Create in user directory
$ mkdir -p ~/venvs
$ python3 -m venv ~/venvs/myenv

# Or fix permissions on target directory
$ chmod 755 /home/joel
$ python3 -m venv myenv

Fix 3: Fix Disk Space

# Check disk usage
$ df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       50G   50G     0 100% /

# Free space
$ sudo apt clean
$ sudo apt autoremove
$ sudo journalctl --vacuum-time=7d

# Create venv in /tmp (has separate partition usually)
$ python3 -m venv /tmp/myenv

Fix 4: Install virtualenv

# Install virtualenv
$ pip3 install --user virtualenv

# Add to PATH
$ export PATH=$HOME/.local/bin:$PATH

# Create virtual environment
$ virtualenv myenv

# Make PATH persistent
$ echo 'export PATH=$HOME/.local/bin:$PATH' >> ~/.bashrc

Fix 5: Fix Symlink Issues

# If symlinks cause problems
$ python3 -m venv --copies myenv
# Creates copies instead of symlinks

# If Python is not found
$ which python3
/usr/bin/python3

# Create venv with explicit Python path
$ python3 -m venv --python=/usr/bin/python3 myenv

Complete Setup Script

#!/bin/bash
# setup-python-env.sh

set -e

VENV_DIR="$HOME/venvs/myenv"

# Check prerequisites
echo "Checking prerequisites..."

# Check Python
if ! command -v python3 &> /dev/null; then
    echo "Installing Python 3..."
    sudo apt update
    sudo apt install -y python3 python3-pip python3-venv
fi

# Check venv module
if ! python3 -c "import venv" &> /dev/null; then
    echo "Installing python3-venv..."
    sudo apt install -y python3-venv
fi

# Create venv directory
mkdir -p "$(dirname "$VENV_DIR")"

# Create virtual environment
echo "Creating virtual environment..."
python3 -m venv "$VENV_DIR"

# Activate and upgrade pip
echo "Upgrading pip..."
source "$VENV_DIR/bin/activate"
pip install --upgrade pip setuptools wheel

echo "Virtual environment created at: $VENV_DIR"
echo "Activate with: source $VENV_DIR/bin/activate"

Common Errors and Fixes

ErrorCauseFix
`No module named venv`venv not installed`sudo apt install python3-venv`
`Permission denied`Directory not writable`chmod 755 /path` or use `~/venvs/`
`No space left`Disk fullFree space with `apt clean`
`command not found: virtualenv`virtualenv not installed`pip3 install virtualenv`
`Failed to create venv`Corrupted PythonReinstall `python3`

Verify Setup

# Activate
$ source ~/venvs/myenv/bin/activate

# Check Python location
$ which python
/home/joel/venvs/myenv/bin/python

# Check pip
$ pip --version
pip 23.0.1 from /home/joel/venvs/myenv/lib/python3.10/site-packages/pip

# Install a package
$ pip install requests

Lessons Learned

  1. Always install python3-venv on Ubuntu/Debian before creating virtual environments.
  2. Check disk space before creating venvs - they require 10-20MB minimum.
  3. Use ~/venvs/ directory for all virtual environments to keep them organized.
  4. Upgrade pip immediately after creating a venv to avoid compatibility issues.
  5. Use --copies if symlink issues cause problems with your application.

This blog does not accept any external sponsorships, affiliate marketing, or ad revenue.