Hi @Alamgir_Khan
Welcome to the Geant4 forum!
Thank you for your question. Please find below instructions to build a singularity container using Ubuntu 22 as bootstrap, it should work similarly for you. You may also want to try using the singularity image itself.
Singularity
This shows you how to build a Singularity container with Geant4, including GDML support and Qt5 visualization.
- Create a file named, for example,
G4_gdml_vis.def, and copy the following code into it:
Bootstrap: docker
From: ubuntu:22.04
%labels
Version Geant4-GDML-Qt5
%environment
export G4INSTALL=/opt/geant4
export G4DATA_DIR=$G4INSTALL/share/Geant4/data
export G4LIB_DIR=$G4INSTALL/lib
export PATH=$G4INSTALL/bin:$PATH
export LD_LIBRARY_PATH=$G4INSTALL/lib:$LD_LIBRARY_PATH
%post
apt-get update && apt-get install -y \\
build-essential \\
cmake \\
wget \\
libxerces-c-dev \\
libexpat1-dev \\
qtbase5-dev \\
mesa-utils libglu1-mesa-dev \\
freeglut3-dev mesa-common-dev
# Install Geant4
cd /opt
G4_VERSION=11.2.2
wget https://github.com/Geant4/geant4/archive/refs/tags/v${G4_VERSION}.tar.gz
tar -xf v${G4_VERSION}.tar.gz
cmake -S geant4-${G4_VERSION} -B geant4-build \\
-DCMAKE_INSTALL_PREFIX=/opt/geant4 \\
-DGEANT4_BUILD_MULTITHREADED=ON \\
-DGEANT4_USE_GDML=ON \\
-DGEANT4_USE_SYSTEM_EXPAT=ON \\
-DGEANT4_INSTALL_DATA=ON \\
-DGEANT4_INSTALL_EXAMPLES=OFF \\
-DGEANT4_USE_QT=ON
cmake --build geant4-build -- -j$(nproc) install
# Clean up
apt-get clean && rm -rf /var/lib/apt/lists/* /opt/v${G4_VERSION}.tar.gz /opt/geant4-${G4_VERSION} /opt/geant4-build
%runscript
echo "Geant4 with GDML and Qt5 is ready."
exec "$@"
- Build the Singularity image
Run this command to build the container image from the definition file. This can take several minutes.
singularity build G4_gdml_vis.sif G4_gdml_vis.def
After it finishes, you will have a file called G4_gdml_vis.sif in your directory.
- Use the image
You can now compile and run your Geant4 projects inside the container. Your current directory . is automatically available inside the container.
If you have Geant4 environment variables set on your host system, either unset them or run with --cleanenv to avoid conflicts.
singularity exec G4_gdml_vis.sif cmake -S ./MyProject -B build -D CMAKE_INSTALL_PREFIX=install
singularity exec G4_gdml_vis.sif cmake --build build -- -j install
singularity exec G4_gdml_vis.sif ./install/bin/MyApp -m run.mac -g geo.gdml -o outputFile.root
For more detailed information about building and running Singularity (now Apptainer) containers, including managing permissions and enabling graphical applications, please refer to the official Apptainer documentation.
I hope this helps.
Best,
Alvaro
PS:
Qt5 may not be easily available in the Ubuntu 24 repositories like it was in earlier versions of Ubuntu. This is because Qt6 is now expected to replace it (and I believe Qt6 is the default version for Geant4 as well). If you want to install Qt5 manually, you can either use the offline installer (link) or compile it from source (link).
After installing it, don’t forget to set the environment variables as shown below:
export QT_DIR=/usr/local/qt5.15.2
export QT_INCLUDE_DIR=${QT_DIR}/include/
export QT_LIBRARY=${QT_DIR}/lib/
export LD_LIBRARY_PATH=${QT_LIBRARY}:${LD_LIBRARY_PATH}
export PATH=$QT_DIR/bin/:$PATH