How to Install CMake on Debian 12, 11 or 10

Diving into software development on Debian platforms requires a reliable build system, and CMake stands out as a top choice. This guide focuses on installing CMake on Debian 12 Bookworm, Debian 11 Bullseye, or Debian 10 Buster. With CMake, developers gain a versatile tool that streamlines the creation of software applications across diverse platforms, from Linux and macOS to Windows.

Below are some of the key attributes of CMake for Debian Users:

  • Cross-Platform Support: CMake’s adaptability ensures your projects remain buildable across many operating systems.
  • Language Versatility: Whether your project is in C, C++, or Fortran, CMake has you covered.
  • Modularity: Craft reusable modules and scripts with CMake, enhancing project consistency.
  • Build System Flexibility: CMake can generate the necessary build files from Make and Ninja to Xcode and Visual Studio.
  • Scalability: Tackle large projects with intricate dependencies effortlessly, thanks to CMake’s design.
  • Customization: Tailor your build logic with CMake’s scripting capabilities to meet specific project requirements.

Considering these features, the subsequent sections of this guide will delve into the installation processes for CMake on Debian, covering both the APT method and source compilation.

Method 1: Install CMake via APT

This section will discuss installing CMake on Debian using the APT package manager. This method is recommended for most users, as it is straightforward and utilizes the default Debian repositories.

Update the Debian System Before Installing CMake

Before installing any new software, it is essential to update your Debian operating system to ensure all existing packages are up to date. This step helps minimize potential conflicts and ensures a smoother installation process. Execute the following command to update your system:

sudo apt update && sudo apt upgrade

Install CMake via APT Command

Now that your system is up-to-date, you can install CMake from Debian’s repository. This method is convenient because it automatically takes care of any required dependencies. To begin the installation, use the following command:

sudo apt install cmake

Confirm that the CMake Version is Installed

Once the installation is complete, it’s a good practice to confirm that CMake has been installed correctly by checking its version. This step also provides the installed version number, which can help verify compatibility with specific projects or build systems. Run the following command to check the version of CMake:

cmake --version

Method 2: Install CMake via source

This section will discuss an alternative method for installing CMake on Debian – compiling it from the source code. This approach is suitable for users who need the latest version of CMake or want more control over the installation process. Remember that this method requires manual updates by downloading and re-compiling newer versions.

Install Required Packages for CMake Installation

Before compiling CMake from the source, you must install the necessary packages and tools. Run the following command to install these dependencies:

sudo apt install build-essential checkinstall zlib1g-dev libssl-dev -y

Download the CMake Version of Choice

First, visit the CMake GitHub releases page and grab the download link for the latest version. Remember to check this page regularly, as the example link below will become outdated.

Next, use the wget command to download the archive file:

wget https://github.com/Kitware/CMake/releases/download/{version}/cmake-{version}.tar.gz

For example, if you want to download version 3.27.1, use the following command:

wget https://github.com/Kitware/CMake/releases/download/v3.27.1/cmake-3.27.1.tar.gz

After downloading the archive, extract its contents using the following command:

tar -zxvf cmake-{version number}.tar.gz

Note: Remember to replace {version number} with the current version you downloaded, as it should differ from the example in this guide.

Now, change to the extracted directory:

cd cmake-{version number}

Run Bootstrap Script for CMake Installation

In this step, you will run the bootstrap script to configure the CMake build. If you encounter any issues, double-check that all required dependencies have been installed.

./bootstrap

The bootstrap script may take a few minutes to complete.

If the bootstrap script is successful, you should see a similar output in your Debian terminal:

Once it’s done, use the make command to build the package:

make

Alternatively, you can run gmake:

gmake

Once the package has been built, you should see a similar output in your Debian terminal:

Finalize CMake Installation via “make-install”

Now that the package has been built, install CMake using the following make install command:

sudo make install

The installation process may take several minutes, so feel free to take a short break.

Confirm CMake Installation

After the installation is complete, verify that CMake has been installed correctly by checking its version:

make --version

Test CMake Installation with a Sample Program

This section will demonstrate how to test your CMake installation by creating and building a simple test program. This process will help you verify that CMake functions correctly on your Debian system.

Create a Project Directory for the CMake Test

First, create a new directory for your test project:

mkdir cmake-test-project

Navigate to the newly created directory:

cd cmake-test-project

Write a Simple C++ Program for the CMake Test

Create a new C++ file called main.cpp and open it in your favorite text editor:

nano main.cpp

Add the following code to main.cpp:

#include <iostream>

int main() {
    std::cout << "Hello, CMake!" << std::endl;
    return 0;
}

Save and close the file.

Create a CMakeLists.txt File for the CMake Test

In the root of your project directory, create a new file named CMakeLists.txt and open it in a text editor:

nano CMakeLists.txt

Add the following content to CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(cmake_test_project)

set(CMAKE_CXX_STANDARD 14)

add_executable(cmake_test_project main.cpp)

This CMakeLists file defines the minimum required CMake version, sets the project name, specifies the C++ standard, and creates an executable named cmake_test_project from the main.cpp file.

Save and close the file.

Configure and Build the Test Program with CMake

Now, create a new directory called build inside your project directory:

mkdir build && cd build

Run the following command to configure the project using CMake:

cmake ..

Next, build the test program using the following command:

make

This command will compile the main.cpp file and generate an executable called cmake_test_project.

Step 5: Run the Test Program to Verify CMake Installation on Debian

Finally, run the test program using the following command:

./cmake_test_project

If everything has been set up correctly, you should see the output “Hello, CMake!” printed on the console. This confirms that your CMake installation is working correctly on your Debian system.

Conclusion

In conclusion, we’ve covered two approaches to installing CMake on Debian Linux: the straightforward APT package manager and the more customizable source compilation. The best method for you depends on your specific needs and technical comfort with Debian. Whichever method you choose, CMake will enhance your software development by simplifying and streamlining the build process across various platforms. Happy building!

Leave a Comment