# Is there a way to detect whether cmake is being run as a sudo or root user?

**URL:** <https://discourse.cmake.org/t/is-there-a-way-to-detect-whether-cmake-is-being-run-as-a-sudo-or-root-user/6340>\
**Category:** Code\
**Created:** [August 25, 2022, 3:20pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-detect-whether-cmake-is-being-run-as-a-sudo-or-root-user/6340 "2022-08-25T15:20:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gonzalo\_Garramuno](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gonzalo_garramuno/32/265_2.png) [@Gonzalo\_Garramuno](https://discourse.cmake.org/u/Gonzalo_Garramuno)\
**Post date:** [August 25, 2022, 3:20pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-detect-whether-cmake-is-being-run-as-a-sudo-or-root-user/6340/1 "2022-08-25T15:20:25Z")

</div>

I would like to detect whether the CMakeLists.txt file is run with sudo priveleges, so that an application for example on macOS can be installed to the /Application directory while if it is not, it installed into CMAKE\_INSTALL\_PREFIX/bin.

Can this be done somehow?

---

<div class="post-metadata">

**Author:** ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)\
**Post date:** [August 25, 2022, 3:25pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-detect-whether-cmake-is-being-run-as-a-sudo-or-root-user/6340/2 "2022-08-25T15:25:28Z")

</div>

A better approach would be to always install to `${CMAKE_INSTALL_PREFIX}/bin`, configure with `-DCMAKE_INSTALL_PREFIX=/Applications/MyApp`, and then install with `sudo cmake --install .`

---

<div class="post-metadata">

**Author:** ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)\
**Post date:** [August 25, 2022, 3:26pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-detect-whether-cmake-is-being-run-as-a-sudo-or-root-user/6340/3 "2022-08-25T15:26:49Z")

</div>

Or even better, use CPack’s productbuild or DragNDrop generator.
