How To Install CMake on CentOS Stream 10

CMake stands as a pivotal tool in modern software development, streamlining the build process across diverse platforms. It acts as a build system generator, creating standardized build files that can be used with native build environments like Make, Ninja, and IDEs such as Visual Studio or Eclipse. This article will guide you through the installation of CMake on CentOS Stream 10, ensuring you can effectively manage your software projects. This guide includes multiple installation methods to suit different needs and preferences. Let’s explore the simple steps to get CMake up and running, enhancing your development workflow.
Prerequisites
Before diving into the installation process, it’s essential to ensure that your system meets certain prerequisites. This preparation avoids potential conflicts and ensures a smooth installation experience. Here’s what you need to get started:
System Requirements
- CentOS Stream 10 Fresh Installation: Begin with a clean CentOS Stream 10 installation to minimize potential conflicts with existing software.
- Root or Sudo Privileges: You’ll need administrative rights to install software. Ensure you have either root access or a user account with sudo privileges.
- Minimum System Specifications: While CMake itself doesn’t demand high resources, ensure your system meets the basic requirements for development work, including sufficient RAM and storage.
- Terminal Access: All installation steps will be performed via the command line, so ensure you have access to a terminal.
Required Base Packages
CentOS Stream 10 requires certain base packages to be installed to support the compilation process. These packages include development tools, compilers, and other essential dependencies.
- Development Tools:
- Install the “Development Tools” group, which provides essential tools like GCC, Make, and other utilities necessary for compiling software.
 
- Compiler Requirements:
- Ensure you have a C++ compiler installed, such as GCC, which is essential for compiling C++ projects using CMake.
 
- Basic Dependencies:
- Install any additional dependencies required by CMake, such as zlib,bzip2, andopenssl.
 
- Install any additional dependencies required by CMake, such as 
Method 1: Installing CMake via DNF
The simplest way to install CMake on CentOS Stream 10 is by using the DNF package manager. This method ensures that you get a stable version of CMake along with automatic updates and dependency management.
System Preparation
Before installing CMake, it’s crucial to update your system to ensure you have the latest packages and dependencies.
- System Update Commands:
- Open your terminal and run the following command to update your system:
sudo dnf update -yThis command updates all installed packages to their latest versions. 
 
- Open your terminal and run the following command to update your system:
- Repository Configuration:
- CentOS Stream 10 typically has the necessary repositories configured by default. However, ensure that the “BaseOS” and “AppStream” repositories are enabled.
 
- Installation of Dependencies:
- Install any missing dependencies that CMake might require. Usually, DNF handles these dependencies automatically, but it’s good to ensure everything is in place.
 
Standard Installation Process
With your system prepared, you can proceed with the CMake installation using DNF.
- DNF Installation Commands:
- Run the following command to install CMake:
sudo dnf install cmake -yThis command downloads and installs CMake from the configured repositories. 
 
- Run the following command to install CMake:
- Version Verification:
- After the installation, verify that CMake is correctly installed by checking its version:
cmake --versionThis command displays the installed CMake version, confirming the successful installation. 
 
- After the installation, verify that CMake is correctly installed by checking its version:
- Basic Configuration Steps:
- CMake doesn’t require extensive configuration after installation. However, ensure that the CMake executable is in your system’s PATH to use it from any directory.
 
Method 2: Installing CMake from Source
Installing CMake from source provides the flexibility to use the latest CMake version or customize the build process. This method involves downloading the source code, compiling it, and then installing CMake on your system.
Downloading Source Code
The first step is to download the CMake source code from the official CMake website or a reliable mirror.
- Obtaining Latest Stable Release:
- Visit the CMake downloads page and find the latest stable release source code (usually a .tar.gzor.tar.bz2file).
- Download the source code using wgetorcurl:wget https://cmake.org/files/v3.x/cmake-3.x.x.tar.gzReplace 3.x.xwith the actual version number.
 
- Visit the CMake downloads page and find the latest stable release source code (usually a 
- Verification of Downloaded Files:
- Verify the integrity of the downloaded file using checksums (SHA256 or MD5) provided on the CMake website. This ensures that the file is not corrupted during the download.
 
- Extraction Process:
- Extract the downloaded source code using the tarcommand:tar -zxvf cmake-3.x.x.tar.gzThis command extracts the source code into a new directory named cmake-3.x.x.
 
- Extract the downloaded source code using the 
Compilation Process
After extracting the source code, navigate into the extracted directory and begin the compilation process.
- Configuration Steps:
- Change into the CMake source directory:
cd cmake-3.x.xCreate a build directory: mkdir build cd buildConfigure the build using CMake: ../configureThis command prepares the build environment. 
 
- Change into the CMake source directory:
- Build Process:
- Compile the source code using the makecommand:makeThis process compiles the CMake source code. It may take some time depending on your system’s resources. 
 
- Compile the source code using the 
- Installation Commands:
- Install CMake using the make installcommand with sudo privileges:sudo make installThis command installs CMake to /usr/local/binby default.
 
- Install CMake using the 
Post-Installation Setup
After installing CMake, configure the environment variables to ensure that CMake can be accessed from any terminal.
- Environment Variable Configuration:
- Add CMake to your system’s PATH by editing your shell’s configuration file (e.g., .bashrcor.zshrc).
 
- Add CMake to your system’s PATH by editing your shell’s configuration file (e.g., 
- Path Settings:
- Open your .bashrcfile using a text editor:nano ~/.bashrcAdd the following line to the end of the file: export PATH="/usr/local/bin:$PATH"Save the file and exit. 
 
- Open your 
- Version Verification:
- Update your current shell session by running:
source ~/.bashrcVerify that CMake is correctly installed by checking its version: cmake --versionThis command displays the installed CMake version, confirming the successful installation. 
 
- Update your current shell session by running:
Method 3: Binary Distribution Installation
Installing CMake using pre-compiled binary distributions is another convenient way to get CMake up and running. This method involves downloading a pre-built binary package and extracting it to a directory in your system.
Download and Preparation
First, you need to download the appropriate binary distribution for CentOS Stream 10 from the CMake website.
- Obtaining Pre-compiled Binaries:
- Visit the CMake downloads page and look for the “Binary distributions” section.
- Download the appropriate binary for Linux (usually a .tar.gzor.shfile).
 
- Verification Steps:
- Verify the integrity of the downloaded file using checksums (SHA256 or MD5) provided on the CMake website.
 
- Directory Setup:
- Create a directory where you want to install CMake. For example:
sudo mkdir /opt/cmake
 
- Create a directory where you want to install CMake. For example:
Installation Steps
After preparing the system and downloading the binary distribution, proceed with the installation.
- Binary Deployment:
- Extract the downloaded binary distribution to the created directory:
sudo tar -zxvf cmake-3.x.x-Linux-x86_64.tar.gz -C /opt/cmakeReplace cmake-3.x.x-Linux-x86_64.tar.gzwith the actual filename of the downloaded binary.
 
- Extract the downloaded binary distribution to the created directory:
- System Integration:
- Create a symbolic link to the CMake executable in /usr/local/binto make it accessible system-wide:sudo ln -s /opt/cmake/cmake-3.x.x-Linux-x86_64/bin/cmake /usr/local/bin/cmakeReplace cmake-3.x.x-Linux-x86_64with the actual directory name.
 
- Create a symbolic link to the CMake executable in 
- Configuration Process:
- Add CMake to your system’s PATH by editing your shell’s configuration file (e.g., .bashrcor.zshrc).
- Open your .bashrcfile using a text editor:nano ~/.bashrcAdd the following line to the end of the file: export PATH="/opt/cmake/cmake-3.x.x-Linux-x86_64/bin:$PATH"Save the file and exit. 
 
- Add CMake to your system’s PATH by editing your shell’s configuration file (e.g., 
Testing the Installation
After installing CMake using any of the methods described above, it’s crucial to test the installation to ensure that it works correctly. This involves checking the CMake version and compiling a basic test project.
Verification Steps
- Version Checking:
- Open your terminal and run the following command:
cmake --versionThis command displays the installed CMake version, confirming the successful installation. 
 
- Open your terminal and run the following command:
- Basic Test Project:
- Create a simple C++ project with a CMakeLists.txtfile to test the compilation process.
 
- Create a simple C++ project with a 
- Compilation Test:
- Create a directory for your test project:
mkdir test_project cd test_projectCreate a simple C++ source file (e.g., main.cpp) with the following content:#include <iostream> int main() { std::cout << "Hello, CMake!" << std::endl; return 0; }Create a CMakeLists.txtfile with the following content:cmake_minimum_required(VERSION 3.10) project(TestProject) add_executable(TestProject main.cpp)Create a build directory: mkdir build cd buildRun CMake to generate the build files: cmake ..Compile the project: makeRun the compiled executable: ./TestProjectIf everything is set up correctly, you should see “Hello, CMake!” printed to the console. 
 
- Create a directory for your test project:
Troubleshooting Common Issues
While installing CMake, you may encounter some common issues. This section provides solutions to these problems, ensuring a smooth installation process.
- Dependency Conflicts:
- If you encounter dependency conflicts, ensure that all required dependencies are installed and that there are no conflicting versions. Use DNF to resolve dependency issues.
 
- Compilation Errors:
- If you encounter compilation errors while building from source, ensure that you have the necessary development tools and compilers installed. Check the error messages for specific details and install any missing libraries.
 
- Path-Related Issues:
- If CMake is not recognized after installation, ensure that the CMake executable is in your system’s PATH. Verify that you have correctly updated your shell’s configuration file (e.g., .bashrcor.zshrc).
 
- If CMake is not recognized after installation, ensure that the CMake executable is in your system’s PATH. Verify that you have correctly updated your shell’s configuration file (e.g., 
- Version Mismatch Solutions:
- If you encounter issues due to version mismatches, ensure that you are using a compatible version of CMake with your project. Check the project’s documentation for specific version requirements.
 
Best Practices and Recommendations
To ensure a secure and efficient development environment, consider the following best practices and recommendations when using CMake.
- Security Considerations:
- Keep your CMake installation up to date to patch security vulnerabilities. Regularly check for updates and apply them promptly.
 
- Performance Optimization:
- Optimize your CMake build configurations for performance. Use appropriate compiler flags and build options to improve compilation times.
 
- Update Management:
- Regularly update CMake to benefit from the latest features and bug fixes. Use DNF or the appropriate method to update your installation.
 
- Backup Procedures:
- Back up your CMake configuration files and project files regularly to prevent data loss. Use version control systems like Git to manage your project files.
 
Congratulations! You have successfully installed CMake. Thanks for using this tutorial to install the CMake on CentOS Stream 10. For additional help or useful information, we recommend you check the official CMake website.
