284

I am trying to install latest cmake in my Linux box and I am always getting the below exception:

userName@phx5qa01c-4e23:~/build$ wget http://www.cmake.org/files/v2.8/cmake-2.8.11.tar.gz
--2013-10-08 14:39:55--  http://www.cmake.org/files/v2.8/cmake-2.8.11.tar.gz
Resolving www.cmake.org... 66.194.253.19
Connecting to www.cmake.org|66.194.253.19|:80... failed: Connection timed out.
Retrying.

--2013-10-08 14:40:17-- (try: 2) http://www.cmake.org/files/v2.8/cmake-2.8.11.tar.gz Connecting to www.cmake.org|66.194.253.19|:80... failed: Connection timed out. Retrying.

--2013-10-08 14:40:40-- (try: 3) http://www.cmake.org/files/v2.8/cmake-2.8.11.tar.gz Connecting to www.cmake.org|66.194.253.19|:80...

It is always retrying... Any thoughts?

Does anyone know what wrong I am doing here? Or is there any better way of installing latest version of cmake in my Linux box?

userName@phx5qa01c-4e23:~/build$ uname -a
Linux phx5qa01c-4e23 2.6.35-22-server #33-Ubuntu SMP Sun Sep 19 20:48:58 UTC 2010 x86_64 GNU/Linux
SSH
  • 3,041

13 Answers13

481

The most common situation is when you want to install the latest version of cmake, but your Operating System's repositories are not updated. For example, in my case I have a laptop running Ubuntu 16.04, and when I executed the command sudo apt install cmake the installed version was 3.5.1; instead of 3.28.1 which is the current version at cmake.org.

Teo, how can I get the latest version?

Well, we can install it by following one of these methods:

  • Using APT Repositories
  • Building and Installing from source
  • Using binary files

A. Using APT Repositories (Recommended for normal users)

Kitware now provides an APT Repository that supports Ubuntu 16.04, 18.04, and 20.04. So we can install it easily following these steps:

A-1. Uninstall the default version provided by Ubuntu's package manager and configuration by using:

sudo apt remove --purge --auto-remove cmake

or:

sudo apt purge --auto-remove cmake

A-2. Prepare for installation

sudo apt update && \
sudo apt install -y software-properties-common lsb-release

A-3. Obtain a copy of kitware's signing key.

wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null

A-4. Add kitware's repository to your sources list for Ubuntu Focal Fossa (20.04), Ubuntu Bionic Beaver (18.04) and Ubuntu Xenial Xerus (16.04).

sudo apt-add-repository "deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main"
sudo apt update

A-5. As an optional step, is recommended that we also install the kitware-archive-keyring package to ensure that Kitware's keyring stays up to date as they rotate their keys.

sudo rm /usr/share/keyrings/kitware-archive-keyring.gpg
sudo apt install kitware-archive-keyring

A-6. Finally we can update and install the cmake package.

sudo apt install cmake

B. Building and Installing (Recommended for developers)

For this approach you need to install the GCC tools:

sudo apt update
sudo apt install build-essential libtool autoconf unzip wget

B-1. Uninstall the default version provided by Ubuntu's package manager as in A-1.

B-2. Go to the official CMake webpage, then download and extract the latest version. Update the version and build variables in the following command to get the desired version:

version=3.28
build=1
## don't modify from here
mkdir ~/temp
cd ~/temp
wget https://cmake.org/files/v$version/cmake-$version.$build.tar.gz
tar -xzvf cmake-$version.$build.tar.gz
cd cmake-$version.$build/

B-3. Install the extracted source by running:

./bootstrap
make -j$(nproc)
sudo make install

B-4. Test your new cmake version.

$ cmake --version

Results of cmake --version:

cmake version 3.28.X

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

C. Using binary files (cmake-gui might not work well)

C-1. Uninstall the default version provided by Ubuntu's package manager as in A-1.

C-2. Go to the official CMake webpage, then download and install the latest .sh version in opt/cmake. Update the version and build variables in the following command to get the desired version:

version=3.28
build=1
## don't modify from here
limit=3.20
result=$(echo "$version >= $limit" | bc -l)
os=$([ "$result" == 1 ] && echo "linux" || echo "Linux")
mkdir ~/temp
cd ~/temp
wget https://cmake.org/files/v$version/cmake-$version.$build-$os-x86_64.sh 
sudo mkdir /opt/cmake
sudo sh cmake-$version.$build-$os-x86_64.sh --prefix=/opt/cmake

C-3. Add the installed binary link to /usr/local/bin/cmake by running this:

sudo ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake

C-4. Test your new cmake version as in B-4.

Note

In 3.28.X the X represents the last part of the version that we defined as build. The build may change if cmake is updated. According to the official web page the Latest Release is 3.28.1. If you want the Previous Release 3.27.9 just replace the version and build parameters like this:

version=3.27
build=9
## don't modify from here
limit=3.20
result=$(echo "$version >= $limit" | bc -l)
os=$([ "$result" == 1 ] && echo "linux" || echo "Linux")
mkdir ~/temp
cd ~/temp
wget https://cmake.org/files/v$version/cmake-$version.$build-$os-x86_64.sh 
sudo mkdir /opt/cmake
sudo sh cmake-$version.$build-$os-x86_64.sh --prefix=/opt/cmake

Observation

For previous versions of CMake (3.19.7 <=), remember that the file name contains an upper case L in -Linux-x86_64.sh and from version 3.20 it has a lower case l in -linux-x86_64.sh

muru
  • 207,228
Teocci
  • 5,025
111

Kitware now has an APT repository that currently supports 20.04, 22.04 and 24.04.

All repos support AMD64, ARM32, ARM64 architectures

Install Instructions:

  1. Remove old version of cmake

     sudo apt purge --auto-remove cmake
    
  2. Obtain a copy of the signing key

     wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null
    
  3. Add the repository to your sources list

    a. For Ubuntu Noble Numbat (24.04)

     echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ noble main' | sudo tee /etc/apt/sources.list.d/kitware.list >/dev/null
    

    b. For Ubuntu Jammy Jellyfish (22.04)

     echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ jammy main' | sudo tee /etc/apt/sources.list.d/kitware.list >/dev/null
    

    c. For Ubuntu Focal Fossa (20.04)

     echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ focal-rc main' | sudo tee -a /etc/apt/sources.list.d/kitware.list >/dev/null    
    
  4. Update and install

     sudo apt update
     sudo apt install cmake
    

Link: https://apt.kitware.com/

Himel
  • 1,219
63

I love the following way because you can get a recent version without much trouble.

Kitware seems to officially support a pip wheels release. So you can get latest cmake just by doing:

pip install --upgrade cmake

Here is the blog about it: https://blog.kitware.com/cmake-python-wheels/

Juan Leni
  • 2,218
  • 5
  • 23
  • 32
54

First uninstall any cmake package previously installed. Then:

  1. Go to http://www.cmake.org/download/ and download the latest .sh installer

  2. Install it (for example) in opt/cmake by running

    sudo mkdir /opt/cmake
    sudo sh <installer filename> --prefix=/opt/cmake
    
  3. Add the cmake bin directory to your path: https://askubuntu.com/a/170240/359343

Installed in this way, cmake-gui looks a little bit horrible, if you find a way to fix it please feel free to edit this answer.

Antonio
  • 840
40

Just in case if someone need to install latest CMAKE in a docker image (like me..). In this case is 3.7.2, but you can check here https://cmake.org/download/ as already pointed out

#install latest cmake
ADD https://cmake.org/files/v3.7/cmake-3.7.2-Linux-x86_64.sh /cmake-3.7.2-Linux-x86_64.sh
RUN mkdir /opt/cmake
RUN sh /cmake-3.7.2-Linux-x86_64.sh --prefix=/opt/cmake --skip-license
RUN ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake
RUN cmake --version
24

Probably the server at www.cmake.org was just very busy. You could try again or download the file using your web browser.

There is however a much simpler way:

sudo apt-get install cmake

Update: commenters point out that "Ubuntu 12.04 is only shipping v2.8.7" and "14.04 is still shipping 2.8". That is for a reason: 12.04 and 14.04 are LTS releases of Ubuntu. LTS releases are intended to remain stable for 5 years, so they receive only security and critical updates, not the latest releases of packages. Normal releases come out every 6 months, and are therefore more likely to come with a recent cmake.

zwets
  • 12,770
  • 2
  • 37
  • 46
6

For a Docker container, best run

RUN wget -qO- "https://cmake.org/files/v3.12/cmake-3.12.1-Linux-x86_64.tar.gz" | \
  tar --strip-components=1 -xz -C /usr/local

Adjust the version, if needed.

sebastian
  • 173
5

You can find very recent versions of cmake through snap. For example, as of writing this answer, the latest version of cmake is 3.15.3, and snap has version 3.15.2.

sudo apt-get purge cmake
sudo snap install cmake --classic
qwr
  • 2,969
4

You can also execute the following:

export fn=/tmp/cmake.sh && ls $fn && (echo "use previous $fn? Enter for yes, ctrl+d for no." && read) || (wget -O $fn http://www.cmake.org/files/v3.0/cmake-3.0.2-Linux-i386.sh 1>&2) && (cd /opt && sudo bash ${fn} && echo sudo ln -f -s /opt/cmake*/bin/cmake /usr/local/bin/cmake && cd -)

This script

  • fetches make 3.0.2 ** if it was already downloaded in this session, then you might reuse it if you did not finish the installation
  • then it copies makes link to the bin cmake.
test30
  • 517
3

Remove old version using:

apt-get purge cmake

Download binary version of cmake archived in a tarball. You can use new version of cmake by adding its bin directory path to $PATH. An alternative solution is to extract tar.gz package and go to the directory made after extracting and run following commands:

cp -r bin /usr/
cp -r doc /usr/share/
cp -r man /usr/share/
cp -r share /usr/

The second method is the same as installation process which .deb package does!

SuB
  • 4,419
2

qwr's answer to use snap can be good but if you want something even newer, cmake makes building a DEB from source easy. The following assumes you have a recent cmake already installed. Ubuntu 18.04's apt install cmake will definitely work here.

sudo apt-get build-dep cmake
git clone http://www.cmake.org/cmake.git
cd cmake
git tag
# find the highest tagged release value (hint, rarely at the bottom)
git co tags/<highest tagged release value>
mkdir build
cd build
cmake ..
cmake --build .
cpack -G DEB
# you built a single deb that has what Ubuntu provides as two debs, the binary
# deb and the additional architecture independent data files
sudo apt remove cmake-data
sudo dpkg -i cmake-<highest tagged release value>-Linux-x86_64.deb
mheyman
  • 129
1

Install the latest version of cmake from backports, where many latest versions reside. The particular backports depends on your current OS version. For example, running Debian Stretch, add the following to /etc/apt/sources.list

deb http://ftp.debian.org/debian stretch-backports main

Then install from this backport, aka:

sudo apt-get -t stretch-backports install -y cmake
jeffmcneill
  • 272
  • 4
  • 9
1

It seems a much simpler alternative fails to be mentioned here. Please have a look at this excellent video!

In a nutshell, assuming for example that we want to install cmake 3.28.4 for arm64, the following command cleanly updates cmake (otherwise find which install script, not tar.gz, is required from cmake downloads):

wget https://github.com/Kitware/CMake/releases/download/v3.28.4/cmake-3.28.4-linux-aarch64.sh \
    -O cmake.sh && \
    chmod +x cmake.sh && \
    ./cmake.sh --skip-license --exclude-subdir --prefix=/usr/local && \
    rm cmake.sh

This puts cmake in /usr/local/bin, which precedes /usr/bin on the PATH variable. This is also where it should be installed because packages that are not tracked using apt should be placed here.

Note that you can prepend RUN to this command to make it a valid dockerfile instruction.

LoW
  • 201