How to cmake install
Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.
Last updated: April 4, 2026
Key Facts
- CMake is a meta-build system generator, not a build system itself.
- The 'install' command is defined within the CMakeLists.txt file of the project.
- Common installation prefixes include /usr/local for system-wide installs and ~/local for user-specific installs.
- The CMAKE_INSTALL_PREFIX variable controls the default installation directory.
- Running `make install` or `ninja install` is the typical way to execute the installation step after building.
What is CMake and Why Use It?
CMake is a popular open-source, cross-platform build system generator. It doesn't build your software directly; instead, it generates native build files (like Makefiles for Make or Ninja build files for Ninja) that are then used by the actual build tools to compile your code. This abstraction allows developers to write build instructions once and have them work across various operating systems and compilers.
The 'cmake install' Process Explained
The 'install' step in the CMake workflow is crucial for making your compiled software usable on your system. After configuring and building your project, the installation process copies the built artifacts (executables, libraries, header files, documentation, etc.) to specific directories. This ensures that the software can be found and executed by the operating system or other programs.
Step 1: Configuration with CMake
Before you can install, you need to configure your project. This is done by running the cmake command, usually followed by the path to your project's source directory (or the build directory if you're doing an out-of-source build). A common command looks like this:
mkdir buildcd buildcmake ..During this phase, CMake reads your project's CMakeLists.txt file, checks for dependencies, determines your system's capabilities, and generates the appropriate build files for your chosen generator (e.g., Makefiles). You can also specify configuration options here, such as the installation prefix:
cmake .. -DCMAKE_INSTALL_PREFIX=/path/to/installThe CMAKE_INSTALL_PREFIX variable is a fundamental CMake variable that dictates the base directory where files will be installed. If not specified, it often defaults to /usr/local on Unix-like systems, which is a standard location for user-compiled software to avoid conflicts with system packages.
Step 2: Building the Project
Once configured, you use your system's build tool to compile the project. If CMake generated Makefiles, you'd typically run:
makeIf you used a different generator, like Ninja, you would run:
ninjaThis step compiles all the source code and links it into executables and libraries.
Step 3: Installation
The final step is installation. This is where the install targets defined in the CMakeLists.txt file are invoked. You execute this using your build tool:
make install# orninja installThis command reads the installation rules specified in the CMakeLists.txt file. These rules tell CMake where to copy each file (e.g., executables go in bin, libraries in lib, headers in include, documentation in share/doc) relative to the CMAKE_INSTALL_PREFIX.
Defining Install Rules in CMakeLists.txt
The power of cmake install lies in the flexibility of defining installation rules within the CMakeLists.txt file. Developers use commands like install() to specify which files or directories should be installed, their destination, and other properties.
Here's a simplified example:
# Install executableinstall(TARGETS my_executable DESTINATION bin)# Install libraryinstall(TARGETS my_library DESTINATION lib)# Install header fileinstall(FILES include/my_header.h DESTINATION include)# Install data filesinstall(DIRECTORY data/ DESTINATION share/my_project/data)
The DESTINATION keyword specifies the subdirectory relative to CMAKE_INSTALL_PREFIX.
Common Issues and Best Practices
- Permissions: Installing to system directories like
/usr/localoften requires administrator privileges (usingsudo make installon Linux/macOS). Installing to a user-owned directory avoids this. - Out-of-Source Builds: Always perform builds and installations from a separate build directory to keep your source directory clean.
- Uninstall Targets: CMake can also generate an
uninstalltarget if configured correctly, allowing you to remove installed files. This is often defined usinginstall(SCRIPT ...)or by explicitly listing files for uninstallation. - Package Managers: For system-wide distribution, consider packaging your software using system package managers (like apt, yum, brew) rather than direct installation, as they handle dependencies and uninstallation more robustly.
In summary, cmake install is the final step in the CMake build process that makes your compiled project available on your system by copying its components to designated locations, controlled by rules defined in the project's CMake configuration.
More How To in Daily Life
Also in Daily Life
More "How To" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- cmake(1) Manual - CMakeBSD-3-Clause
- install — CMake 3.29.1 DocumentationBSD-3-Clause
- Install Documentation - CMake Community WikiCC-BY-SA-4.0
Missing an answer?
Suggest a question and we'll generate an answer for it.