How to Install CMake on Ubuntu? [SOLVED]


Ubuntu

Author: Omer Cakmak
Reviewer: Deepak Prasad

CMake is an open-source build system generator that is used to manage the build process of software projects. It is an essential tool for developers who work with C, C++, or other programming languages. CMake can be used to generate makefiles, build project files for various IDEs, and configure software builds. If you are a developer working on Ubuntu, you may need to install CMake to manage the build process for your projects.

In this article, we will discuss how to install CMake on Ubuntu, including the necessary prerequisites, installation methods, and how to verify that the installation was successful. By following the steps outlined in this guide, you will be able to install CMake and start using it to manage the build process of your software projects on Ubuntu.

 

Pre-requisites

Update the apt package manager:

Run the following command to update the apt package manager to ensure that you have the latest package information:

sudo apt update

Install the build-essential package:

The build-essential package includes the necessary compilers and libraries required to build software on Ubuntu. Run the following command to install the build-essential package:

sudo apt install build-essential

Install the cmake-data package:

The cmake-data package contains data files required by CMake. Run the following command to install the cmake-data package:

sudo apt install cmake-data

After installing these prerequisites, you are now ready to install CMake on Ubuntu using one of the installation methods available.

 

Different methods to install CMake on Ubuntu

Since the CMake package is open source software, it is available in the Ubuntu repositories. You can also install from source code and binary file. In addition, a compiled CMake official page has been published, you can download and use it directly. Now let's explain these 4 methods.

 

Method-1: Install from Repository

Check what version of CMake is in the Ubuntu repository:

$ sudo apt search cmake
...
cmake/jammy-updates 3.22.1-1ubuntu1.22.04.1 amd64
  cross-platform, open-source make system
...

Now install cmake on Ubuntu using apt command:

$ sudo apt install cmake -y

Installed CMake version:

$ cmake --version
cmake version 3.22.1

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

 

Method-2: Install From Binary File

In this method, we will install it by downloading the .run binary file from the CMake official page.

Install CMake on Ubuntu

First, pull the cmake-3.26.1-linux-x86_64.sh file with wget:

$ wget https://github.com/Kitware/CMake/releases/download/v3.26.1/cmake-3.26.1-linux-x86_64.sh
--2023-03-25 20:18:12--  https://github.com/Kitware/CMake/releases/download/v3.26.1/cmake-3.26.1-linux-x86_64.sh
Resolving github.com (github.com)... 140.82.121.4
Connecting to github.com (github.com)|140.82.121.4|:443... connected.
Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/537699/388c0513-ecd4-4970-a673-599c75fa03e6?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20230325%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230325T201813Z&X-Amz-Expires=300&X-Amz-Signature=74344d99cb32467d7625edf9ec0d759374dbcf1f551ee957ea99bf6fc19fe998&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=537699&response-content-disposition=attachment%3B%20filename%3Dcmake-3.26.1-linux-x86_64.sh&response-content-type=application%2Foctet-stream [following]
--2023-03-25 20:18:13--  https://objects.githubusercontent.com/github-production-release-asset-2e65be/537699/388c0513-ecd4-4970-a673-599c75fa03e6?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20230325%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230325T201813Z&X-Amz-Expires=300&X-Amz-Signature=74344d99cb32467d7625edf9ec0d759374dbcf1f551ee957ea99bf6fc19fe998&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=537699&response-content-disposition=attachment%3B%20filename%3Dcmake-3.26.1-linux-x86_64.sh&response-content-type=application%2Foctet-stream
Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.110.133, 185.199.108.133, 185.199.109.133, ...
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.110.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 48319283 (46M) [application/octet-stream]
Saving to: ‘cmake-3.26.1-linux-x86_64.sh’

cmake-3.26.1-linu 100%[=============>]  46.08M  2.71MB/s    in 16s     

2023-03-25 20:18:30 (2.86 MB/s) - ‘cmake-3.26.1-linux-x86_64.sh’ saved [48319283/48319283]

Then give the binary file execute permission:

$ chmod +x cmake-3.26.1-linux-x86_64.sh

Then run the file:

$ sudo sh cmake-3.26.1-linux-x86_64.sh 
CMake Installer Version: 3.26.1, Copyright (c) Kitware
This is a self-extracting archive.
The archive will be extracted to: /home/foc

...
The following individuals and institutions are among the Contributors:

Do you accept the license? [yn]: 
y
By default the CMake will be installed in:
  "/home/foc/cmake-3.26.1-linux-x86_64"
Do you want to include the subdirectory cmake-3.26.1-linux-x86_64?
Saying no will install in: "/home/foc" [Yn]: 
Y

Using target directory: /home/foc/cmake-3.26.1-linux-x86_64
Extracting, please wait...

Unpacking finished successfully

Move the creating cmake directory under opt:

$ sudo mv cmake-3.26.1-linux-x86_64 /opt

Link the cmake executable:

$ sudo ln -s /opt/cmake-3.26.1-linux-x86_64/bin/cmake /usr/local/bin/cmake

You can now use it by calling cmake from the terminal. CMake version:

$ cmake --version
cmake version 3.26.1

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

 

Method-3: Install From .tar.gz File

In this method, we will download the .tar.gz file published on the CMake official page and copy it to the system.

Pull cmake-3.26.1-linux-x86_64.tar.gz to the system with wget:

$ wget https://github.com/Kitware/CMake/releases/download/v3.26.1/cmake-3.26.1-linux-x86_64.tar.gz
--2023-03-25 20:28:39--  https://github.com/Kitware/CMake/releases/download/v3.26.1/cmake-3.26.1-linux-x86_64.tar.gz
...
Length: 48314038 (46M) [application/octet-stream]
Saving to: ‘cmake-3.26.1-linux-x86_64.tar.gz’

cmake-3.26.1-linu 100%[=============>]  46.08M  2.75MB/s    in 16s     

2023-03-25 20:28:56 (2.91 MB/s) - ‘cmake-3.26.1-linux-x86_64.tar.gz’ saved [48314038/48314038]

Extract the downloaded file:

$ tar xvf cmake-3.26.1-linux-x86_64.tar.gz

Move the file under the opt directory:

$ sudo mv cmake-3.26.1-linux-x86_64 /opt

Link the cmake file:

$ sudo ln -s /opt/cmake-3.26.1-linux-x86_64/bin/cmake /usr/local/bin/cmake

CMake version:

$ cmake --version
cmake version 3.26.1

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

 

Method-4: Install From Source Code

In this method, we will explain how to compile CMake from source code.

How to Install CMake on Ubuntu? [SOLVED]

Extract cmake-3.26.1.tar.gz with wget:

$ wget https://github.com/Kitware/CMake/releases/download/v3.26.1/cmake-3.26.1.tar.gz
--2023-03-25 20:32:44--  https://github.com/Kitware/CMake/releases/download/v3.26.1/cmake-3.26.1.tar.gz
...
HTTP request sent, awaiting response... 200 OK
Length: 10667838 (10M) [application/octet-stream]
Saving to: ‘cmake-3.26.1.tar.gz’

cmake-3.26.1.tar. 100%[=============>]  10.17M  3.43MB/s    in 3.0s    

2023-03-25 20:32:49 (3.43 MB/s) - ‘cmake-3.26.1.tar.gz’ saved [10667838/10667838]

Before starting the build, confirm that the following dependencies are installed:

$ sudo apt install libssl-dev g++

Extract the downloaded .tar.gz file:

$ tar xvf cmake-3.26.1.tar.gz

Enter the extracted directory:

$ cd cmake-3.26.1/

Run the bootstrap:

$ ./bootstrap
...
-- Configuring done (32.8s)
-- Generating done (0.5s)
-- Build files have been written to: /home/foc/cmake-3.26.1
---------------------------------------------
CMake has bootstrapped. Now run gmake.

Then run make

$ make

and make install:

$ sudo make install

When compiling cmake is placed in the relevant directories:

$ which cmake
/usr/local/bin/cmake

$ cmake --version
cmake version 3.26.1

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

CMake compiled from the source code was installed on the system.

 

Verifying the installation and testing CMake

After installing CMake on Ubuntu, it is important to verify that the installation was successful and test that CMake is working as expected. Here are the steps to verify and test the installation:

Verify the CMake version:

To check the version of CMake that was installed on your system, run the following command:

# cmake --version
cmake version 3.22.1

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

This command should display the version number of CMake installed on your system.

Create a simple CMake project:

Create a new directory for your CMake project and create a file named CMakeLists.txt in that directory. Add the following lines to the CMakeLists.txt file:

cmake_minimum_required(VERSION 3.0)
project(hello_world)
add_executable(hello_world hello_world.cpp)

Create a new file named hello_world.cpp in the same directory and add the following code to the file:

#include <iostream>

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

Generate build files with CMake:

In your project directory, run the following command to generate the build files with CMake:

# cmake .
-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp

This command should generate build files for your project. You should see a Makefile and other build files in the directory.

Build the project:

Run the following command to build the project using the generated build files:

# make
[ 50%] Building CXX object CMakeFiles/hello_world.dir/hello_world.cpp.o
[100%] Linking CXX executable hello_world
[100%] Built target hello_world

Run the executable:

Finally, run the executable by running the following command:

# ./hello_world 
Hello, World!

This command should output "Hello, World!" to the console.

If you are able to successfully build and run the hello_world project, then CMake has been installed correctly and is working as expected on your Ubuntu system.

 

Best practices for using CMake on Ubuntu

Here are some best practices for using CMake on Ubuntu

  1. Keep your CMakeLists.txt file organized and easy to read.
  2. Use CMake variables to simplify the build process.
  3. Use a separate build directory to keep your source code directory clean.
  4. Use CMake's built-in commands and modules whenever possible.
  5. Use CTest to automate testing.

 

Frequently asked questions and troubleshooting tips for CMake on Ubuntu:

 

Summary

In this article, we discussed how to install CMake on Ubuntu, including the prerequisites required for installation. We also provided a step-by-step guide to verifying the installation and testing CMake by creating a simple CMake project. Additionally, we discussed some best practices for using CMake on Ubuntu, frequently asked questions and troubleshooting tips, and next steps for using CMake.

CMake is a powerful tool for managing software projects, and its installation and usage on Ubuntu can be straightforward with the right knowledge and guidance. By following the steps outlined in this article, Ubuntu users can quickly and easily install CMake, verify its installation, and begin managing their software projects with ease.

Moreover, by adopting the best practices for using CMake, users can improve their productivity and efficiency, while troubleshooting tips can help them address common errors and issues. With this knowledge, users can confidently use CMake on Ubuntu to manage their software projects and contribute to the open-source community.

Feel free to comment on the problems you encounter during the CMake installation.

 

References

cmake.org - Installing CMake
askubuntu.com - How do I install the latest version of cmake from the command line?

 

Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment