Skip to main content

CompileUbuntu — wiki

Install the development libraries and dependencies needed to install pygame.

Download pygame from github, build, then install!

Pygame 2

Here are the dependencies for pygame 2 (using SDL2) and using Python 3. These instructions are tested to work on Ubuntu 22.04 and 24.04.

sudo apt-get update
sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev libfreetype6-dev libportmidi-dev libjpeg-dev build-essential python3-setuptools python3-dev python3-numpy python3-pip python3-virtualenv cython3 python3-full
git clone https://github.com/pygame/pygame.git
cd pygame
python3 setup.py -config -auto
python3 setup.py build
python3 setup.py install --user
# python3 -m pygame.examples.aliens
# python3 -m pygame.tests

Note to self: we can test the install instructions with this code:

vagrant init ubuntu/bionic64
vagrant up
vagrant ssh

Legacy instructions for older versions

These are instructions to build and install pygame 1. You probably don't need these, and the above instructions are the recommended ones to follow

Python 2.x

#install dependencies
sudo apt-get install git python-dev python-numpy python-opengl \
    libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsmpeg-dev \
    libsdl1.2-dev libportmidi-dev libswscale-dev libavformat-dev libavcodec-dev \
    libtiff5-dev libx11-6 libx11-dev fluid-soundfont-gm timgm6mb-soundfont \
    xfonts-base xfonts-100dpi xfonts-75dpi xfonts-cyrillic fontconfig fonts-freefont-ttf libfreetype6-dev

# Grab source
git clone git@github.com:pygame/pygame.git

# Finally build and install
cd pygame
python setup.py build
sudo python setup.py install

# Run some tests
python -m pygame.tests
python -m pygame.examples.aacircle
python -m pygame.examples.aliens
python -m pygame.examples.freetype_misc
python -m pygame.examples.glcube
python -m pygame.examples.sound
python -m pygame.examples.stars

Python 3.x

#install dependencies
sudo apt-get install git python3-dev python3-setuptools python3-numpy python3-opengl \
    libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsmpeg-dev \
    libsdl1.2-dev libportmidi-dev libswscale-dev libavformat-dev libavcodec-dev \
    libtiff5-dev libx11-6 libx11-dev fluid-soundfont-gm timgm6mb-soundfont \
    xfonts-base xfonts-100dpi xfonts-75dpi xfonts-cyrillic fontconfig fonts-freefont-ttf libfreetype6-dev

# Grab source
git clone git@github.com:pygame/pygame.git

# Finally build and install
cd pygame
python3 setup.py build
sudo python3 setup.py install

# Run some tests
python3 -m pygame.tests
python3 -m pygame.examples.aacircle
python3 -m pygame.examples.aliens
python3 -m pygame.examples.freetype_misc
python3 -m pygame.examples.glcube
python3 -m pygame.examples.sound
python3 -m pygame.examples.stars

Python 3.x into virtual environment

To install pygame into a virtual environment it is recommended to first install and configure virtualenvwrapper.

# Install dependencies
sudo apt-get install git python3-dev python3-numpy \
    libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsmpeg-dev \
    libsdl1.2-dev  libportmidi-dev libswscale-dev libavformat-dev libavcodec-dev libfreetype6-dev

# Grab source
git clone git@github.com:pygame/pygame.git

# Build pygame
cd pygame
python3 setup.py build

# Create and activate the virtual environment
mkvirtualenv NAME_OF_PROJECT --python=/usr/bin/python3
workon NAME_OF_PROJECT

# Install into the virtual environment
python3 setup.py install --prefix="$HOME/.virtualenvs/NAME_OF_PROJECT"

Installing pygame with pip

For this to work you must first follow the instructions to install the development dependencies for the version of python you're targeting as pip will download the source for pygame from bitbucket and compile it.

As an added bonus, if you add pygame to your requirements.txt and you also setup a virtual environment, pip will automatically install pygame into the virtual environment for you.

# System-wide installation WARNING: This is not advised! Don't do this if you don't know what you're doing!
sudo pip install https://github.com/pygame/pygame.git

Tests and examples with OpenGL

There is a bug in python3-opengl-3.0.2-1 in Ubuntu Xenial, this can be circumvented by installing https://launchpad.net/ubuntu/yakkety/amd64/python3-opengl/3.1.0+dfsg-1 from Yakkety. Download the package http://launchpadlibrarian.net/260965664/python3-opengl_3.1.0+dfsg-1_all.deb or a more recent version and install with sudo dpkg -i python3-opengl_3.1*.deb

.

Tests and examples without OpenGL

Building is not concerned with the availability of the package python-opengl or python3-opengl. Running the tests or running the example glcube however does need one of these packages. When OpenGL is not applicable, the test can be run in such a way to skip the OpenGL tests:

SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=disk python -m pygame.tests --exclude opengl
#or
SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=disk python3 -m pygame.tests --exclude opengl

Raspbian

For the time being, this is parked here but needs to find a place to a wiki page called CompileDebian which doesn't exist. The above also applies for Raspbian with the exception that instead of timgm6mb-soundfont the package musescore-soundfont-gm needs to be installed.