Install Pygame on Linux
Install Pygame with pip in a virtual environment on any distribution, or use your package manager, and understand why "externally-managed-environment" appears.
On this page
Recommended: pip in a virtual environment#
This works on every distribution and gives you the latest Pygame. First make sure Python's venv module is available:
# Debian, Ubuntu, Linux Mint
sudo apt install python3-venv python3-pip
# Fedora
sudo dnf install python3-pip
# Arch Linux
sudo pacman -S python-pipThen create an environment for your project and install Pygame:
mkdir my-game && cd my-game
python3 -m venv .venv
source .venv/bin/activate
python -m pip install pygame # or: python -m pip install pygame-cePrebuilt wheels exist for x86_64 and 64-bit ARM (aarch64, including Raspberry Pi OS 64-bit), so pip doesn't need a compiler on supported Python versions.
Why "externally-managed-environment"?#
Recent Debian, Ubuntu, Fedora and other distributions mark their system Python as managed by the package manager (PEP 668). Running pip install pygame outside a virtual environment then stops with error: externally-managed-environment, to protect system tools that depend on Python. Use a virtual environment as above. Avoid --break-system-packages on your main system.
Alternative: the distribution package#
Most distributions package Pygame, which is convenient for quick scripts that use the system Python:
sudo apt install python3-pygame # Debian, Ubuntu
sudo dnf install python3-pygame # Fedora
sudo pacman -S python-pygame # Arch LinuxDistribution packages can lag behind the latest release. Don't mix a distribution package and a pip install in the same Python.
Check it works#
python -c "import pygame; print(pygame.version.ver)"
python -m pygame.examples.aliensCommon Linux problems#
| Problem | Fix |
|---|---|
externally-managed-environment | Use a virtual environment (see above). |
No module named venv / ensurepip is not available | Install python3-venv (Debian/Ubuntu). |
pip compiles and fails with sdl2-config: not found | No wheel for your Python or CPU. Use a supported Python, or install SDL2 development packages (libsdl2-dev, libsdl2-image-dev, libsdl2-mixer-dev, libsdl2-ttf-dev, libfreetype6-dev on Debian/Ubuntu) and retry. |
No sound, or mixer not initialized | See no sound in Pygame. |
| Running under WSL on Windows | WSL2 on Windows 11 can show windows through WSLg; on older setups run Pygame directly on Windows instead. |