In this guide from the Linux Tutorials, we want to teach you to Install and Use CMake on Ubuntu 22.04.
CMake is an open-source, cross-platform, and free tool (or meta-build system), that some would describe as a build system generator that is used in conjunction with your build environment.
It is utilized to control the build process (perform build configuration) by using configuration files or what you would call scripts (CMakeLists.txt) to generate build files. In addition to supporting various build systems and operating systems, it also supports several programming languages such as C++, C#, Cuda, and more.
Install and Use CMake on Ubuntu 22.04
To install CMake, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with Ubuntu 22.04.
Now follow the steps below to complete this guide.
Install CMake 3 on Ubuntu 22.04
First, update your local package index with the following command:
sudo apt update
Next, install the required packages and dependencies on Ubuntu 22.04:
sudo apt install build-essential checkinstall zlib1g-dev libssl-dev -y
To install the latest CMake version, you need to visit the CMake downloads page.
Download CMake LTS on Ubuntu
Then, use the wget command to download the latest stable version of CMake:
Extract your downloaded file:
tar -zxvf cmake-3.23.2.tar.gz
Next, switch to your CMake directory on Ubuntu 22.04:
At this point, use the following commands to install CMake:
The Bootstrap script may take a few minutes.
Build and Install CMake on Ubuntu
Use the make or gmake command to build your process:
# make or # gmake
Now install CMake using the following:
sudo make install
This process can take a few minutes to almost ten minutes.
Verify your CMake installation on Ubuntu 22.04 by checking its version:
cmake version 3.23.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
When your installation is completed, let’s see how to use the CMake.
How To Use CMake
At this point, we want to teach you how to use CMake by creating a sample project hello world on Ubuntu 22.04.
First, you need to create a directory for your project and switch to it with the following command:
# mkdir cmproject # cd cmproject
Then, you need to create a C++ source file with your favorite text editor, here we use vi:
Add the following content to the file:
CMakeLists.txt file (with this exact capitalization) which is required by CMake:
The root directory of the project (
~/cmproject in this case) must contain a
CMakeLists.txt file. Each line in a
CMakeLists.txt file has a command.
CMakeLists.txt file for this project is quite trivial and will only contain these three lines:
project(MyProject VERSION 0.0.1)
When you are done, save and close the file.
It’s recommended to have a separate
build directory for executables. To do this, run the following command:
[olivia@ cmproject]# tree . ├── build ├── CMakeLists.txt └── hello.cpp 1 directory, 2 files
.. is an alias for the parent directory and tells
cmake to find the
CMakeLists.txt file in the parent directory. If you see CXX anywhere while working with CMake, it refers to C++ as CXX.
cmake --build .
At this point, you learn to Install and Use CMake on Ubuntu 22.04.
Hope you enjoy it.
You may be interested in this article How To Install CMake on Ubuntu 20.04.