Running Sage on Windows 10
Sage is a great tool for computational mathematics and related fields. It lets you borrow functionality from loads of great libraries, while still writing pythonic code, making the learning curve gentler and collaboration with computer scientists easier. In other words, it is the perfect CTF cryptanalyst tool.
Sage is included by default in various Linux distributions, and the developers also provide useful self contained tarballs that usually require just a single line of code to get compiled:
$ tar xf sage-x.y.tar.gz
$ cd sage-x.y
$ make
Yet, in my experience not every mathematics student receives a *nix education, which means that often the idea of running Sage on Linux can be perceived as daunting.
Sage on Windows
Since 2016, the Sage developers provide Windows installers for every release. I tried it earlier this year on a pretty powerful development machine, but I found the experience awful. I’m not exactly sure what this release is doing under the hood (maybe something alike to running Cygwin internally?), but the result is an incredibly slow and barebone command interface that does make working impossible.
Yet, a better solution for running Sage on Windows exist since Windows 10 released the Windows Subsystem for Linux (WSL). It requires only a few steps (and some waiting), but the result is a usable Sage setup.
WSL is basically a local install of a Linux distribution as an Windows program. It runs from the command line (more on that later), and behaves as an independent operating system, except that it has direct access to the Windows file system.
Installing Sage
Instructions to enable WSL can be found here. At the time of writing, it is sufficient to run PowerShell as Administrator and execute one command and then install your preferred distribution from the Microsoft Store. From now on I’ll assume the distribution to be Ubuntu 18.04, but it should apply also to newer versions.
After installing (and IIRC restarting a few times), a new Ubuntu program will be available on the start menu. Open it, and follow the instructions for the first run, it should ask you to setup a password. After all is done, we can finally install Sage by running the command
$ sudo apt install sagemath
which downloads and installs Sage. It will ask for the password that was just setup. Note that nothing will show up when entering the password, that’s fine.
Finally we can start working with Sage. To share files between Windows and Linux, I’d suggest to choose a working directory on Windows, let’s say C:\path\to\dir
where we can store code and experiment results. Then, every time we start Ubuntu, we can first access that directory, and then execute Sage:
$ cd /mnt/c/path/to/dir
$ sage
A warning message may appear after running Sage. It involves the WSL emulation of Linux not being perfect. My experience is that the warning message can be safely ignored (I’m not aware of how to trigger any errors due to it).
Installing a better terminal
One last note on running Sage on Windows is that the terminal provided by WSL is quite limited. It does not support any tabs or (the expected) right-click context menus, and also keyboard shortcuts like Ctrl+C/Ctrl+V don’t work as expected.
An alternative approach is to install a third party terminal, such as ConEmu. After installation run ConEmu. On first run it should propose you to pick the default shell (the WSL Bash shell should be one of the options). If the default Windows shell is chose instead, simply run
> bash
to start Ubuntu, and then proceed as above. Inside ConEmu, right clicking and keyboard shortcuts should run as expected.