--- title: Sage安装指南(从conda-forge) description: 本人安装SageMath遇到的问题及解决方法 date: 2025-04-15 13:52:00 +0800 categories: [计算机] tags: [计算机,数学] --- ## 前言 本指南是基于本人(作为一名几乎不熟悉 WSL的及其任何语法的小白)安装SageMath时遭遇的问题与解决方案写成的,因此可能并不完善或管用.必须要提到的是,新手(包括我自己)并不推荐自己安装SageMath,如[Sage for Undergraduates](http://gregorybard.com/Sage.html)中所说: > A local installation --- not recommended for beginners, this can be great for those who do not have reliable access to a high-speed internet connection. Most Sage users access Sage via the internet. There is almost never any reason to do a local install of Sage on your laptop or home computer. The exception is if you have limited or no internet connectivity, such as in rural areas. This is good news, because it saves a lot of headaches and hassles (especially for students), that you would have to suffer if you were using Mathematica, Maple, Matlab, or Magma. > 本地安装 --- 不建议初学者进行,但这对那些没有可靠的高速互联网连接的用户来说是个不错的选择。大多数 Sage 用户通过互联网访问 Sage。几乎没有任何理由在笔记本电脑或家用电脑上进行本地安装。但如果您的互联网连接有限或没有互联网连接,例如在农村地区,情况就例外了。这是个好消息,因为如果您使用 Mathematica、Maple、Matlab 或 Magma,可以省去很多麻烦(尤其是学生)。 SageMath的在线版可在[SageMathCell](https://sagecell.sagemath.org/)或[CoCalc](https://www.cocalc.com/)中使用. 最后,本指南主要参考[Welcome to Sage Installation Guide](https://doc.sagemath.org/html/en/installation/index.html),是写给Windows用户的. ## 安装 ### 安装WSL 首先,按照[如何使用 WSL 在 Windows 上安装 WSL的](https://learn.microsoft.com/zh-cn/windows/wsl/install)安装 WSL (Windows Subsystem for WSL的):在控制台(按下Win+R,在窗口中输入`cmd`)中输入命令 ```powershell wsl --install ``` 右键单击并选择“以管理员身份运行”再重启.一般来说会直接安装WSL2,输入命令 ```powershell wsl -l -v ``` 若显示Version 2即为WSL2. 然后[设置 WSL的 用户名和密码](https://learn.microsoft.com/zh-cn/windows/wsl/setup/environment#set-up-your- WSL的-username-and-password),一般安装好会跳出命令行页面,其中指示用户设置用户名和密码.如果不记得了,可以在其中输入`whoami`来查看自己的用户名(其实命令行页面中每行前面会显示`user@xxx`格式,用户名即其中的`user`),再输入`passwd`来重设密码. ### 安装`Miniforge3` 在命令行中输入 ```shell cd ~ curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh" bash Miniforge3-$(uname)-$(uname -m).sh ``` 以安装`Miniforge3`,这一步可能会需要梯子(国内下载可能会很慢).其中可能会有几步需要按Enter或输入yes.官方教程并不清晰,具体请参照[该页面](https://github.com/conda-forge/sage-feedstock/issues/101)中的讨论或[Miniforge的安装指南](https://github.com/conda-forge/miniforge)操作: 在输入命令之后,可能会在某些地方停住.首先停住的地方需要按Enter,这表明你同意将软件安装在位置`~/miniforge3`.第二次停住时需要输入yes,这表明你同意相关法律条款.最后会有第三次停住,这时也输入yes.理论上此时`Miniforge3`已安装. 验证其是否安装可以在WSL的命令行中输入命令`conda`,如果没有出现 > conda: command not found 则说明安装成功.否则需要在WSL的命令行中输入 ```shell cd ~/miniforge3/bin ./conda init ``` 然后重新打开WSL的命令行.如果用户名前有一个`(base)`则说明已成功,此时可再次同上验证. 最后,去掉`(base)`字样可输入指令 ```shell conda config --set auto_activate_base false ``` 以禁用基础环境的自动激活. ### 安装并启用SageMath 在安装好`Miniforge3`后,在WSL的命令行中输入 ```shell cd ~ conda create -n sage sage python=3.13 ``` 即可安装好Sage.(最后这个3.13是最新版python版本,也可更改为其它版本)此时在命令行中输入 ```shell conda activate sage sage ``` 即可启用Sage.此时一般会出现页面 ``` ┌────────────────────────────────────────────────────────────────────┐ │ SageMath version 8.8, Release Date: 2019-06-26 │ │ Using Python 3.10.4. Type "help()" for help. │ └────────────────────────────────────────────────────────────────────┘ sage: ``` 可输入代码测试: ```python sage: factor(2005) 5 * 401 ``` 此时说明已安装成功. ## 使用Jupyter Notebook调用sage ### 安装Jupyter Notebook 使用命令 ```shell conda install -c conda-forge jupyterlab conda install -c conda-forge notebook ``` 或 ```shell pip install jupyterlab pip install notebook ``` 以安装JupyerLab 和Jupyter Notebook.此时可在启用sage后用命令 ```shell sage -n jupyter ``` 以启用Jupyter, 此时可从命令行输出的三个网址(在紫色时间之后)进入Jupyter. ### 自动打开网址 在命令行中输入 ```shell sudo apt update sudo apt install wslu ``` 以安装`wslu`,此时一般需要输入[第一步](/posts/Sage%E5%AE%89%E8%A3%85%E6%96%B9%E6%B3%95/#%E5%AE%89%E8%A3%85wsl)设置的密码. 安装后在Windows的文件资源管理器的地址栏(显示最近访问或此电脑的栏位)输入 ``` \\wsl.localhost\Ubuntu\home\{your_username} ``` 此处替换`{your_username}`为上述设置的用户名,用记事本打开其中的`.bashrc`文件,并在最后添加代码 ```shell conda activate sage export BROWSER=wslview ``` 此时每次打开wsl都可以自动激活`sage`环境,且使用Jupyter时能自动打开网址. ### 设置快捷方式 在桌面新建一个快捷方式,名字任选,在`目标位置`栏位输入 ``` wsl.exe -- bash -i -c "sage --notebook jupyterlab" ``` 大概需要过一段时间(初次一两分钟),就能自动打开sage并跳转到浏览器的jupyterlab中.如果不想用jupyterlab而用jupyter notebook,可将上述命令中的`jupyterlab`删掉.