How to Install CMake on Ubuntu 24.04, 22.04 or 20.04

CMake is a powerful and versatile open-source tool designed to manage the build process of software using a compiler-independent method. It is widely used in software development to control the compilation process across various platforms, generating native build configurations for tools like Make, Ninja, or Visual Studio. CMake supports complex build environments, allowing for custom scripts, automated testing, and packaging.

On Ubuntu 24.04, 22.04, or 20.04, you have two primary methods to install CMake. The first method is via the Ubuntu default repository, which provides a stable and easy installation path for most users. The second method involves downloading and compiling the source archive directly from the official CMake website, giving you access to the latest version and the ability to customize the build process. These methods are also applicable to Ubuntu’s short-term releases.

Update Ubuntu Before CMake Installation

Begin by updating your Ubuntu system to prevent conflicts during the CMake installation. Use the following command to update and upgrade your system packages:

sudo apt update && sudo apt upgrade

Method 1: Install CMake via Ubuntu Default Repository

For convenience and ease of maintenance, installing CMake from Ubuntu’s repository is the recommended approach for most users. Run the following command to install CMake:

sudo apt install cmake

Post-installation, confirm the successful installation of CMake by checking its version:

cmake --version

Method 2: Install CMake by Compiling Source

Compiling from source is the go-to method for users needing CMake’s latest features. This approach requires manual updates, offering the latest version but at the cost of convenience.

Before starting, install the necessary dependencies using:

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

Next, visit the CMake GitHub releases page for the latest version link. Avoid using outdated links; always check for the newest release.

To download the .tar.gz archive, adapt the following command with the latest version link:

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

Important Note: Ensure you select the correct package. For instance, if you’re installing cmake-3.26.0-rc2.tar.gz, don’t mistakenly choose a pre-built version like cmake-3.26.0-rc2-linux-x86_64.tar.gz.

Extract the archive with:

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

Navigate to the extracted directory:

cd cmake-{version number}

Verify that all dependencies are correctly installed before proceeding. Next, initiate the build process by running the Bootstrap script:

./bootstrap

After bootstrap completion, build the package using:

make

To install the compiled CMake, execute:

sudo make install

This step might take a few minutes.

Finally, verify the CMake installation:

cmake --version

Test CMake Installation

Creating a Test Directory

To verify the CMake installation, create a new directory for a simple “Hello, World!” program. In the terminal, execute:

mkdir test-hello && cd test-hello

Setting Up CMakeLists

Next, create a CMakeLists.txt file using the nano editor:

sudo nano CMakeLists.txt

In this file, input the following configuration:

cmake_minimum_required(VERSION 3.16)
project(HelloWorld)
add_executable(hello main.cpp)

Save and exit the editor (CTRL+X, then press Y).

Creating the Main C++ File

Now, create a main.cpp file:

sudo nano main.cpp

Add the basic C++ code to print “Hello, World!”:

#include <iostream>

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

Again, save and exit the editor.

Generating the Makefile

With the files in place, generate a makefile using CMake:

cmake .

Building the Program

Compile the program by running:

make

Running the Program

To execute the program, enter:

./hello

The terminal should display “Hello, World!” confirming that CMake functions correctly on your Ubuntu system.

Conclusion

Installing CMake on your Ubuntu system via either the default repository or by compiling the source ensures that you have the tools necessary for effective cross-platform software development. Using the Ubuntu repository is quick and reliable, while compiling from source offers the flexibility to use the latest version with custom configurations. Regularly updating CMake is crucial for taking advantage of the latest features and improvements. Whether you’re developing for desktop, mobile, or embedded systems, CMake provides the foundation for a streamlined and efficient build process.

Useful Links

Here are some valuable links related to using CMake:

  • CMake Official Website: Visit the website for information about the build system, features, and download options.
  • Getting Started with CMake: Access the guide for a comprehensive introduction to using CMake.
  • CMake Documentation: Explore the official documentation for detailed guides on installing, configuring, and using CMake.
  • CMake Support: Find support resources, including FAQs, forums, and contact information for CMake assistance.
  • CMake GitHub Repository: Access the CMake GitHub repository to view the source code, report issues, and contribute to the development.
  • CMake Releases: You can download the latest versions of CMake and view release notes on GitHub.

2 thoughts on “How to Install CMake on Ubuntu 24.04, 22.04 or 20.04”

    • Hi again Roberto,

      I am still at a loss how the GitHub download link did not show bootstrap and the other additional files, I downloaded Source code (tar.gz) yesterday and today and https://github.com/Kitware/CMake/archive/refs/tags/v3.28.2.tar.gz and it was in it and its exactly the same as the link on https://cmake.org/download.

      Regardless, if it works now that’s great and the main thing, I should put this down as the main download location as its more simple for just grabbing the source to compile, the GitHub page is good if you need a pacific source as it contains much more than the source if you want CMake via another option, but it can be confusing.

      Thanks for the feedback anyway.

      Reply

Leave a Comment