How to Fix “cmake command not found” Error?

In Linux, the “cmake” command is used to run CMake and generate the necessary build files for a project. CMake is a cross-platform and open-source tool for building, testing, and managing software projects. Users can utilize the “cmake” command to configure a project with various options, set up dependencies, and generate makefiles or other build files.

This guide will offer how to fix the “cmake command not found” error and its solutions.

  • Reason 1: Cmake Package is Not Installed
    • Solution: Install Cmake Package 
  • Reason 2: Environment Variable Not Added
    • Solution: Add Cmake to PATH 

Reason 1: Cmake Package is Not Installed

In Linux, the “cmake: command not found” error typically occurs when the Cmake package is not installed or not properly configured on the system:

Solution: Install Cmake Package 

CMake generates makefiles and workspaces that can be used in the compiler environment. It builds and manages software projects written in different programming languages like C, C++, Java, Fortran, and Python. Follow the steps to fix the issue:

Step 1: Install Cmake

To install Cmake on the Linux system, use the package manager of the distribution. For instance, use the “apt” package manager for Ubuntu in the following command:

$ sudo apt install cmake    #Ubuntu, Debian, and LinuxMint
$ sudo yum install cmake    #CentOS/RHEL
$ sudo dnf install cmake    #Fedora

The output shows that the cmake package has been installed along with the dependencies.

Step 2: Check if Cmake is Installed

To check if Cmake is installed on the Linux system, run the “cmake” command with the “version” option in the terminal:

$ cmake --version

The output shows that the cmake package has the version number 3.22.1. 

Reason 2: Not Add the PATH Environment Variable

If Cmake is already installed on the system, but users are still getting the “cmake: command not found” error, it could be because the Cmake binary is not in the PATH environment variable.

Solution: Add Cmake to PATH Environment Variable

One of the convenient solutions is to add Cmake to the system PATH and reload the configuration file. For this, follow the below-mentioned steps:

Step 1: Determine the Location of the CMake Binary File

To determine the location of the CMake binary file, run the “which” command with the “cmake”:

$ which cmake

The output shows the “/usr/bin/cmake” path to the CMake executable file.

Step 2: Open the .bashrc File  

Open the .bashrc file in the text editor (nano) via the “sudo” privileges as follow:

$ sudo nano ~/.bashrc

It opens the .bashrc file in the nano text editor and adds the following line at the end of the file:

export PATH="/usr/bin:$PATH"

Save the file and exit the editor.

Note: Replace the path to the directory that contains the cmake executable file found in step 1.

Step 3: Reload the .bashrc File

To reload the .bashrc file, utilize the “source” command below:

source ~/.bashrc

Now, users can run the “cmake” command without issues.

Step 4: Verify Installed Cmake Command

To check the installed Cmake command, execute the “cmake” command with the “version” option in the terminal:

$ cmake --version

The output confirms that the cmake package has the version number 3.22.1. 

Conclusion

The “cmake: command not found” error occurs due to the cmake command not being installed in the system. It can be easily fixed by installing the “Cmake” package or adding the Cmake binary to your PATH environment variable. After adding the PATH variable, it is necessary to reload the .bashrc file. Users can verify the installed version of the Cmake package via the “cmake –version” command.

This article has illustrated all possible solutions to resolve the “cmake: command not found” error.