How do I install the latest version of cmake from the command line?

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

Solution 1:

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.22.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 && \
sudo apt clean all

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 /etc/apt/trusted.gpg.d/kitware.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 https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main"

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 apt update
sudo apt install kitware-archive-keyring
sudo rm /etc/apt/trusted.gpg.d/kitware.gpg

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

sudo apt update
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.22
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.22.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.22
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.22.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.22.1. If you want the Previous Release 3.21.4 just replace the version and build parameters like this:

version=3.21
build=4
## 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

Solution 2:

Kitware now has an APT repository that currently supports Ubuntu 16.04, 18.04 and 20.04

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 /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null
    
  3. Add the repository to your sources list

    a. For Ubuntu Focal Fossa (20.04)

    sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ focal main'     
    

    b. For Ubuntu Bionic Beaver (18.04)

    sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic main'
    

    c. For Ubuntu Xenial Xerus (16.04)

    sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ xenial main'
    
  4. Update and install

    sudo apt update
    sudo apt install cmake