Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

CMake says Could NOT find OpenGL on Ubuntu

ErrorCould NOT find OpenGL

Short answer

CMake finds OpenGL only when the development files are installed. On Ubuntu install the libgl1-mesa-dev package, which brings libgl-dev and libglvnd-dev, then rerun CMake from a clean build folder. If a legacy libGL is what you have, set OpenGL_GL_PREFERENCE to LEGACY.

  • Applies to CMake 3.28, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Rerun CMake in a fresh build folder and read the missing: list at the end of the OpenGL line.

    rm -rf build && cmake -S . -B build
  2. 2Match what you see, then jump to its fix

    1. 1The missing: list names an OpenGL include or library variableThe OpenGL development files are not installedFix
    2. 2The libraries OPENGL_opengl_LIBRARY and OPENGL_glx_LIBRARY are the ones missingCMake prefers GLVND but you only have the legacy libGLFix
    3. 3OpenGL is found, but linking or configuring fails on a target nameThe target links against the wrong imported target nameFix

What does the error look like?

  • Could NOT find OpenGL
  • find_package(OpenGL REQUIRED) stops configuration because OpenGL_FOUND is false

Cause 1 of 3

The OpenGL development files are not installed

1Check

On GLVND systems OpenGL is found through libOpenGL and the context libraries libGLX or libEGL. Check what Ubuntu has.

dpkg -l | grep -E 'libgl-dev|libglvnd-dev'

2Fix

Install libgl1-mesa-dev. On Ubuntu 24.04 it is a transitional package that depends on libgl-dev and libglvnd-dev.

sudo apt install libgl1-mesa-dev

3Verify

Delete the build folder, because CMake caches the failed lookup, and configure again.

rm -rf build
cmake -S . -B build

Confirmed by packages.ubuntu.com

Did this fix it?

Cause 2 of 3

CMake prefers GLVND but you only have the legacy libGL

1Check

OpenGL_GL_PREFERENCE defaults to GLVND in most cases, which looks for libOpenGL and libGLX. LEGACY prefers libGL.

grep -n OpenGL_GL_PREFERENCE CMakeLists.txt

2Fix

Set the preference before find_package, or install the GLVND packages from the first cause.

set(OpenGL_GL_PREFERENCE LEGACY)
find_package(OpenGL REQUIRED)

3Verify

Configure again from a clean folder.

rm -rf build
cmake -S . -B build

Confirmed by cmake.org

Did this fix it?

Cause 3 of 3

The target links against the wrong imported target name

1Check

OpenGL::GL is the most portable target on Linux that is not EGL-only. OpenGL::OpenGL exists only on GLVND-based systems.

grep -n 'OpenGL::' CMakeLists.txt

2Fix

Link OpenGL::GL, and OpenGL::GLU only if you use GLU.

target_link_libraries(app PRIVATE OpenGL::GL)

3Verify

Configure and build.

cmake --build build

Confirmed by cmake.org

Did this fix it?

How do I stop it coming back?

  • List the apt packages your build needs in the README or a Dockerfile.
  • Delete the build folder or the CMake cache after installing a missing package.
  • Link imported targets such as OpenGL::GL, not raw library paths.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Servers

dnf cannot install MySQL on Amazon Linux 2023

dnf cannot find a mysql-server package on Amazon Linux 2023

Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.