2.2. Installing Geant4 on Linux
Table of Contents
Package manager installation
On Linux you can often install Geant4 using your distribution package manager. This is the fastest way to get a working setup, though versions may lag behind the official releases and some options may be missing.
Before installing, make sure you have a C++ compiler, CMake, and basic development tools. On Debian or Ubuntu you typically install:
sudo apt update
sudo apt install build-essential cmakeOn Fedora, RHEL or CentOS you usually need:
sudo dnf groupinstall "Development Tools"
sudo dnf install cmakeOn Debian or Ubuntu, Geant4 packages are available in the standard repositories. A typical minimal installation is:
sudo apt install geant4 geant4-dataSome distributions split data libraries into several packages. You can explore them with:
apt search geant4and install any optional data sets you need.
On Fedora you can search for Geant4 packages with:
dnf search geant4and install a package such as:
sudo dnf install geant4
Package managers usually place Geant4 headers and libraries in system locations and may provide CMake configuration files, for example under /usr/lib/x86_64-linux-gnu/cmake/Geant4 or a similar path depending on the distribution. You can check that CMake can find Geant4 with a small test CMakeLists.txt that calls find_package(Geant4 REQUIRED) and then runs CMake in an empty build directory. If CMake configures successfully, your packaged Geant4 installation is ready.
Some package builds enable geant4-config, a helper script that prints compiler and linker flags. You can test its presence with:
geant4-config --version
If it prints a version number, Geant4 is correctly installed and on your PATH. For this course, using your distribution package is fine if the version is reasonably recent and if it includes the data libraries required by the examples you want to run.
Building from source
Building Geant4 from source gives you full control over the version, build options, and installation location. It is the recommended approach when you want a specific release, need features such as multithreading or advanced visualization, or if your distribution does not provide suitable packages.
First install the build tools and dependencies. For example, on Debian or Ubuntu:
sudo apt update
sudo apt install build-essential cmake git \
qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools \
libx11-dev libxmu-dev libxi-dev libgl1-mesa-dev libglew-dev \
libexpat1-devThis set is more than minimal, but it prepares you for building Geant4 with Qt and OpenGL visualization. On Fedora, RHEL or CentOS similar packages exist with slightly different names.
Download the Geant4 source from the official website or the Git repository. A typical workflow for a specific release uses a tarball:
mkdir -p $HOME/geant4/src
cd $HOME/geant4/src
wget https://geant4-data.web.cern.ch/releases/geant4.11.2.0.tar.gz
tar -xf geant4.11.2.0.tar.gzAdapt the file name and directory to the actual version you want to use.
Create a separate build directory. Geant4 uses out of source builds, which keeps build files separate from the source tree. For example:
mkdir -p $HOME/geant4/build
cd $HOME/geant4/buildRun CMake to configure the build. The key options for a basic beginner installation are the installation prefix, the build type, and the data library handling. A typical command line looks like this:
cmake -DCMAKE_INSTALL_PREFIX=$HOME/geant4/install \
-DCMAKE_BUILD_TYPE=Release \
-DGEANT4_INSTALL_DATA=ON \
-DGEANT4_BUILD_MULTITHREADED=ON \
-DGEANT4_USE_QT=ON \
-DGEANT4_USE_OPENGL_X11=ON \
../src/geant4.11.2.0
Geant4 then configures the build. The option GEANT4_INSTALL_DATA=ON instructs CMake to download and install the official data libraries along with the code. This is very convenient for beginners, because you do not have to manage those files manually.
Always use an out of source build directory, for example build, and specify an installation prefix that you control, for example $HOME/geant4/install. Avoid installing into /usr or other system directories when you are learning Geant4.
If configuration completes without errors, compile Geant4. On a multicore machine you can build in parallel:
cmake --build . -- -j4Replace 4 with the number of cores you want to use. This may take several minutes.
After a successful build, install Geant4 into the chosen prefix:
cmake --install .
This step copies headers, libraries, data libraries, CMake configuration files, and optional tools into $HOME/geant4/install or the prefix that you selected.
To make Geant4 easy to use, you must set up the environment for that installation. Geant4 provides a shell script for this purpose. If you installed into $HOME/geant4/install, you can enable the environment with:
source $HOME/geant4/install/bin/geant4.sh
This script sets environment variables such as G4DATA and updates PATH and LD_LIBRARY_PATH to point to the installed Geant4. You should add this source line to your shell startup file, for example .bashrc, so that it is executed every time you open a new terminal.
You can now verify the build by configuring and running one of the installed example applications. The examples are usually found under $HOME/geant4/install/share/Geant4-<version>/examples. Each example contains its own CMakeLists.txt. For a simple check, choose a basic example, create a build directory inside it, configure with CMake, and compile. If compilation succeeds and the example runs, your source build is complete and ready for use in the rest of this course.
Views: 14
KAHIBARO