Glusoft

Install SDL on Linux

The goal of this tutorial is simply to compile and install SDL on Linux .

Install SDL from binary

1) Update Package List (Optional)

Open a terminal on your Linux machine and update the package list to ensure you have the latest information about available packages. Then use the package manager specific to your Linux distribution.


For example, on Ubuntu, use:

sudo apt update

2) Install SDL Development Libraries on Linux

Next, you’ll need to install the SDL development libraries and any necessary dependencies. These libraries allow you to compile and run SDL-based applications. Again, use the package manager for your specific Linux distribution.

On Debian/Ubuntu-based systems do:

sudo apt install libsdl2-dev

On Red Hat/Fedora-based systems you can type:

sudo dnf install SDL2-devel

On Arch Linux you can do:

sudo pacman -S sdl2

3) Verify the Installation

After installation, you can verify if SDL is correctly installed by compiling a simple SDL application and running it. For example, let’s create a hello world named “Hello SDL“.
Then create a file named hello_sdl.c and open it with your preferred text editor. Add the following code:

    #include <SDL.h>

        int main() {
            if (SDL_Init(SDL_INIT_VIDEO) != 0) {
                printf("SDL_Init Error: %s\n", SDL_GetError());
                return 1;
            }
        
            SDL_Window* window = SDL_CreateWindow("Hello SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
            if (window == NULL) {
                printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
                return 1;
            }
        
            SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
            if (renderer == NULL) {
                printf("SDL_CreateRenderer Error: %s\n", SDL_GetError());
                return 1;
            }
        
            SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
            SDL_RenderClear(renderer);
            SDL_RenderPresent(renderer);
        
            SDL_Delay(2000); // Wait for 2 seconds
        
            SDL_DestroyRenderer(renderer);
            SDL_DestroyWindow(window);
            SDL_Quit();
        
            return 0;
        }
        

4) Compile and Run the Test Program

Now, compile the hello_sdl.c program using the following command:

gcc -o hello_sdl hello_sdl.c `sdl2-config --cflags --libs`

Finally, run the program:

./hello_sdl

If everything is set up correctly, you should see a window displaying a white screen for 2 seconds before it closes.
That’s it! You have successfully installed SDL on Linux and tested its functionality.

Compile SDL

1) Download the Source Code

Start by downloading the SDL source code from the official website (https://www.libsdl.org/download-2.0.php). You need to choose the latest stable release or a specific version depending on your preference.

Alternatively, you can use wget to download the source code directly from the terminal:

wget https://www.libsdl.org/release/SDL2-2.0.x.tar.gz

Note: Replace “x” in the URL with the version number you want to download.

2) Extract the Archive

Then use the tar command to extract the downloaded archive:

tar -xzvf SDL2-2.0.x.tar.gz

3) Navigate to the directory

Then change your working directory to the newly extracted SDL directory:

cd SDL2-2.0.x

4) Configure

The next step is to configure the build. You should use the configure script to generate the necessary build files and configure the build options. This script will detect your system configuration and set up the build accordingly.

./configure

5) Compilation of SDL

Now that the configuration is complete, it’s time to compile SDL. Then use the make command to initiate the compilation process:

make

This step may take some time, depending on your system’s performance.

6) Install SDL on the system

After successful compilation, you can install SDL system-wide using the following command:

sudo make install

This step is optional, as you can also use the compiled SDL directly from the build directory without installing it system-wide.

That’s it! You have successfully compiled and installed SDL on Linux.