How to containerize Sage math

When you want to run a recent version of Sage math (from, 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).

As superuser:

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
# choose a commit (this one succeeds)
git checkout f10820ff877ea61a2fdbf0d7fc3c1445ead77d72
make configure
export MAKE="make -j9"
# Since "make" will compile and install, you should specify a prefix directory
./configure --prefix=$HOME/progs
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 the BROWSER environment variable to either firefox or chromium).

p0 = vector([0, 0, 0])
p1 = vector([1, 1, 1])
p2 = vector([1, -1, 3])

arrow3d(p0, p1, color='red') + arrow3d(p1, p2)
If you think you found a mistake in this blog post, or would like to suggest an improvement to this blog post, you can write me an e-mail to the address public dot michael at franzl dot name; as subject please use the prefix "Comment to blog post" and append the post title.
Copyright © 2023 Michael Franzl