最常见的情况是您想要安装最新版本的cmake,但您的操作系统的存储库没有更新。例如,在我的情况下,我有一台运行Ubuntu 16.04的笔记本电脑,当我执行命令时​​sudo apt install cmake​​,安装的版本是3.5.1;而不是cmake.org上的当前版本​​3.23.2​​。

Teo,我怎样才能获得最新版本?

好吧,我们可以按照以下方法之一安装它:

  • 使用 APT 存储库
  • 从源代码构建和安装
  • 使用二进制文件

A. 使用 APT 存储库(推荐给普通用户)

Kitware 现在提供了一个支持 Ubuntu 16.04、18.04 和 20.04的​​APT 存储库。​​因此,我们可以按照以下步骤轻松安装它:

A-1。使用以下命令卸载 Ubuntu 包管理器和配置提供的默认版本:

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>sudo apt remove --purge --auto-remove cmake
</code></span></span>

或者:

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>sudo apt purge --auto-remove cmake
</code></span></span>

A2。准备安装

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>sudo apt update && \
sudo apt install -y software-properties-common lsb-release && \
sudo apt clean all
</code></span></span>

A-3。获取工具包签名密钥的副本。

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>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
</code></span></span>

A-4。将套件的存储库添加到 Ubuntu Focal Fossa (20.04)、Ubuntu Bionic Beaver (18.04) 和 Ubuntu Xenial Xerus (16.04) 的源列表中。

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>sudo apt-add-repository "deb https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main"
</code></span></span>

A-5。作为可选步骤,建议我们也安装该​​kitware-archive-keyring​​软件包,以确保 Kitware 的密钥环在轮换密钥时保持最新。

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>sudo apt update
sudo apt install kitware-archive-keyring
sudo rm /etc/apt/trusted.gpg.d/kitware.gpg
</code></span></span>

A-5.注意如果运行​​sudo apt update​​出现以下错误:

Err:7 https://apt.kitware.com/ubuntu bionic InRelease
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 6AF7F09730B3F0A4
Fetched 11.0 kB in 1s (7552 B/s)

复制公钥​​6AF7F09730B3F0A4​​并运行以下命令:

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6AF7F09730B3F0A4
</code></span></span>

A-6。最后我们可以更新和安装​​cmake​​包。

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>sudo apt update
sudo apt install cmake
</code></span></span>

B. 构建和安装(推荐给开发者)

对于这种方法,您需要安装 GCC 工具:

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>sudo apt update
sudo apt install build-essential libtool autoconf unzip wget
</code></span></span>

B-1。卸载 Ubuntu 包管理器提供的默认版本,如A-1 所示

B-2。转到​​官方 CMake 网页​​​,然后下载并解压最新版本。更新以下命令中的​​version​​​和​​build​​变量以获得所需的版本:

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>version=3.23
build=2
## 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/
</code></span></span>

B-3。通过运行安装提取的源:

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>./bootstrap
make -j$(nproc)
sudo make install
</code></span></span>

B-4。测试你的新​​cmake​​版本。

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>$ cmake --version
</code></span></span>

结果​​cmake --version​​:

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>cmake version 3.23.X

CMake suite maintained and supported by Kitware (kitware.com/cmake).
</code></span></span>

C. 使用二进制文件(​​cmake-gui​​可能效果不佳)

C-1。卸载 Ubuntu 包管理器提供的默认版本,如A-1 所示

C-2。转到​​官方 CMake 网页​​,然后下载并安装最新.sh版本。​​opt/cmake​​​更新以下命令中的​​version​​​和​​build​​变量以获得所需的版本:

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>version=3.23
build=2
## 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
</code></span></span>

C-3。​/usr/local/bin/cmake​​通过运行以下命令添加已安装的二进制链接:

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>sudo ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake
</code></span></span>

C-4。像在B-4中一样测试您的新​​cmake​​版本。

笔记

3.23.X中,X代表我们定义为 build 的版本的最后一部分。如果更新cmake,构建可能会发生变化。根据官方网页,​​最新版本是 3.23.2​​​。如果您想要​​以前的版本 3.21.4​​,只需替换版本并构建参数,如下所示:

<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code>version=3.22
build=5
## 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
</code></span></span>

观察

对于以前版本的 CMake (3.19.7 <=),请记住文件名包含大写字母​​L​​​,从 3.20 版本开始,文件名包含​​-Linux-x86_64.sh​​​小写字母​​l​​​​-linux-x86_64.sh​


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.23.2 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-5.Note If running ​​sudo apt update​​ gets the following error:

Err:7 https://apt.kitware.com/ubuntu bionic InRelease
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 6AF7F09730B3F0A4
Fetched 11.0 kB in 1s (7552 B/s)

Copy the public key ​​6AF7F09730B3F0A4​​ and run this command:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6AF7F09730B3F0A4

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.23
build=2
## 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.23.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.23
build=2
## 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.23.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.23.2​​​. If you want the ​​Previous Release 3.21.4​​ just replace the version and build parameters like this:

version=3.22
build=5
## 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​