看到SageMath发布了10.1版,但没有了以前即下即用的binary版本,遂记录一下安装过程(Linux版本)。

首先需要下载几个工具

  • Miniconda3:解决依赖问题和环境污染问题的,SageMath好像从9.6后就推荐用Conda解决依赖包的问题,然后就下架了binary…,下载地址:https://docs.conda.io/en/latest/miniconda.html
  • Mambaforce:按照官网教程来即可:https://doc.sagemath.org/html/en/installation/conda.html
  • SageMath 10.1的源码:如果只用官网的mamba create好像会依赖不全,所以我选择用源码再build一遍,下载地址:https://www.sagemath.org/download-source.html

懒人包:如果你是Linux 64bits的话

1
2
3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
curl -L -O https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-$(uname)-$(uname -m).sh
wget https://mirrors.aliyun.com/sagemath/src/sage-10.0.tar.gz

然后Miniconda3和Mambaforce下载下来的都是个安装脚本,直接运行然后按照引导安装即可

建议先安装Conda,安装完后需要激活一下bashrc,或者重启一下终端也行(其他shell的source换成对应的rc文件)

1
2
3
sh Miniconda3-latest-Linux-x86_64.sh
source ~/.bashrc
conda --version

如无意外应该可以运行conda了,但是终端执行命令会出现个烦人的(base),可以参考这里解决:

1
2
echo "conda deactivate" >> ~/.bashrc
source ~/.bashrc

然后安装Mamba

1
2
3
sh Mambaforge-$(uname)-$(uname -m).sh
source ~/.bashrc
mamba --version

解压源码压缩包

1
2
tar xf ./sage-10.0.tar.gz
cd ./sage-10.0

在build之前先换一下Conda的源,可以参考这里,比如我换阿里的源,就把~/.condarc改成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
channels:
- defaults
show_channel_urls: true
default_channels:
- http://mirrors.aliyun.com/anaconda/pkgs/main
- http://mirrors.aliyun.com/anaconda/pkgs/r
- http://mirrors.aliyun.com/anaconda/pkgs/msys2
custom_channels:
conda-forge: http://mirrors.aliyun.com/anaconda/cloud
msys2: http://mirrors.aliyun.com/anaconda/cloud
bioconda: http://mirrors.aliyun.com/anaconda/cloud
menpo: http://mirrors.aliyun.com/anaconda/cloud
pytorch: http://mirrors.aliyun.com/anaconda/cloud
simpleitk: http://mirrors.aliyun.com/anaconda/cloud

最后就是按照官网最下面的教程一顿操作了,build的时候需要在sage-10.0目录下执行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
export SAGE_NUM_THREADS=16
./bootstrap-conda
mamba env create --file src/environment-dev.yml --name sage-dev
mamba activate sage-dev
./bootstrap
./configure --with-python=$CONDA_PREFIX/bin/python \
--prefix=$CONDA_PREFIX \
$(for pkg in $(./sage -package list :standard: \
--exclude rpy2 \
--has-file spkg-configure.m4 \
--has-file distros/conda.txt); do \
echo --with-system-$pkg=force; \
done)
pip install --no-build-isolation -v -v --editable ./pkgs/sage-conf ./pkgs/sage-setup
pip install --no-build-isolation -v -v --editable ./src
sage -c 'print(version())'

mamba deactivate

PS:SAGE_NUM_THREADS可以根据自己电脑的线程数设置。然后最好分开执行,方便观察报错(虽然我这好像没报错

PPS:建议安装时使用“好”一点的网络(你懂的)

然后每次运行的时候都需要先mamba activate一下,执行完要mamba deactivate

1
2
3
mamba activate sage-dev
# run sage ... ...
mamba deactivate

其实有点麻烦,

如果想一键启动的话可以把以下脚本放到PATH的某个目录中,如果你电脑原本没sage的话甚至可以命名为sage,或者想区分版本的话命名sage10之类的,然后下面的bash需要换成你自己的shell

1
2
3
4
5
6
#!/usr/bin/env bash
source ~/mambaforge/etc/profile.d/conda.sh
source ~/mambaforge/etc/profile.d/mamba.sh
mamba activate sage-dev
sage $*
mamba deactivate

PS:那两行source主要用于解决mamba无法在shell脚本中运行的问题,参考这里

然后运行的时候执行sage ...sage10 ...就好了,就是相比于之前的binary版本会多了conda/mamba的启动时间,建议需要多次执行脚本时还是用activate/deactivate的方式。