262

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
muru
  • 197,895
  • 55
  • 485
  • 740
SSH
  • 2,821
  • Are you sure there isn't a firewall blocking your connection? Why using wget? Try to download that file (http://www.cmake.org/files/v2.8/cmake-2.8.11.tar.gz) using a web browser. – Eric Carvalho Oct 08 '13 at 22:54
  • Similar question here: http://askubuntu.com/questions/610291/how-to-install-cmake-3-2-on-ubuntu-14-04 with a good answer. – Elliptical view Oct 24 '16 at 22:19
  • I think your question is "How to install latest cmake version in Linux" but the command sudo apt-get install cmake does not install the latest version. – Teocci Aug 17 '17 at 01:54
  • https://askubuntu.com/a/952929/579410 – KindDragon Jul 19 '18 at 13:55
  • The answers below didn't help me, but this answer to "Installing latest cmake on Ubuntu 18.04.3 LTS run via WSL: OpenSSL error" did: https://askubuntu.com/a/1205458/1191005

    I also had to use this answer to "Unable to install libssl1.0.0:i386 due to unmet dependencies": https://askubuntu.com/a/1253788/1191005

    – Tatiana Racheva Mar 10 '21 at 20:10
  • follow this https://apt.kitware.com/ – Eyad Ahmed Feb 15 '24 at 11:22

12 Answers12

447

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 && \
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.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

Teocci
  • 4,665
  • 23
    The make install command need root privileges. The cmake --version command only works after open a new terminal because cmake is installed under /usr/local/bin/ by default, not /usr/bin/. – HD189733b Jan 22 '17 at 19:10
  • When I ran this, I got an error CMake Error: Could not find CMAKE_ROOT !!! and it failed to update cmake. – Timothy Swan Dec 25 '17 at 21:47
  • 2
    This is not a solution to UPDATE but to INSTALL cmake. Try this command or google it to find a solution: sudo apt-get remove cmake cmake-data – Teocci Dec 26 '17 at 02:18
  • @Teocci I like your tutorial. Just a hint: For also getting ccmake (the CMake curses interface) built and installed you need to do sudo apt-get install libncurses-dev first (see here, here and here). – Florian Feb 08 '18 at 20:52
  • 1
    Note to also build the cmake-gui tool, add --qt-gui to the bootstrap command. – Richard Whitehead Mar 12 '18 at 16:53
  • 1
    apt purge includes apt remove, so it's pointless to do sudo apt remove cmake; apt purge cmake

    Just do apt purge

    – gerardw Dec 16 '18 at 16:10
  • Instruction set A produces: bash: /usr/bin/cmake: No such file or directory. B works fine. – Sandu Ursu May 03 '19 at 19:16
  • You might want to symlink all the cmake tools in /bin after the install, not just cmake itself. You can use sudo cp -s /opt/cmake/bin/* /usr/local/bin or sudo cp -s /opt/cmake/cmake-$VERSION-Linux-x86_64/bin/* /usr/local/bin to do that, depending on it you chose to install cmake in a versioned directory or not. – BeeOnRope Jun 12 '19 at 20:00
  • Pretty solid guide, and the one that actually work for me... The rest I found do not cover the process from start to end. – BusyProgrammer Sep 07 '19 at 11:48
  • I tried B but cmake --version gives me Command 'cmake' not found – Anakin Dec 04 '19 at 12:42
  • @Anakin If you get bash: /usr/bin/cmake: No such file or directory, see HD189733b's upvoted comment above. – leekaiinthesky Jan 07 '20 at 23:34
  • @Teocci Perhaps add a . ~/.profile or . ~/.bashrc step to A-4 to fix this issue. Thank you for the answer!! – leekaiinthesky Jan 07 '20 at 23:35
  • Why not use Kitware's APT repo for CMake which supports 16.04? – Alex Reinking Feb 28 '20 at 04:19
  • Kitware now release their source files and distributes them using Github, the links on their website have been updated to the format https://github.com/Kitware/CMake/releases/download/v$version/cmake-$verison.$build-Linux-x86_64.sh But wget might not manage to download the file, so you will need to manually download it and run the rest of the commands as usual – Mor Paz Apr 25 '20 at 17:54
  • 2
    My poor laptop just froze while executing make and I had to do a hard shutdown. Maybe it's better not to use all the cores? -j$(($(nproc)-2))? – Dan Oak Dec 08 '20 at 13:19
  • For solution A, you may need to follow https://askubuntu.com/a/1390307/312805 – david Apr 12 '22 at 16:20
  • A instructions working perfect Ubuntu 22.04, cmake version 3.23.2 – bim Jun 04 '22 at 08:53
  • @Teocci: Great. I have successfully installed using step A i.e. "Using APT Repositories (Recommended for normal users)". However, can you please tell me how to reverse this step? I mean I want to remove the cmake installed through APT repository. – BZKN Aug 23 '22 at 19:37
  • 1
    @BZKN just run sudo apt remove --purge --auto-remove cmake – Teocci Aug 24 '22 at 04:45
  • RE: Copyright and Content Licensing, read https://askubuntu.com/help/licensing - any publicly posted content here is auto-licensed under CC BY-SA of a specific version at the time of the post being made. This is plastered everywhere in the SE network, with the Terms of Service and the help centers. Everything is therefore under a CC BY-SA license. This permits remixing, modification, etc. for any purpose - therefore any contributed edit is (I believe) legal under the TOS and if you want your stuff under a different license, don't post here on Ask Ubuntu / Stack Exchange. – Thomas Ward Jan 11 '23 at 16:36
  • 1
    @ThomasWard to whom u are talking to? to me? I didn't talk about copyright. First this post is under the CC BY-SA 3.0 license so u can Share and Adapt. What this means? simple you can share the link, use it in your company, adapt it by copying the content and put it in ur personal page, blog, copy part of it in ur own answer. But this is the original post. I will give u an example, everybody knows the Mona Lisa imagine it is under this license, can u go and paint over, put on her red lips or something like that? Of course not u can make a copy and change it – Teocci Jan 12 '23 at 00:28
  • I had to sudo ln -s /usr/local/bin/cmake /usr/bin/cmake to make it work – mrtpk Nov 09 '23 at 09:13
  • Can confirm this steps still works in 2024. Able to update WSL cmake to version 3.28.3. – Syaiful Nizam Yahya Mar 19 '24 at 08:57
96

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

All repos support AMD64, ARM32, ARM64 architectures and 18.04 also supports x86

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 Jammy Jellyfish (22.04)

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

    b. For Ubuntu Focal Fossa (20.04)

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

    c. For Ubuntu Bionic Beaver (18.04)

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

     sudo apt update
     sudo apt install cmake
    
Himel
  • 1,069
  • 9
    Best solution as of today but the repository does not support the arm64 architecture. – Victor Lamoine Nov 12 '19 at 18:01
  • 1
    Related links: https://cmake.org/download/ https://apt.kitware.com/ – wisbucky Feb 20 '20 at 19:28
  • 1
    unsecure repo error

    W: GPG error: https://apt.kitware.com/ubuntu bionic InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 291F9FF6FD385783 E: The repository 'https://apt.kitware.com/ubuntu bionic InRelease' is not signed. N: Updating from such a repository can't be done securely, and is therefore disabled by default. N: See apt-secure(8) manpage for repository creation and user configuration details.

    Better use the PIP approach below

    – Bizmate Sep 03 '20 at 08:38
  • is the first step necessary? It should upgrade anyway right? – Abhineet Pandey Feb 25 '21 at 11:59
  • @Bizmate this should help – JoeVictor Apr 12 '22 at 22:15
  • For me it worked only with python3.6, but not with python3.11 and python.3.9. – user2340939 Apr 20 '23 at 11:58
60

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,178
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
  • 13
    Add a link from /usr/local/bin/cmake to the installed binary. This way there is no need to change the PATH. sudo ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake – Christian Mar 16 '16 at 14:30
  • 3
    This is the best answer. – SuB Nov 20 '16 at 09:59
  • 3
    really, this is most useful answer for initial question – amigo421 Dec 01 '16 at 19:37
  • This gives me broken cmake-gui, which doesn't work over xRDP (prev version worked), and xRDP is the only way to access the server. – stiv Mar 28 '18 at 18:56
  • @stiv 1. Did you verify cmake after the installation is working, and that you have no other cmake version in your path? 2. Can you verify if you are using the cmake-gui provided in the installation, and not some older version? (To know the version, append --version to the command line) – Antonio Mar 29 '18 at 16:22
  • I would definitely suggest this answer to anyone coming around looking for a prebuilt version of x86-64 CMake – dddJewelsbbb Jul 22 '19 at 19:59
  • for i in /opt/cmake/cmake-3.19.1-Linux-x86_64/bin/*; do sudo ln -s "$i" /usr/local/bin/"$i"; done makes all the symbolic links, and may fix the noted-above cmake-gui issue. – studog Dec 01 '20 at 14:51
  • This also works on wsl btw – Anirudh Jan 15 '21 at 17:50
  • A release and a feature worth circumventing apt are 2 different things. I just downloaded cmake yesterday via apt, and it has all the usual features. If you are worried about using an up-to-date make, you should be using autotools on Debian anyway. I spend a significant amount of my time on here telling people NOT to do exactly what your answer suggests. When you install like this, dpkg doesn't even know this package exists. If you are going to do it the axe backward way, at least port to a .deb package, or better yet get a real one that was configured by a maintainer, and load it... – Nate T Sep 13 '21 at 20:41
  • ...into dpkg manually with dpkg -i. The first time you go to download a pkg that requires cmake as a dependency, what will you do then? Your cmake won't fill that dependency. This is only one of many such issues that can be avoided by making sure that your system's software is properly managed. Ubuntu is a well oiled machine. It will run forever so long as you keep the oil filled. Apologies if this sounds harsh, but this is not something you should do unless you absolutely need a feature that is not available via apt / snap. – Nate T Sep 13 '21 at 20:42
  • @NateT The question is about "how do you get the latest version of cmake" (i.e. with all the latest features), and this is the answer. If you have a better answer please draft it and publish it. – Antonio Sep 14 '21 at 07:23
  • Yea, that's why I didn't DV any answers (the UV was me.) In that context, just because he is in a VM and redos are easy, its not a big deal. The reason for the comments is because most of the users who will need this resource in the future (those new to linux environments) wont know the difference. Put yourself in a new user's shoes and imagine the impression you would get if you landed here. Gonna delete a few comments. Starting to look like graffiti : ) – Nate T Sep 14 '21 at 09:59
  • This is the only answer that worked for me! For step 2, type y for the license and N for adding the additional path folder. – Caleb Stanford Sep 30 '22 at 22:57
  • That should be the accepted answer. A suggestion might be that you could edit PATH environment variable by appending export PATH="${PATH}:/opt/cmake/bin" to ~/.bashrc. – Caglayan DOKME Feb 25 '24 at 14:59
39

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
23

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,354
  • 3
    ubuntu 12.04 is only shipping v 2.8.7 of cmake with apt-get. The current release of version 2 is 2.8.12. I suggest you persevere with building it from source. – Richard Hodges Jun 16 '14 at 17:23
  • 92
    This is not the correct answer. The latest version is almost never on apt. – Senjai Feb 20 '15 at 06:09
  • 3
    14.04 still shipping 2.8 – Stolas Mar 17 '16 at 09:22
  • @Stolas and other commenters: you are referring to LTS Releases of Ubuntu (12.04, 14.04). In an LTS release you only get security and critical fixes, and certainly not an upgrade of a package to its latest version. The whole point of LTS releases is that they don't do this, in order to guarantee stability for 5 years. I will update my answer to address this point. – zwets Mar 23 '16 at 07:06
  • 6
    This answer is wrong. The StableReleaseUpdates policy is not just for LTS releases. All Ubuntu releases, once actually released, only get fixes for security vulnerabilities or otherwise serious bugs. As shown here (and on Launchpad), even non-LTS releases haven't received new cmake versions. Separate backports for some packages do exist (but not cmake). – Eliah Kagan Aug 04 '17 at 11:56
  • @EliahKagan, how is the answer wrong? It does not claim that non-LTS releases receive package updates. (In fact, why would they when the next release is always less than 6 months away?) What I point out is that if you stick to an LTS release, you won't get cmake updates. When Stolas wrote "14.04 still shipping 2.8", Wily had been out for 5 months, carrying cmake 3.2. One month later, xenial came out with cmake 3.5. Zesty now has 3.7. Yet if Stolas is still on 14.04 LTS (which could be for another two years) they'll always get 2.8. Edited my answer to perhaps make this clearer. – zwets Aug 23 '17 at 12:58
  • 16
    This answer is wrong because sudo apt-get install cmake does not, by itself, ever upgrade cmake to a newer version. This does not work on any Ubuntu release. If your answer is actually "upgrade your Ubuntu system to a newer release" then you may want to edit again to make that clearer. (That would not be a reasonable solution for many users, though, and often there is no released version of Ubuntu whose repositories have the latest version of cmake.) – Eliah Kagan Aug 23 '17 at 13:55
  • +1 @EliahKagan required != stable* and stable != newer and *newer != required – dsgdfg Apr 27 '21 at 14:24
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,802
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,229
  • 5
  • 24
  • 33
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
  • Now run the command man cmake and you will see one more reason that apt outshines the install from source method 99.999 % of the time. – Nate T Sep 12 '21 at 09:55
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
  • 255
  • 2
  • 8