Skip to content

Instantly share code, notes, and snippets.

@Cartexius
Last active November 7, 2024 00:57
Show Gist options
  • Save Cartexius/4c437c084d6e388288201aadf9c8cdd5 to your computer and use it in GitHub Desktop.
Save Cartexius/4c437c084d6e388288201aadf9c8cdd5 to your computer and use it in GitHub Desktop.
Install gtest in Ubuntu

Reference - https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/

sudo apt-get install libgtest-dev

sudo apt-get install cmake # install cmake

cd /usr/src/gtest

sudo cmake CMakeLists.txt

sudo make

sudo cp *.a /usr/lib

sudo ln -s /usr/lib/libgtest.a /usr/local/lib/gtest/libgtest.a

sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/gtest/libgtest_main.a

@pareshBloomA
Copy link

@basil59, I agree with you, but looking at www.askubuntu.com stack exchange reference below shows that libgtest-dev is now stored in /usr/src/googletest/googletest. So it might be a good idea to make symbolic links to googletest folder. Of course, I could be wrong, and this would be dependent on the package you are trying to use gtest for, and may require you to just use /usr/local/lib/gtest.

Reference to answer for 17.04 and 18.04 libgtest-dev: https://askubuntu.com/a/145913

sudo apt-get install libgtest-dev
cd /usr/src/googletest/googletest
sudo mkdir build
cd build
sudo cmake ..
sudo make
sudo cp libgtest* /usr/lib/
cd ..
sudo rm -rf build

Then do:

sudo mkdir /usr/local/lib/googletest
sudo ln -s /usr/lib/libgtest.a /usr/local/lib/googletest/libgtest.a
sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/googletest/libgtest_main.a

@StrayDragon
Copy link

@basil59, I agree with you, but looking at www.askubuntu.com stack exchange reference below shows that libgtest-dev is now stored in /usr/src/googletest/googletest. So it might be a good idea to make symbolic links to googletest folder. Of course, I could be wrong, and this would be dependent on the package you are trying to use gtest for, and may require you to just use /usr/local/lib/gtest.

Reference to answer for 17.04 and 18.04 libgtest-dev: https://askubuntu.com/a/145913

sudo apt-get install libgtest-dev
cd /usr/src/googletest/googletest
sudo mkdir build
cd build
sudo cmake ..
sudo make
sudo cp libgtest* /usr/lib/
cd ..
sudo rm -rf build

Then do:

sudo mkdir /usr/local/lib/googletest
sudo ln -s /usr/lib/libgtest.a /usr/local/lib/googletest/libgtest.a
sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/googletest/libgtest_main.a

Thank for your summary, It works very well. 😄

@robotsorcerer
Copy link

Excellent @pareshBloomA! Thanks!

@jcmonteiro
Copy link

jcmonteiro commented Aug 5, 2019

It is (almost) never a good idea to install files by moving them into /usr/* directories.
As a good practice, also create a build directory so the source directory does not get messed up.

cd /usr/src/gtest
sudo mkdir build && cd build
sudo cmake ..
sudo make install

@liuqun
Copy link

liuqun commented Aug 20, 2019

It is (almost) never a good idea to install files by moving them into /usr/* directories.
As a good practice, also create a build directory so the source directory does not get messed up.

cd /usr/src/gtest
sudo mkdir build && cd build
sudo cmake ..
sudo make install

the build dir can be put anywhere, such as /tmp/build or ~/build

sudo apt-get install -y libgtest-dev cmake

mkdir -p $(HOME)/build
cd $(HOME)/build
cmake /usr/src/gtest

@jcmonteiro
Copy link

That's even better. Just don't forget that, if you did not change the CMAKE_INSTALL_PREFIX, sudo is still needed to install via sudo make install.

@dlime
Copy link

dlime commented Jan 31, 2020

I have no 'install' target by compiling /usr/src/gtest in ubuntu 19.04 (I did sudo apt-get install -y lgoogletest ibgtest-dev cmake)

@dlime
Copy link

dlime commented Jan 31, 2020

Hello guys, I've managed to install GTest & Gmock from the git repo:
https://gist.github.com/dlime/313f74fd23e4267c4a915086b84c7d3d

@georgegoldman
Copy link

so which is better approach ?

@georgegoldman
Copy link

what i got when i ran sudo cp libgtest* /usr/lib/
``
Uploading Screenshot from 2023-11-04 23-01-32.png…

@mamadrabiie
Copy link

mamadrabiie commented Nov 10, 2023

@georgegoldman I can't see the photo you uploaded, but I reckon your error relates to this:
Apparently, libgtest* files will be stored inside the lib directory so instead, you should run
sudo cp lib/libgtest* /usr/lib/

I combined all the answers above and minorly modified it, it worked on GitHub actions with -ubuntu-latest as well.

sudo apt-get install -y libgtest-dev cmake
mkdir -p $HOME/build
cd $HOME/build
sudo cmake /usr/src/googletest/googletest
sudo make
sudo cp lib/libgtest* /usr/lib/
cd ..
sudo rm -rf build
sudo mkdir /usr/local/lib/googletest
sudo ln -s /usr/lib/libgtest.a /usr/local/lib/googletest/libgtest.a
sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/googletest/libgtest_main.a

@kumailxp
Copy link

Thanks! Works like awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment