How to Install CMake on Rocky Linux

CMake is an open-source build system generator that creates native build files for any platform and compiler. Instead of writing separate build scripts for different environments, you define your build process once in a CMakeLists.txt file, and CMake generates the appropriate Makefiles, Ninja files, or IDE project files. This makes CMake essential for cross-platform C and C++ development, game engine builds, and large-scale software projects with complex dependencies.

This guide covers two installation methods for Rocky Linux: the AppStream repository for stable, system-integrated packages, and source compilation for access to the latest CMake features. By the end, you will have a working CMake installation verified and ready for building software projects.

Update Rocky Linux Before CMake Installation

Before installing any software, update your system packages to ensure you have the latest security patches and dependency versions. This prevents conflicts during installation and ensures compatibility with the CMake build process.

sudo dnf upgrade --refresh

Choose Your CMake Installation Method

Rocky Linux offers CMake through its AppStream repository, which provides a tested and stable version that integrates with the system package manager. Alternatively, you can compile CMake from source to access the latest release with new features and improvements. The table below summarizes both approaches to help you decide which fits your workflow.

MethodChannelVersionUpdatesBest For
DNF (AppStream)Rocky AppStreamDistribution defaultAutomatic via dnf upgradeMost users who want stability and easy maintenance
Source CompilationGitHub ReleasesLatest stableManual recompilation requiredDevelopers needing latest features or custom build flags

We recommend the DNF method for most users because it provides automatic security updates and requires minimal maintenance. Only compile from source if you specifically need features unavailable in the AppStream version or require custom compilation options.

CMake Versions by Rocky Linux Release

The CMake version available through the AppStream repository varies depending on your Rocky Linux release:

Rocky Linux VersionAppStream CMake Version
Rocky Linux 103.30.x
Rocky Linux 93.26.x
Rocky Linux 83.26.x

If your project requires a newer CMake version than your distribution provides, use the source compilation method documented later in this guide.

Install CMake via DNF (AppStream)

The AppStream repository provides a stable CMake version that receives security updates through the standard dnf upgrade process. This straightforward method works well for production systems where stability matters more than having the absolute latest version.

To install CMake along with its required dependencies, run the following command:

sudo dnf install cmake

DNF will display the packages to be installed and prompt for confirmation. After installation completes, verify that CMake is accessible by checking its version:

cmake --version

Example output on Rocky Linux 10:

cmake version 3.30.5

CMake suite maintained and supported by Kitware (kitware.com/cmake).

This output confirms that you installed CMake correctly and that it is available in your system PATH. You can now use CMake to configure and build software projects.

Install CMake via Source Compilation

Compiling CMake from source provides access to the latest stable release, which may include new generator support, improved performance, or bug fixes not yet available in the AppStream repository. This method requires manual updates when new versions are released, so it is best suited for development environments where staying current is important.

Install Build Dependencies

CMake’s bootstrap process requires a C++ compiler, OpenSSL development headers for HTTPS support in CMake’s file download commands, and standard build utilities. If you are also compiling kernel modules or other system-level software, you may want to install Linux kernel headers on Rocky Linux as well. Install the CMake build dependencies with the following command:

sudo dnf install gcc gcc-c++ openssl-devel make curl wget tar -y

The -y flag automatically confirms the installation prompt, which is useful for scripted deployments but means you will not see the package list before installation proceeds.

Download and Extract CMake Source

First, create a build directory in your home folder to keep the source files organized. Then use the GitHub API to detect the latest stable version and download the corresponding source archive. This approach ensures you always get the current release without manually checking the releases page.

mkdir -p ~/cmake-build && cd ~/cmake-build
VERSION=$(curl -s https://api.github.com/repos/Kitware/CMake/releases/latest | grep -oP '"tag_name": "v\K[^"]+')
echo "Downloading CMake version: ${VERSION}"
wget -q https://github.com/Kitware/CMake/releases/download/v${VERSION}/cmake-${VERSION}.tar.gz

Expected output:

Downloading CMake version: 4.2.1

If the GitHub API is unavailable or you prefer to specify a version manually, visit the CMake releases page and replace ${VERSION} with the desired version number in the download command.

Next, extract the downloaded archive and navigate into the source directory:

tar -xzf cmake-${VERSION}.tar.gz
cd cmake-${VERSION}

Configure and Compile CMake

CMake uses a bootstrap script rather than autoconf for its initial configuration. This script detects your compiler, verifies that required libraries are present, and prepares the build system. Run the bootstrap process with:

./bootstrap

The bootstrap process compiles a minimal CMake binary first, then uses it to configure the full build. This takes several minutes depending on your system’s CPU and disk speed. When complete, you will see output similar to:

---------------------------------------------
CMake has bootstrapped.  Now run make.

After bootstrap completes successfully, compile CMake using all available CPU cores to speed up the build:

make -j$(nproc)

The -j$(nproc) flag tells make to run parallel compilation jobs equal to your CPU core count. This significantly reduces build time on multi-core systems. Compilation typically takes 5-15 minutes depending on your hardware.

Install CMake System-Wide

Once compilation finishes, install CMake to /usr/local using sudo. This places the binary in /usr/local/bin, which Rocky Linux includes in the default PATH:

sudo make install

After installation, verify the source-compiled version is active:

cmake --version

Expected output:

cmake version 4.2.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

The version number confirms that the source-compiled CMake is now your default installation. The /usr/local/bin directory takes precedence over /usr/bin in the standard PATH, so your system uses the source-compiled version even if you also have the AppStream version installed.

Update CMake from Source

Unlike the DNF installation which updates automatically, source-compiled CMake requires manual updates. The following script automates the update process by detecting the latest version, comparing it to your installed version, and recompiling if an update is available.

Create the update script using nano:

nano ~/update-cmake.sh

Paste the following content into the editor:

#!/bin/bash
# CMake Source Update Script
# Downloads and compiles the latest stable CMake release

set -e  # Exit on any error

# Check for required tools
for cmd in curl wget tar make; do
    if ! command -v $cmd &> /dev/null; then
        echo "Error: $cmd is required but not installed."
        exit 1
    fi
done

echo "Checking for latest CMake version..."
LATEST=$(curl -s https://api.github.com/repos/Kitware/CMake/releases/latest | grep -oP '"tag_name": "v\K[^"]+')

if [ -z "$LATEST" ]; then
    echo "Error: Could not detect latest version from GitHub API."
    exit 1
fi

CURRENT=$(cmake --version 2>/dev/null | head -1 | grep -oP '\d+\.\d+\.\d+' || echo "not installed")

echo "Current installed version: ${CURRENT}"
echo "Latest available version:  ${LATEST}"
echo ""

if [ "$CURRENT" = "$LATEST" ]; then
    echo "CMake is already up to date."
    exit 0
fi

read -p "Continue with update? (y/n) " -n 1 -r
echo ""

if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Update cancelled."
    exit 0
fi

BUILD_DIR="$HOME/cmake-build"
mkdir -p "${BUILD_DIR}"
cd "${BUILD_DIR}"

echo "Downloading CMake ${LATEST}..."
wget -q "https://github.com/Kitware/CMake/releases/download/v${LATEST}/cmake-${LATEST}.tar.gz"

echo "Extracting source code..."
tar -xzf "cmake-${LATEST}.tar.gz"
cd "cmake-${LATEST}"

echo "Running bootstrap (this may take a few minutes)..."
./bootstrap

echo "Compiling CMake (this may take 5-15 minutes)..."
make -j$(nproc)

echo "Installing CMake (requires sudo password)..."
sudo make install

echo ""
echo "Update complete!"
cmake --version

Save the file by pressing Ctrl+O, then Enter, and exit with Ctrl+X. Make the script executable and run it when you want to check for updates:

chmod +x ~/update-cmake.sh
~/update-cmake.sh

Example output when an update is available:

Checking for latest CMake version...
Current installed version: 4.2.0
Latest available version:  4.2.1

Continue with update? (y/n) y

Downloading CMake 4.2.1...
Extracting source code...
Running bootstrap (this may take a few minutes)...
Compiling CMake (this may take 5-15 minutes)...
Installing CMake (requires sudo password)...

Update complete!
cmake version 4.2.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

Run this script manually when you want to update. Avoid automating it with cron because compilation requires significant CPU resources and the sudo password prompt would fail in non-interactive environments.

Troubleshoot CMake Installation

Bootstrap Fails with Missing Compiler

If the bootstrap script fails with an error about missing compilers, the dependency installation did not complete successfully. Check that the C++ compiler is available:

g++ --version

If this command fails or shows “command not found,” install the compiler packages:

sudo dnf install gcc gcc-c++ -y

Then run the bootstrap script again.

Wrong CMake Version After Source Installation

If cmake --version shows the AppStream version instead of your source-compiled version, the system is finding the /usr/bin/cmake binary before /usr/local/bin/cmake. Verify which binary is being executed:

which cmake

Expected output for source-compiled installation:

/usr/local/bin/cmake

If it shows /usr/bin/cmake instead, either remove the AppStream version with sudo dnf remove cmake or ensure /usr/local/bin appears before /usr/bin in your PATH environment variable.

Build Directory Cleanup

After a successful installation, you can remove the build directory to free disk space. The installation placed the compiled binary in /usr/local, so the source files are no longer needed:

rm -rf ~/cmake-build

If you plan to update frequently, keep the build directory to save time on subsequent updates, as make can reuse some build artifacts.

Remove CMake from Rocky Linux

If you no longer need CMake, the removal process depends on which installation method you used.

Remove DNF-Installed CMake

For the AppStream installation, DNF removes the package and its unused dependencies automatically:

sudo dnf remove cmake

DNF defaults to removing orphaned dependencies when uninstalling packages, so it automatically cleans up cmake-data, cmake-filesystem, libuv, and other packages it installed with CMake.

Remove Source-Compiled CMake

Source-compiled software does not have an automatic uninstall mechanism. Remove the installed files manually by targeting the specific CMake binaries and data directories:

The following commands permanently delete files from /usr/local. Double-check the paths before running them to avoid accidentally removing other software installed to the same prefix.

sudo rm -rf /usr/local/bin/cmake /usr/local/bin/ctest /usr/local/bin/cpack /usr/local/bin/ccmake
sudo rm -rf /usr/local/share/cmake-*
sudo rm -rf /usr/local/doc/cmake-*

Also clean up the build directory if you kept it:

rm -rf ~/cmake-build
rm -f ~/update-cmake.sh

Confirm the removal succeeded by checking the version command:

cmake --version

Expected output after removal:

-bash: cmake: command not found

This confirms that you successfully removed CMake from your system.

Conclusion

You now have CMake installed on Rocky Linux, ready to configure and build C/C++ projects. The AppStream method provides automatic updates and minimal maintenance, while source compilation gives you access to the latest features when your projects require them. With the update script in place, keeping a source-compiled installation current requires only a single command. For more details on using CMake in your projects, refer to CMake’s official documentation.

Leave a Comment

Let us know you are human: