The latest version of SageMath (10.4) can be built smoothly in Ubuntu 24.04 with the system gcc (13.2) and python (3.12.3). Here are the steps (adapted from this page):
First, install the Ubuntu packages required to build SageMath via the command
sudo apt install automake bc binutils bzip2 ca-certificates cliquer cmake curl ecl eclib-tools fflas-ffpack flintqs g++ gengetopt gfan gfortran git glpk-utils gmp-ecm lcalc libatomic-ops-dev libboost-dev libbraiding-dev libbz2-dev libcdd-dev libcdd-tools libcliquer-dev libcurl4-openssl-dev libec-dev libecm-dev libffi-dev libflint-dev libfreetype-dev libgc-dev libgd-dev libgf2x-dev libgiac-dev libgivaro-dev libglpk-dev libgmp-dev libgsl-dev libhomfly-dev libiml-dev liblfunction-dev liblrcalc-dev liblzma-dev libm4rie-dev libmpc-dev libmpfi-dev libmpfr-dev libncurses-dev libntl-dev libopenblas-dev libpari-dev libpcre3-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 zlib1g-dev libzmq3-dev libzn-poly-dev m4 make nauty openssl palp pari-doc pari-elldata pari-galdata pari-galpol pari-gp2c pari-seadata patch perl pkg-config planarity ppl-dev python3-setuptools python3-venv r-base-dev r-cran-lattice singular sqlite3 sympow tachyon tar tox xcas xz-utils
To benefit from extra functionalities when running SageMath (e.g. exporting a Jupyter notebook to pdf), it is recommended to install some additional Ubuntu packages:
sudo apt install texlive-latex-extra texlive-xetex latexmk pandoc dvipng
Then you can download SageMath 10.4 sources and launch the build by typing (from you home directory or from the directory in which you want SageMath to be installed):
git clone --branch master https://github.com/sagemath/sage.git
cd sage
make configure
./configure
MAKE="make -j8" make
The last command launches the build in parallel on 8 threads (as specified by -j8); adapt to your CPU (usually you may choose a number of threads that is twice the number of cores of your CPU). The build time is about half an hour on a modern CPU. Once it is finished, you can create a symbolic link by typing:
sudo ln -sf $(pwd)/sage /usr/local/bin
Then you can change to any directory and start SageMath in Jupyter by running sage -n (or sage -n jupyterlab to run SageMath in Jupyterlab).