1
0
mirror of synced 2024-11-05 12:38:54 +01:00
modern-cmake/chapters/intro/installing.md
Henry Fredrick Schreiner fc91530b24 Updated to 3.15.1
2019-07-28 21:10:18 -04:00

5.9 KiB

Installing CMake

{% hint style='tip' %} Your CMake version should be newer than your compiler. It should be newer than the libraries you are using (especially Boost). New versions work better for everyone. {% endhint %}

If you have a built in copy of CMake, it isn't special or customized for your system. You can easily install a new one instead, either on the system level or the user level. Feel free to instruct your users here if they complain about a CMake requirement being set too high. Especially if they want < 3.1 support. Maybe even if they want CMake < 3.15 support...

Official package

You can download CMake from KitWare. This is how you'll probably get CMake if you are on Windows. It's not a bad way to get it on macOS either, but using brew install cmake is much nicer if you use Homebrew (and you should).

On Linux, there are binaries provided, but you'll need to pick an install location. If you already use ~/.local for user-space packages, the following single line command1 will get CMake for you 2:

{% term %} ~ $ wget -qO- "https://cmake.org/files/v3.15/cmake-3.15.1-Linux-x86_64.tar.gz" | tar --strip-components=1 -xz -C ~/.local {% endterm %}

If you just want a local folder with CMake only:

{% term %} ~ $ mkdir -p cmake-3.15 && wget -qO- "https://cmake.org/files/v3.15/cmake-3.15.1-Linux-x86_64.tar.gz" | tar --strip-components=1 -xz -C cmake-3.15 ~ $ export PATH=pwd/cmake-3.15/bin:$PATH {% endterm %}

You'll obviously want to append to the PATH every time you start a new terminal, or add it to your .bashrc or to an LMod system.

And, if you want a system install, install to /usr/local; this is an excellent choice in a Docker container, for example on GitLab CI. Do not try it on a non-containerized system.

{% term %} docker $ wget -qO- "https://cmake.org/files/v3.15/cmake-3.15.1-Linux-x86_64.tar.gz" | tar --strip-components=1 -xz -C /usr/local {% endterm %}

If you are on a system without wget, replace wget -qO- with curl -s.

You can also build CMake on any system, it's pretty easy, but binaries are faster.

CMake Default Versions

Here are some common build environments and the CMake version you'll find on them. Feel free to install CMake yourself, it's 1-2 lines and there's nothing "special" about the built in version. It's also very backward compatible.

Distribution CMake version Notes
RHEL/CentOS 7 2.8.11 Don't use the default on this system. Grab a new copy or use the EPEL repo.
EPEL for RHEL/CentOS 3.13.4 Called cmake3
Ubuntu 14.04 LTS: Trusty 2.8.12 Don't use the default on this system.
Ubuntu 16.04 LTS: Xenial 3.5.1
Ubuntu 18.04 LTS: Bionic 3.10.2 An LTS with a pretty decent minimum version!
Ubuntu 18.10: Cosmic 3.12.1
Ubuntu 19.04: Disco 3.13.4
Python PyPI 3.13.3 Just pip install cmake on many systems. Add --user for local installs.
Anaconda 3.14.0 For use with Conda
Conda-Forge 3.14.5 For use with Conda
Homebrew on macOS 3.15.1 On macOS with Homebrew, this is only a few minutesa behind cmake.org.
Chocolaty on Windows 3.14.6 Also up to date. The normal cmake.org installers are common on Windows, as well.
TravisCI Trusty 3.9 The December 2017 update added a recent version of clang and CMake! Finally!
TravisCI Xenial 3.12.4 Mid November 2018 this image became ready for widescale use.

Pip

This is also provided as an official package, maintained by the authors of CMake at KitWare. It's a rather new method, and might fail on some systems (Alpine isn't supported last I checked, but that has CMake 3.8), but works really well when it works (like on Travis CI). If you have pip (Python's package installer), you can do:

gitbook $ pip install cmake

And as long as a binary exists for your system, you'll be up-and-running almost immediately. If a binary doesn't exist, it will try to use KitWare's scikit-build package to build, which currently can't be listed as a dependency in the packaging system, and might even require (an older) copy of CMake to build. So only use this system if binaries exist, which is most of the time.

This has the benefit of respecting your current virtual environment, as well.

{% hint style='info' %} Personally, on Linux, I put versions of CMake in folders, like /opt/cmake312 or ~/opt/cmake312, and then add them to LMod. See [envmodule_setup][envmodule_setup] for help setting up an LMod system on macOS or Linux. It takes a bit to learn, but is a great way to manage package and compiler versions. [envmodule_setup]: https://github.com/CLIUtils/envmodule_setup {% endhint %}


  1. I assume this is obvious, but you are downloading and running code, which exposes you to a man in the middle attack. If you are in a critical environment, you should download the file and check the checksum. (And, no, simply doing this in two steps does not make you any safer, only a checksum is safer). ↩︎

  2. If you don't have a .local in your home directory, it's easy to start. Just make the folder, then add export PATH="$HOME/.local/bin:$PATH" to your .bashrc or .bash_profile or .profile file in your home directory. Now you can install any packages you build to -DCMAKE_INSTALL_PREFIX=~/.local instead of /usr/local! ↩︎