This page explains how to install the Google Test framework on Ubuntu.

Install dependencies and tools

First, install GCC, CMake, GNU Make and Git if you haven't already.

sudo apt update
sudo apt install gcc g++ cmake make git

Download, build and install

#!/usr/bin/env bash

# Script to download and install GoogleTest

set -ex

version="release-1.10.0" # Release tag on GitHub
prefix="$HOME/.local"

cd /tmp
# Download
git clone --single-branch --depth=1 --branch "$version" \
    https://github.com/google/googletest.git
mkdir googletest/build && cd $_
# Configure
cmake .. -DCMAKE_INSTALL_PREFIX="$prefix" -DCMAKE_BUILD_TYPE=Release
# Build
make -j$(nproc)
# Install
make install

Usage

To use GoogleTest in your project, you can use the following CMake snippet:

enable_testing()
find_package(GTest REQUIRED)
include(GoogleTest)

add_executable(test-exe test.cpp) # test.cpp contains TEST(...) cases
target_link_libraries(test-exe PRIVATE GTest::gtest_main)

if(NOT CMAKE_CROSSCOMPILING)
    gtest_discover_tests(test-exe)
endif()

CMake has to find GoogleTest, so you need to make sure that $HOME/.local is in its search paths. See Installing Locally.

Tested on