Setup

Don't loose 1 to 5 hours of your time to compile Sagemath from source... On Fedora and Ubuntu, or via WSL for students on Windows, you'll get a running Sagemath environnement in a few minutes, including openssl and necessary pip dependencies (numpy and pycryptodome).

If you are on Windows and don't know WSL yet, we recommend this Fedora WSL setup tutorial.

The official installation via conda-forge installs sage 10.7, is working well and can be installed in ~2min. It just requires to setup conda, enable it, and enable a sage environnement each time you want to use. It also changes your prompt with (base) or (sage) suffix and you have to configure your shell to load conda on start.

This setup using distrobox installs sage 10.8, is taking a bit more time (~4m30s), but is much more automated that the conda-forge installation. You just run one command, in contrary to the ~8 steps of the other installation script. After installation, there is nothing to do manually, just run sage.

It's up to you to choose between the two :) In both cases, you might want to benefit from the tips at the bottom. They include a watch mode to run Python and sage scripts to rerun your scripts when they change.

On Fedora & Ubuntu

Install base and CRY tools via lxup, including sagemath. Help: I don't have lxup !

lxup play base
lxup play cry

If you don't see any red output, you should have a fully working CRY environnement! You'll learn how to use it in the next pages.

If you don't use Fish

Make sure ~/.local/bin is in your PATH

Setup details
  • openssl
  • sagemath - via Distrobox setup named sagebox
  • pycryptodome and numpy in sagebox

Thanks to @a-mango I discovered a very smart approach with Distrobox that let us expose a binary from a container to the host! 🤯🤯

How to use sage ?

Sage is installed inside a container, it is mostly transparent, but you will see some additionnal output on the first start.

> sage
Starting container...                   	[ OK ]
Installing basic packages...            	[ OK ]
Setting up devpts mounts...             	[ OK ]
Setting up read-only mounts...          	[ OK ]
Setting up read-write mounts...         	[ OK ]
Setting up host's sockets integration...	[ OK ]
Integrating host's themes, icons, fonts...	[ OK ]
Setting up distrobox profile...         	[ OK ]
Setting up sudo...                      	[ OK ]
Setting up user's group list...         	[ OK ]
Setting up skel...                      	[ OK ]

Container Setup Complete!
┌─────────────────────────────────────────────────┐
│ SageMath version 10.8, Release Date: 2025-12-18 │
│ Using Python 3.12.5. Type "help()" for help.    │
└─────────────────────────────────────────────────┘
sage: 

You can try out Sagemath using any available function like factor() (primes number factorisation)

sage: factor(123234)
2 * 3 * 19 * 23 * 47

> # hit ctrl+d to quit sage !

You can write Sage scripts inside .sage file

> cat test.sage
print("hello from sage")

> sage test.sage
hello from sage

If you need to install another dependency with pip or apt, you have to enter the container first. Here is how to do it.

distrobox enter sagebox

# inside the opened shell
sage -pip install <name>
apt install <name>
exit
How the distrobox setup works under the hood ?

Here are the equivalent commands running in the Ansible playbook. If you need replicate on another Linux distribution.

# Basic preparation
cd ~/Desktop
git clone https://codeberg.org/samuelroland/productivity.git
cd productivity/HEIG/common-setup

# Install Podman and Distrobox
sudo dnf install -y podman distrobox

distrobox create --name sagebox --image docker.io/sagemath/sagemath --home "$HOME/.local/sagebox_home" --pull --yes
cd s4
distrobox enter sagebox

# Note: there is currently a bug in distrobox on the first entering of the distrobox box...
# Just try it a second time it should work.

# Then inside the shell opened by distrobox
sh sage-distrobox-setup.sh # Run the script to setup pycryptodome numpy and export /usr/bin/sage
fish_add_path ~/.local/bin

# Note: if you want to automate without manually entering the distrobox shell
# you can create a script with the 2 lines and start it in distrobox like this.
distrobox enter sagebox -- sh sage-distrobox-setup.sh

Tips

Note: The tools scr and r are part of lxup play base.

  • scr can generate some Python hello world

    scr python # in SCR_TMP_FOLDER
    scr python hello-world # OR in the current folder under hello-world subfolder
    
  • scr can also generate a Sage hello world

    scr sage
    
  • r can run some Python

    r hello.py
    
  • r can run also run some Sage ! (you are right... we saved 3 chars on typing)

    r hello.sage
    
  • r also has a watch mode ! Try adding -w to reload the script on change.

    r -w hello.py
    # Also works for .sage
    r -w hello.sage