How to set up SDL2 on Linux

This article explains how to get started with SDL2 in Linux. For other SDL2 tutorials (including setting up on Windows), check out my SDL2 Tutorials at Programmer’s Ranch.

Using apt-get

If you’re on a Debian-based Linux distribution, you can use the APT package manager to easily install the SDL2 development libraries. From a terminal, install the libsdl2-dev package:

sudo apt-get install libsdl2-dev

Installing from source

If for whatever reason you can’t use a package manager, you’ll have to compile SDL2 from the source code. To do this, you first have to download SDL2:

sdl2linux-download

After extracting the archive to a folder, cd to that folder and run:

./configure

When it’s done, run:

make all

Finally, run:

sudo make install

Testing it out

To verify that you can compile an SDL2 program, use the following code (it’s the same used in my “SDL2: Setting up SDL2 in Visual Studio 2010” article at Programmer’s Ranch):

#include <SDL2/SDL.h>

int main(int argc, char ** argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Quit();

    return 0;
}

You can use vi or your favourite editor to create the source code:

sdl2linux-minimal

To compile this (assuming it’s called sdl2minimal.c), use the following command:

gcc sdl2minimal.c -lSDL2 -lSDL2main -o sdl2minimal

We need to link in the SDL2 libraries, which is why we add the -lSDL2 -lSDL2main. Be aware that those start with a lowercase L, not a 1. The program should compile. It won’t show you anything if you run it, but now you know that you’re all set up to write SDL2 programs on Linux.

sdl2linux-run

12 thoughts on “How to set up SDL2 on Linux”

  1. I found this very helpful, thanks for the post. I thought setting up libsdl would be much more difficult but I got through this with no problems in under 7 minutes.

  2. Hi, I just wanted to say thank you!
    It worked like a charm and now I’m off to the races with some SDL2 action 😀

    Best wishes from Germany

  3. Thanks for the straight-forward, concise instructions on how to install SDL2 on Linux. I initially tried using the “apt” method, but it failed on my particular Linux distro, so it was great that you also included the “make” method as well.

  4. This isn’t a Linux tutorial, it’s an Ubuntu tutorial and should be labelled as such. Really tiresome to see rank amateurs poisoning open source like this.

    1. What’s tiresome is ungrateful and arrogant people being rude without knowing what they’re talking about.

      There is no mention of Ubuntu in the article. The apt-get instructions work on any Debian-based distribution (the screenshots are from Linux Mint), and installing from source works on any distribution.

      Who’s the amateur now? A public apology would be in order.

  5. I am a bit surprised here. I am trying to get a game engine (coded in C++) I set up with SDL2 in Windows to work in Linux as well. I’ve installed SDL2 by building it from source in Zorin OS (using a VM at the present time)

    When I try to compile that (using an SCons script) it compiles fine, but running it will cause an error be thrown saying that libSDL2_mixer-2.0.so.0 doesn’t exist (it actually does in /usr/local/lib and the other SDL2 .so file are there as well. The specific file IS an alias, though. Does that matter?).

    Now the funny part is that when I try the code in your example and even include “-lSDL2_mixer” everything appears to be working.

    Now I know that this is hard to answer without any code, but perhaps you understand it’s an already fully working engine (in Windows it already works fine, after all. I’m just trying to get it to work in Linux to expand my horizon, and perhaps also to be able to actually move to Linux in the nearby future) and its code is even spread over multiple github repositories, so I’m a bit wondering. The main repository is in https://github.com/TrickyGameTools/ScyndiCreativeInterpreter though (it’s a multi-project solution in VS, but only the directory SCI_Run is important as far as this issue is concerned).

Leave a Reply

Your email address will not be published. Required fields are marked *