CMake says Could NOT find OpenGL on Ubuntu
Could NOT find OpenGLShort 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.
Fix it fast
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 build2Match what you see, then jump to its fix
- 1
The missing: list names an OpenGL include or library variableThe OpenGL development files are not installedFix - 2
The libraries OPENGL_opengl_LIBRARY and OPENGL_glx_LIBRARY are the ones missingCMake prefers GLVND but you only have the legacy libGLFix - 3
OpenGL is found, but linking or configuring fails on a target nameThe target links against the wrong imported target nameFix
- 1
What does the error look like?
Could NOT find OpenGLfind_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-dev3Verify
Delete the build folder, because CMake caches the failed lookup, and configure again.
rm -rf build
cmake -S . -B buildConfirmed by packages.ubuntu.com
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.txt2Fix
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 buildConfirmed by cmake.org
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.txt2Fix
Link OpenGL::GL, and OpenGL::GLU only if you use GLU.
target_link_libraries(app PRIVATE OpenGL::GL)3Verify
Configure and build.
cmake --build buildConfirmed by cmake.org
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 fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
Apache warns that a ${VAR} config variable is not defined
${VAR} left unchanged in the configuration, with a warning in the error log
Apache expands ${VAR} from Define or from the shell environment that exists when the server starts.
3 causes, each with a fix