Installing pygame on Max OS X 10.8.5 Mountain Lion

I first tried to follow this guide, which did install pygame but left it spitting this kind of error when I tried to actually use it:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1000) creating CGSWindow on line 259'

That sucked, so here is an alternative.

I assume that you already have a 64-bit version of Python 2.7.2+ along with pip and virtualenv, and the XCode Command Line Tools.

First, install XQuartz. It is an X11 server, which SDL needs, and which is no longer in OSX. The official version is here, but that site is flaky, so I downloaded it from CNET(direct download link). After running that dmg, you should log out and back in, then start the x server (e.g. cmd+space, ‘xquartz’, enter). If it shows up in your dock, you’re good.

Next, we install SDL.  Download the OSX 10.5 runtime library dmg from libsdl.org and run it. Inside, just find the SDL.framework folder and copy it into /Library/Frameworks — you’ll probably need to enter your password.

Next set the environment variables needed to perform the compilation:

export CC='/usr/bin/gcc'
export CFLAGS='-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -I/opt/X11/include -arch i386 -arch x86_64'
export LDFLAGS='-arch i386 -arch x86_64'
export ARCHFLAGS='-arch i386 -arch x86_64'

(thanks to james friend for writing the guide I linked above, which this is directly lifted from). The effects of these lines will only last as long as your terminal is open, so don’t close it until you get things working.

You can now make a virtualenv, activate it, and install pygame:

virtualenv game-env
. game-env/bin/activate
pip install pygame

It should say something like:


Downloading/unpacking pygame
  Downloading pygame-1.9.1release.tar.gz (2.1MB): 2.1MB downloaded
  Running setup.py egg_info for package pygame

    WARNING, No "Setup" File Exists, Running "config.py"
    Using Darwin configuration...

    Hunting dependencies...
    Framework SDL found
    Framework SDL_ttf not found
    Framework SDL_image not found
    Framework SDL_mixer not found
    Framework smpeg not found
    PNG     : found
    JPEG    : found
    SCRAP   : not found
    PORTMIDI: not found
    Framework CoreMidi found

    If you get compiler errors during install, doublecheck
    the compiler flags in the "Setup" file.

    Continuing With "setup.py"

As long as “SDL” was found, you’re in a good spot.

If you get an error that looks like this at the very end:

/Library/Frameworks/SDL.framework/Versions/Current/Headers/SDL_syswm.h:58:10: fatal error: 'X11/Xlib.h' file not found

#include <X11/Xlib.h>

         ^

1 error generated.

error: command 'clang' failed with exit status 1

then you’ve probably got to paste in the export lines listed above.

If you get an error that looks like this at the end:

src/_numericsurfarray.c:1100: warning: implicit declaration of function ‘SDL_FreeSurface’

lipo: can't open input file: /var/folders/5y/shcnnkg970n53k9bvdvngw8w0000gp/T//cc1KHqyH.out (No such file or directory)

error: command '/usr/bin/gcc' failed with exit status 1

with something like this near the top:

Installing collected packages: pygame
  Running setup.py install for pygame
    building 'pygame._numericsurfarray' extension
    /usr/bin/gcc -DNDEBUG -g -Os -Wall -Wstrict-prototypes -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -I/opt/X11/include -pipe -arch i386 -arch x86_64 -I/NEED_INC_PATH_FIX -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c src/_numericsurfarray.c -o build/temp.macosx-10.8-intel-2.7/src/_numericsurfarray.o
    In file included from src/_numericsurfarray.c:23:
    src/pygame.h:106:17: error: SDL.h: No such file or directory

then you’ll need to double check that you installed SDL properly. If you try this over and over and it doesn’t work, you might try to play with the export lines to find your copy of SDL, or follow the guide I linked at the start, which uses homebrew to install SDL.

If things are messed up, you can always just delete the SDL.framework folder, delete your virtual environment, and start again.

If things look good, try running a sample program like the following:

>>> import pygame
>>> pygame.init()
(4, 0)
>>> pygame.display.set_mode((640, 480))
<Surface(640x480x32 SW)>
>>> pygame.quit()
>>> ^D

A screen should appear, and execution should finish cleanly. If that’s the case, you can make games with pygame. 🙂

One thought on “Installing pygame on Max OS X 10.8.5 Mountain Lion

Leave a Reply

Your email address will not be published.