Search code examples
c++ubuntusdlubuntu-20.04

SDL2: Could not create window: No available video device


I'm running Ubuntu 20.04 and trying to use the SDL2 library to build a C++ program. I've installed the library from apt, so didn't compile it myself. I'm also using CMAKE.

The problem is that I can't run the program because it reports an error saying:

Could not create window: No available video device

I've read this and this and much more links, but none worked. I already installed:

  • xorg-dev
  • libx11-dev
  • libgl1-mesa-glx
  • libsdl2-dev

(with sudo apt install xorg-dev libx11-dev libgl1-mesa-glx libsdl2-dev.)


Solution

  • If your DISPLAY environment variable is not set, you can either set it inline along with your app command

    DISPLAY=:0.0 ./yourApp
    

    or set it permanently in the console where you execute your app:

    export DISPLAY=:0.0
    ./yourApp
    

    To set environmental variables from CMake, you can use cmake set command set(ENV{DISPLAY} :0.0)