CMake cannot find GoogleTest (find_package(GTest) fails) on Ubuntu
Could NOT find GTestShort answer
find_package(GTest) needs an installed GoogleTest. On Ubuntu install libgtest-dev, delete the CMake cache, and link GTest::gtest or GTest::gtest_main. If GoogleTest lives in a custom prefix, give CMake that prefix. A package built with CMake is found in config mode first.
Fix it fast
1Find out which cause you have
Check whether the package is installed, then configure from a clean build folder so a cached failure is not reused.
dpkg -l libgtest-dev rm -rf build && cmake -S . -B build2Match what you see, then jump to its fix
- 1
dpkg reports no such package, and find_package(GTest REQUIRED) stops configurationGoogleTest is not installedFix - 2
You built GoogleTest yourself and installed it under /opt or your home folderGoogleTest is installed in a prefix CMake does not searchFix - 3
GTest is found, but the build fails at link time with undefined references to testing:: symbols or mainThe test target links the wrong nameFix
- 1
What does the error look like?
Could NOT find GTestfind_package(GTest REQUIRED) stops configuration
Cause 1 of 3
GoogleTest is not installed
1Check
libgtest-dev is in the universe section of Ubuntu 24.04 (version 1.14.0-1).
apt-cache policy libgtest-dev2Fix
Install it, then configure again.
sudo apt install libgtest-dev3Verify
Configure from scratch.
rm -rf build
cmake -S . -B buildConfirmed by packages.ubuntu.com
Cause 2 of 3
GoogleTest is installed in a prefix CMake does not search
1Check
Since CMake 3.20 the module first looks for GTestConfig.cmake from a CMake-built install, then falls back to standard locations. GTEST_ROOT is only a hint for that fallback.
find /opt "$HOME" -name 'GTestConfig.cmake' 2>/dev/null2Fix
Add the install prefix to CMAKE_PREFIX_PATH, or set GTEST_ROOT for the module-mode lookup.
cmake -S . -B build -DCMAKE_PREFIX_PATH=/opt/googletest
# or
cmake -S . -B build -DGTEST_ROOT=/opt/googletest3Verify
The configure step prints that GTest was found.
cmake -S . -B build | grep -i gtestConfirmed by cmake.org
Cause 3 of 3
The test target links the wrong name
1Check
GTest::gtest is the core framework. GTest::gtest_main also supplies main(), so link it only when your tests do not define their own.
grep -n 'gtest' CMakeLists.txt2Fix
Link the imported target that matches your test file.
find_package(GTest REQUIRED)
add_executable(unit_tests tests.cpp)
target_link_libraries(unit_tests PRIVATE GTest::gtest_main)3Verify
Build and run the tests.
cmake --build build
ctest --test-dir buildConfirmed by cmake.org
How do I stop it coming back?
- List libgtest-dev in the build requirements, or fetch GoogleTest with FetchContent for a self-contained build.
- Link imported targets, not paths to .a files.
- Delete the build folder after installing a missing package.
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