The pip install command is failing with a "Permission denied" error because the user account running pip doesn’t have the necessary write permissions for the Python installation directory.

Common Causes and Fixes

  1. Installing Packages Globally Without Root Privileges

    • Diagnosis: When you run pip install <package> and get a Permission denied error, it’s usually because pip is trying to write to a system-wide Python installation (like /usr/local/lib/python3.x/site-packages) that requires root privileges.
    • Fix 1 (Recommended): Install into user site-packages.
      pip install --user <package>
      
      This installs the package into your user’s home directory (e.g., ~/.local/lib/python3.x/site-packages), which you always have write permissions for. It’s the safest and most common way to install packages for your own use.
    • Fix 2 (Use with Caution): Use sudo.
      sudo pip install <package>
      
      This elevates your privileges to root, allowing pip to write to system directories. Warning: This can lead to conflicts if different applications or system tools rely on specific versions of packages installed system-wide. Only use this if you understand the implications and are sure you need to install system-wide.
    • Why it works: The --user flag tells pip to target a directory within your home folder that your user account owns. sudo temporarily grants root permissions, allowing modification of protected system files.
  2. Incorrect Ownership of Python Installation Directories

    • Diagnosis: If you’ve previously used sudo to install packages or managed Python installations manually, the ownership of the site-packages directory or its parent directories might have been set to root. Subsequent non-sudo pip commands will then fail. Check ownership with ls -ld /usr/local/lib/python3.x/site-packages (replace python3.x with your Python version).
    • Fix: Recursively change the ownership of the relevant Python installation directory back to your user.
      sudo chown -R $USER:$USER /usr/local/lib/python3.x/site-packages
      
      Replace $USER:$USER with your actual username and group if they differ, and adjust the path to your Python version.
    • Why it works: chown changes the owner and group of files and directories. By setting it to your user, pip (when run as your user) will have the necessary permissions to write to these locations.
  3. Using pip installed via sudo for a Virtual Environment

    • Diagnosis: If you installed pip itself using sudo (e.g., sudo apt install python3-pip or sudo pip install pip), and then try to use that pip to install into a virtual environment created without sudo, you can run into permission issues within the virtual environment’s site-packages directory.
    • Fix: Reinstall pip within the virtual environment using the virtual environment’s pip executable, or reinstall the virtual environment from scratch.
      # Activate your virtual environment first
      source /path/to/your/venv/bin/activate
      
      # Reinstall pip within the venv
      pip install --upgrade pip
      
      # Then install your package
      pip install <package>
      
      If you suspect deeper issues, remove and recreate the virtual environment:
      deactivate
      rm -rf /path/to/your/venv
      python3 -m venv /path/to/your/venv
      source /path/to/your/venv/bin/activate
      pip install <package>
      
    • Why it works: Virtual environments are designed to be isolated and managed by the user. Ensuring pip and its installations within the venv are owned by the user creating the venv prevents permission conflicts.
  4. Full Disk or Quota Exceeded

    • Diagnosis: While less common for Permission denied specifically, a full disk or user quota can manifest as write failures. Check disk space with df -h and your quota with quota -s.
    • Fix: Free up disk space or request a quota increase.
    • Why it works: pip cannot write new files if there’s no space available on the partition.
  5. SELinux or AppArmor Restrictions

    • Diagnosis: Security modules like SELinux (common on Red Hat-based systems) or AppArmor (common on Debian/Ubuntu) can prevent processes, including pip, from writing to certain directories, even if standard file permissions allow it. Check system logs for SELinux/AppArmor denials (e.g., /var/log/audit/audit.log for SELinux, /var/log/syslog or journalctl for AppArmor).
    • Fix (SELinux): Temporarily set SELinux to permissive mode to test: sudo setenforce 0. If this resolves the issue, you’ll need to create specific SELinux policies to allow pip access to the target directory. This is complex and depends heavily on your setup. Consult SELinux documentation for your distribution.
    • Fix (AppArmor): Edit the relevant AppArmor profile (often in /etc/apparmor.d/) to allow write access for the pip executable to the Python site-packages directory. Reload AppArmor profiles afterward.
    • Why it works: These security modules enforce mandatory access controls beyond standard Unix permissions. Modifying their policies allows pip to perform the necessary write operations.
  6. Read-Only File System Mount

    • Diagnosis: The partition containing your Python installation or site-packages directory might be mounted as read-only. Check with mount | grep ' / ' (or the specific mount point). Look for ro in the options.
    • Fix: Remount the filesystem as read-write.
      sudo mount -o remount,rw /path/to/mountpoint
      
      You’ll also need to investigate why it was mounted read-only in the first place (e.g., filesystem errors, manual configuration in /etc/fstab).
    • Why it works: A read-only filesystem explicitly prevents any write operations, including package installations. Remounting it with read-write (rw) permissions removes this restriction.

After resolving these issues, you might encounter a No module named '<package>' error if the installation itself was interrupted or corrupted, requiring a re-installation.

Want structured learning?

Take the full Pip course →