CCMake is a widely used, open-source tool that simplifies software build processes by generating build files for platforms like Make, Ninja, and Visual Studio. It’s an essential tool for cross-platform development, whether you’re building desktop, mobile, or embedded applications.
This guide will show you how to install CMake on Ubuntu 24.04, 22.04, or 20.04 using two methods. You can opt for the Ubuntu default repository for a quick and stable setup or compile CMake from source to access the latest features. By the end, you’ll have CMake installed and ready to streamline your development workflow.
Update and Upgrade Your System
Before installing CMake, ensure your Ubuntu system is up to date. This minimizes potential errors during installation by providing the latest software and security updates.
Step 1: Command to Update and Upgrade
Run the following command in your terminal:
sudo apt update && sudo apt upgrade
sudo apt update
: Refreshes the list of available packages and their versions.sudo apt upgrade -y
: Upgrades all installed packages to the latest versions automatically, without prompting for confirmation (-y
).
Step 2: Check for Reboot Requirements
After updating, some updates (like kernel upgrades) may require a system reboot to take effect. If you see a prompt for a reboot or want to ensure your system is cleanly updated, use the following command:
sudo reboot
Installing CMake on Ubuntu
To install CMake on Ubuntu, you can choose between two methods based on your needs. The first method uses the Ubuntu default repository for a quick and stable installation, while the second method involves compiling CMake from source, offering the latest features at the cost of some complexity.
Method 1: Install CMake via Ubuntu Default Repository
Step 1: Install CMake from the Repository
To begin, open your terminal and run the following command to install CMake:
sudo apt install cmake
Step 2: Verify the Installation
After installation is complete, verify that CMake is installed correctly by checking its version:
cmake --version
This method is straightforward and reliable, making it suitable for most users. It installs the version available in Ubuntu’s repository, which may not always be the latest.
Method 2: Install CMake by Compiling Source
For advanced users or those requiring the latest features, compiling CMake from source is an excellent option. This method involves downloading, building, and installing the source code manually.
Step 1: Install Build Dependencies
Before starting, install the necessary tools and libraries for the build process. Run the following command:
sudo apt install build-essential checkinstall zlib1g-dev libssl-dev -y
Step 2: Download the Latest Source Archive
Visit the CMake GitHub Releases page and locate the latest .tar.gz
source archive. Replace <version>
with the desired version in the following command:
wget https://github.com/Kitware/CMake/releases/download/<version>/cmake-<version>.tar.gz
Step 3: Extract the Source Archive
After downloading, extract the contents using:
tar -zxvf cmake-{version number}.tar.gz
Navigate to the extracted directory:
cd cmake-{version number}
Step 4: Build and Install CMake
Start the build process by running the bootstrap
script. This step configures the environment and checks dependencies:
./bootstrap
Once complete, compile the source code using:
make
Finally, install the compiled CMake package:
sudo make install
This step might take a few minutes.
Step 5: Verify the Installation
To confirm the installation, check the CMake version:
cmake --version
Testing Your CMake Installation
Testing your CMake installation ensures it is correctly configured and functioning as expected. This section demonstrates how to create and build a simple “Hello, World!” program, verifying that CMake is working properly on your Ubuntu system.
Step 1: Create a Test Directory
To begin, create a dedicated directory for your test project. This keeps your files organized and ensures the test is isolated from other projects. Open your terminal and execute the following command:
mkdir test-hello && cd test-hello
The mkdir
command creates a new directory called test-hello
, and cd
navigates into it.
Step 2: Set Up a CMakeLists.txt File
The CMakeLists.txt
file is the heart of a CMake project, specifying the build configuration. Use the nano
editor to create this file in the test directory:
sudo nano CMakeLists.txt
Add the following configuration to define the project and its executable:
cmake_minimum_required(VERSION 3.16)
project(HelloWorld)
add_executable(hello main.cpp)
cmake_minimum_required
: Ensures the CMake version meets the minimum required for the project.project
: Names the project, in this case,HelloWorld
.add_executable
: Specifies the executable target and its source file.
Save and exit the editor by pressing CTRL+X
, then Y
, and finally Enter
.
Step 3: Create a C++ Source File
The next step is to write a basic C++ program to print “Hello, World!” to the terminal. Create a file named main.cpp
in the same directory:
sudo nano main.cpp
Enter the following code into the file:
#include <iostream>
int main()
{
std::cout << "Hello, World!" << std::endl;
return 0;
}
This code uses the C++ standard library to output “Hello, World!” to the terminal. Save and close the editor using the same steps as above.
Step 4: Generate a Build System with CMake
With the configuration and source files in place, generate a build system using CMake. In the terminal, run:
cmake .
CMake processes the CMakeLists.txt
file and generates a Makefile
in the current directory. The output should confirm successful generation of the build files.
Step 5: Build the Program
To compile the program, use the make
command, which reads the generated Makefile
and builds the hello
executable:
make
The terminal output will display the compilation process, confirming the creation of the hello
executable.
Step 6: Run the Program
Finally, run the compiled program to verify everything is working correctly:
./hello
You should see the following output in the terminal:
This confirms that CMake is installed and functioning as expected on your system.
Conclusion
Installing CMake on Ubuntu is an essential step for developers aiming to streamline their software build processes. Whether you choose the convenience of the Ubuntu default repository or the flexibility of compiling from source, both methods provide the tools needed for effective cross-platform development. The repository method is quick and reliable for most users, while compiling ensures access to the latest features and customization options.
Keeping CMake updated is vital to leverage new features, security patches, and performance improvements. With its robust capabilities, CMake empowers developers to efficiently manage builds for desktop, mobile, and embedded systems, making it a cornerstone of modern software development.
Frequently Asked Questions (FAQs)
A: The best method for beginners is to use the Ubuntu default repository. It’s quick, easy, and requires minimal setup. You can install it by running sudo apt install cmake
in the terminal.
A: To get the latest version, you’ll need to compile CMake from source. This involves downloading the latest release from the official CMake website or GitHub, extracting the files, and manually building it. It’s more advanced but gives you access to new features.
A: Yes, updating your system is important to avoid package conflicts and ensure compatibility. Run sudo apt update && sudo apt upgrade
before installing CMake to keep your system up to date.
A: If the installation fails, check for dependency issues or network problems. For dependency errors, try running sudo apt --fix-broken install
. If you’re compiling from source, verify that all required packages are installed.
A: You can test CMake by creating a simple project with a CMakeLists.txt
file and a basic C++ program. Use CMake to build and run the program. If it works, your installation is successful.
A: No, CMake is specifically designed for projects that require cross-platform builds or custom build configurations. If you’re working on smaller or single-platform projects, simpler build tools might suffice.
Useful CMake Links
Here are some helpful resources to further enhance your understanding and usage of CMake. These links are directly related to the content covered in this guide and provide additional support for both beginners and advanced users:
- CMake Getting Started Guide
Explore the official CMake getting started guide to familiarize yourself with the basics of using CMake for your projects. - CMake Documentation
Access the comprehensive documentation for CMake, covering everything from basic commands to advanced usage and customization. - CMake Support Page
If you need help or have questions about CMake, visit the official support page for FAQs, forums, and additional resources. - CMake GitHub Repository
Stay updated with the latest CMake releases, source code, and contributions by exploring its GitHub repository.
Share Your Thoughts
Have questions or feedback about this guide? Feel free to leave a comment below. Whether you’re new to CMake or a seasoned developer, we’d love to hear about your experience or help with any issues you encounter. Your input can also help others in the community, so don’t hesitate to share!
Thank you for taking the time to reply to my previous comment. I just wanted to clarify that the files in this link: https://github.com/Kitware/CMake/releases, do not include the boostrap script and other files you mentioned. The instructions do work but I had to download the file from this link: https://cmake.org/download.
I just thought that might be useful for others.
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.