Sage pour Windows 10 et supérieur

Windows users could at some point use a binary file to install Sage on their computers, but now this way of installation is no more available for newer Sage versions. The standard procedure is to use the Windows Subsystem for Linux (WSL), which is already installed on Windows 10 and later. I am no expert in Linux or WSL so maybe some steps are unnecessary or lengthy but at least it worked for me.

  1. Activate the virtualisation in the BIOS of your PC (see for instance this page).

  2. Install WSL, as described here. You can first check this page to see which Linux distribution has the version of Sage that you want (in our case, the latest, which is 9.7 as I am writing this note). Then compare with the available distributions in WSL by wsl -l -o in a Windows Shell (or PowerShell).

    • If there is a match, then you can directly install the corresponding distribution by wsl --install -d <DistributionName> where <DistributionName> has to be changed accordingly.
    • If not, then just run wsl --install, then you can see which distribution has been installed with wsl -l -v and then uninstall it by typing wsl --unregister Ubuntu-20.04 (if Ubuntu 20.04 is the distribution listed by the previous instruction).
    • Instead of the previous step, you should be able to just activate WSL in the Windows Features settings.
    • You can still install Sage 9.7 in Ubuntu from the source files (see for instance here; we will use some steps that are described here), but it is a bit long. Instead, we will see how to install Sage 9.7 after an ArchLinux installation on WSL.
  3. As I am writing this note, the last version of Sage is not available on the distributions that are proposed in WSL (in particular, in Ubuntu). Currently, the last version of Sage is available on the ArchLinux repository. ArchLinux is easy to install on WSL, see here (I did Method 1: zip file). Some comments about the installation steps:

    • For step 2 (extract all files), I did extract in C:\Users\salim\Arch.
    • For step 3, after the first time you ran Arch.exe it will close.
    • If you want to try ArchLinux, you can run Arch.exe again and check that it has registered in WSL just by running a Windows shell and typing wsl -l -v, which should indicate that Arch is running (the “Version” refers to the version of WSL and should be 2). Note that, to run Arch.exe, you can either directly click on it in the explorer, or open a Windows shell, navigate to the Arch folder and then type .\Arch.exe. Note that even if you close the Arch window (or after having entered exit in the Arch terminal), WSL indicates that Arch is running. To close it you can just type wsl -t Arch in a Windows shell.
    • Be sure to complete the section “Setup after install” (note that WSL2 is what you have installed thus you are not concerned by the warning about WSL1). Note that each time you see “Arch.exe” you should enter .\Arch.exe (in a Windows shell). The steps are: setting the root password, set up the default user (type your username instead of {username}, for instance in a Windows shell .\Arch.exe config --default-user salim) and initialise keyring.
    • I believe that I had to run twice sudo pacman -Syu, which updates the installed packages, in order for it to fully complete (in which case it should say “there is nothing to do” at the end). If you are used to Ubuntu, this command sudo pacman -Syu is the analogue of sudo apt install update && sudo apt full-upgrade.
    • Note that in an Arch session, you can open your current directory in the Windows File Explorer by typing explorer.exe . in the Arch console (do not forget the finat dot! It stands for “current directory”). You can also just enter \\wsl$ in the Windows Explorer.
  4. Now you can just install (and wait a bit…) Sage by typing sudo pacman -S sagemath in an Arch console.

  5. Now Sage is installed on your computer with WSL, congratulations! You can run it in an Arch console simply with sage. Now I will explain how to use Jupyter (in fact JupyterLab), and this will require a few more steps.

  6. I believe Jupyter is not installed by default in the sagemath package: you can install it by sudo pacman -S jupyter in an Arch session and then launch Sage on Jupyter by sage -n jupyter. Instead I will explain how to launch Sage with JupyterLab (an evolved version of Jupyter) and create a shortcut on your Windows desktop. As for Jupyter, you first have to install it by sudo pacman -S jupyterlab.

  7. In your Arch session, go to your personnal folder (cd ~) and then create a file sage_nb.sh (for instance first open the Windows Explorer by explorer.exe . as mentionned in 3 and then just right-click and create a new Notepad). Fill it with:

    #!/bin/bash
    # Switch to the desired windows directory
    cd /mnt/c/Users/salim/
    # Start the Jupyter notebook
    sage -n jupyterlab --browser="/mnt/c/'Program Files (x86)'/Microsoft/Edge/Application/msedge.exe %s"
    

    (do not forget the final %s! It means something like “consider what precedes as a string”). You can replace /mnt/c/Users/salim by the directory that you want to be opened as JupyterLab lauches (this path stands for the Windows path C:\Users\Salim). If you have some spaces in your path, the name of the folder should be enclosed between two apostrophes (as for the forthcoming 'Program Files (x86)'). Note also that I use Microsoft Edge, if you want to use another web browser you have to change the path accordingly (for instance "/mnt/c/'Program Files (x86)'/Google/Chrome/Application/chrome.exe %s" for Google Chrome or "/mnt/c/'Program Files'/'Mozilla Firefox'/firefox.exe %s" for Mozilla Firefox).

  8. Still in your an Arch session, make this sage_nb.sh file executable by (first going into your personal Arch folder by cd ~) typing chmod ug+x sage_nb.sh.

  9. In order for your web browser to open at the right url, you have to modify the JupyterLab configuration file in the following way.

    • In your Arch session, type sage -n jupyterlab --generate-config. It will tell you that a file jupyter_lab_config.py was created in home\salim\.sage\jupyter-4.1 (where of course salim is replaced by your username). Remember that you can open it for instance by the explorer.exe . thing.

    • Look at the section ## Disable launching browser by redirect file (somewhere at the end of the file) and modify the line #c.ServerApp.use_redirect_file = True into c.ServerApp.use_redirect_file = False. Ensure that you have deleted the # and that the line does not begin with a blank space, otherwise your configuration file will be ignored.

    • Note that there are some lines c.ServerApp.browser and c.ServerApp.open_browser, which would make the --browser=... of point 7 useless but I didn’t manage to make this work.

  10. Now if you type

    cd ~
    .\sage_nb.py`
    

    in your Arch session, your JupyterLab should launch in the selected web browser. To create a shortcut that will do this on your Windows desktop, you can follow these steps:

    • Right-click on the desktop then New > Shortcut
    • Put C:\Users\salim\Arch\Arch.exe run ~/sage_nb.sh as location
    • Add the name you want
    • Once the shortcut is created, you can go to its properties and change the “Run” property to “Minimised”, so that you won’t see the shell showing up as you open Sage. If you still have the old Sage (installed from a pre-compiled binary) on your PC then you can pick up the .ico file (first copy it somewhere else since you are supposed to uninstall this old Sage!), and if not you can find it here.
  11. When the next version of Sage is be available, check here to see if it is available on ArchLinux. If it is, to upgrade the sagemath package you have to upgrade your whole ArchLinux. If you do it regularly, then by the same instruction sudo pacman -Syu as in point 3 this will do the job. On the contrary, use the instruction sudo pacman -Sy archlinux-keyring && sudo pacman -Su (if you try to use Syu as before then you will get some errors).