How to install the latest cmake version on Ubuntu ?

CMake is a widely used build system that helps automate the compilation process for software projects. Keeping CMake up-to-date ensures compatibility with the latest software and features. This guide will show you how to install the latest version of CMake on Ubuntu.

If you are trying to compile some opensource software which using cmake for the compilation, some of the .cmake file used by that opensource code might contains the requirement check for version as,

cmake_minimum_required(VERSION 3.11)

Due to this version check, if your default Ubuntu installed cmake version is less than 3.11, [ As in our case, on Ubuntu 18.04, default cmake version is 3.10.2 ], you will see an error like as below,

CMake Error at CMakeLists.txt:25 (cmake_minimum_required):
  CMake 3.11 or higher is required.  You are running version 3.10.2

To solve this dependency check, we will need to upgrade the cmake version to latest, which can be done on Ubuntu 18.04 as below,

Remove current cmake as,

$ sudo apt remove cmake

Make sure you have “pip” installed, if not, install as mentioned in “How to Install Pip on Ubuntu”

1) Upgrade CMake using PIP

Now, install cmake as,

$ sudo pip install cmake --upgrade

Check the version of pip as,

$ which cmake
/usr/local/bin/cmake
$ cmake --version
cmake version 3.13.3

2) Upgrade Latest CMake Version using Source Code

1. Install Prerequisites

Before installing CMake, ensure that you have the necessary prerequisites. Open a terminal and update the package list:

sudo apt update

2. Remove Old CMake Version (if necessary)

If you have an older version of CMake installed, you might want to remove it first to avoid conflicts:

sudo apt remove --purge cmake

3. Download the Latest CMake Version

Visit the official CMake website to find the latest version. As of this writing, the latest version can be downloaded using the following command (replace 3.27.0 with the latest version number):

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

4. Extract the Downloaded Archive

Extract the downloaded tar.gz file:

tar -zxvf cmake-3.27.0.tar.gz

5. Compile and Install CMake

Navigate to the extracted directory and compile the source code:

cd cmake-3.27.0
./bootstrap
make
sudo make install

6. Verify the Installation

After the installation is complete, verify the CMake version to ensure it was installed correctly:

cmake --version

You should see the latest version of CMake displayed.

3) Installing CMake Using APT Repository

If you prefer a simpler method, you can add the Kitware APT repository to install the latest CMake version.

1. Add the Kitware APT Repository

sudo apt update
sudo apt install -y software-properties-common apt-transport-https ca-certificates gnupg lsb-release
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://apt.kitware.com/keys/kitware-archive-latest.asc | sudo tee /etc/apt/keyrings/kitware-archive-keyring.asc >/dev/null
echo 'deb [signed-by=/etc/apt/keyrings/kitware-archive-keyring.asc] https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main' | sudo tee /etc/apt/sources.list.d/kitware.list >/dev/null
sudo apt update

2. Install CMake

sudo apt install cmake

3. Verify the Installation

Check the installed CMake version:

cmake --version

You should now have the latest version of CMake installed on your Ubuntu system.

Leave a Comment