How to containerize Sage math
When you want to run a recent version of Sage math (from sagemath.org), then pretty much the only option is to build it from sources, because:
- Pre-built binaries are discontinued⮺
- Packages in Linux distributions are more or less chronically outdated
However, the build prerequisites are relatively massive⮺ and it may not be desired to install all of these packages on the host.
Even though the build doesn’t require running code as superuser, it still makes sense to containerize Sage math.
What follows are working build steps on Debian Bookworm that are the result of the official build instructions (linked above).
apt-get install autoconf automake libtool pkg-config
apt-get install bc binutils bzip2 ca-certificates cliquer cmake curl ecl eclib-tools fflas-ffpack g++ gcc gengetopt gfan gfortran glpk-utils gmp-ecm lcalc libatomic-ops-dev libboost-dev libbraiding-dev libbrial-dev libbrial-groebner-dev libbz2-dev libcdd-dev libcdd-tools libcliquer-dev libcurl4-openssl-dev libec-dev libecm-dev libffi-dev libflint-arb-dev libflint-dev libfplll-dev libfreetype6-dev libgc-dev libgd-dev libgf2x-dev libgiac-dev libgivaro-dev libglpk-dev libgmp-dev libgsl-dev libhomfly-dev libiml-dev liblfunction-dev liblinbox-dev liblrcalc-dev liblzma-dev libm4ri-dev libm4rie-dev libmpc-dev libmpfi-dev libmpfr-dev libncurses5-dev libntl-dev libopenblas-dev libpari-dev libplanarity-dev libppl-dev libprimesieve-dev libpython3-dev libqhull-dev libreadline-dev librw-dev libsingular4-dev libsqlite3-dev libssl-dev libsuitesparse-dev libsymmetrica2-dev libz-dev libzmq3-dev m4 make maxima maxima-sage meson nauty ninja-build openssl palp pari-doc pari-elldata pari-galdata pari-galpol pari-gp2c pari-seadata patch perl pkg-config planarity ppl-dev python3 python3-distutils python3-venv singular singular-doc sqlite3 sympow tachyon tar texinfo tox xcas xz-utils
apt-get install texlive
apt-get install tk tk-dev
apt-get install texlive-generic-extra texlive-xetex latexmk pandoc dvipng default-jdk ffmpeg libavdevice-dev
As unprivileged user:
# Get Sage
git clone https://github.com/sagemath/sage.git
# choose a commit (this one succeeds)
git checkout f10820ff877ea61a2fdbf0d7fc3c1445ead77d72
export MAKE="make -j9"
# Since "make" will compile and install, you should specify a prefix directory
make # takes around half an hour on a recent Desktop machine
# That's it, start Sage
One of the difficulties here is when Sage tries to open a 3D plot window using the local browser,
which is impossible because there is no display server running in the container.
Luckily, I’ve solved this problem in my previous blog post
How to run graphical Wayland applications in systemd-nspawn.
If you follow the steps there, you will be able to enter the following statements into Sage, and it
will open an interactive 3D plot in the host’s graphical window environment (you want to set
BROWSER environment variable to either
p0 = vector([0, 0, 0])
p1 = vector([1, 1, 1])
p2 = vector([1, -1, 3])
arrow3d(p0, p1, color='red') + arrow3d(p1, p2)