Install Sage Math from source on WSL

Here’s how I got a fully featured Sage Math install on my system. I did this because I wanted to run on windows, and still be able to install packages, which isn’t possible with the precompiled binary.

Basic process:

  1. Enable hardware assisted virtualization.
  2. Set up WSL (Windows Subsystem for Linux)
  3. Install Linux into WSL
  4. Download and compile Sage
  5. Configure JupyterLab
  6. Make it easy to start from windows

Enable hardware assisted virtualization

WSL requires that your system supports hardware assisted virtualization (which pretty much all modern systems), there are 3 things that you need to get it working:

  • The CPU must support it
  • The motherboard must support it.
  • You need to enable it in the EFI (or BIOS)

This article takes you through getting everything sorted out:

https://support.bluestacks.com/hc/en-us/articles/115003174386-How-to-enable-Virtualization-VT-on-Windows-10-for-BlueStacks-4

Set up WSL 2

Follow this guide: https://docs.microsoft.com/en-us/windows/wsl/install-win10

Install and configure Linux:

  1. Go to the Microsoft store and install Ubuntu.

Note: If you already have Ubuntu installed, I’d recommend you follow these instructions to set up a separate, clean install for sage: https://hfakhraei.github.io/Install-multiple-instance-of-same-Linux-Distribution-on-WSL

  1. Start Ubuntu from the start menu. A command window should pop up.
  2. Run these commands to update Ubuntu:
    sudo apt update
    sudo apt upgrade
  1. Reboot by going (in Windows) to Services and restarting the LxssManager. You can also shut down all running WSL instances using the command wsl --shutdown in the Windows command prompt or Powershell.
  2. If necessary upgrade to the latest release: https://linuxconfig.org/how-to-upgrade-ubuntu-to-20-10

Set up Sage

These instructions are a simplified version of the official documentation: https://doc.sagemath.org/html/en/installation/source.html

  1. Start Ubuntu – All the commands in this section should be run on the Ubuntu installation.
  2. Install the packages we need to have ready in advance:
sudo apt install bc binutils bzip2 ca-certificates cliquer cmake coinor-cbc coinor-libcbc-dev curl default-jdk dvipng eclib-tools fflas-ffpack ffmpeg flintqs g++ gcc gfan gfortran git glpk-utils gmp-ecm imagemagick latexmk lcalc libatomic-ops-dev libavdevice-dev libboost-dev libbraiding-dev libbrial-dev libbrial-groebner-dev libbz2-dev libcdd-dev libcdd-tools libcliquer-dev libcurl4-openssl-dev libec-dev libecm-dev libffi-dev libfile-slurp-perl libflint-arb-dev libflint-dev libfreetype6-dev libgc-dev libgd-dev libgf2x-dev libgiac-dev libgivaro-dev libglpk-dev libgmp-dev libgsl-dev libiml-dev libisl-dev libjson-perl liblfunction-dev liblrcalc-dev liblzma-dev libm4rie-dev libmongodb-perl libmpc-dev libmpfi-dev libmpfr-dev libnauty-dev libncurses5-dev libntl-dev libopenblas-dev libpari-dev libpcre3-dev libperl-dev libplanarity-dev libppl-dev libpython3-dev libreadline-dev librw-dev libsqlite3-dev libssl-dev libsuitesparse-dev libsvg-perl libsymmetrica2-dev libterm-readkey-perl libterm-readline-gnu-perl libxml2-dev libxml-libxslt-perl libxml-writer-perl libz-dev libzmq3-dev libzn-poly-dev m4 make nauty ninja-build openssl palp pandoc pari-doc pari-elldata pari-galdata pari-galpol pari-gp2c pari-seadata patch perl pkg-config planarity ppl-dev python3 python3-distutils r-base-dev r-cran-lattice sqlite3 sympow tachyon tar texlive texlive-xetex xcas xz-utils yasm
  1. Download and extract the source tarball into a subfolder of your home directory
cd ~
wget http://www.mirrorservice.org/sites/www.sagemath.org/src/sage-9.2.tar.gz
tar xvf sage-9.2.tar.gz
  1. Start building
cd sage-9.2/
./configure
make

This will take a long time (3 hours on my system)

  1. Test sage
./sage

You should get a prompt that looks like this:

┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.2, Release Date: 2020-10-24                     │
│ Using Python 3.8.6. Type "help()" for help.                        │
└────────────────────────────────────────────────────────────────────┘
sage:
  1. Type exit and press enter to leave.
  1. Make the command available from anywhere on this ubuntu system – edit ~/.bashrc and add this line:
alias sage=~/sage-9.2/sage
  1. Close ubuntu then reopen it.
  2. Try typing sage and verify that it starts.

JupyterLab

  1. Install Jupyter Lab and it’s extras:
sage -i jupyterlab_widgets

This also involves compliation from source, so will take a while.

  1. When it’s done, try starting the jupyterlab notebook like this:
sage --notebook jupyterlab

You might notice a problem – the list of files only includes those on your linux machine – you probably want the ones on your windows computer. You can do this by navigating to where the windows files are mounted in linux.

  1. Try it (fill in your windows username instead of the square brackets):
cd /mnt/c/Documents\ and\ Settings\[your windows username]
sage --notebook jupyterlab

The list of files should now be the ones in that folder on your windows machine.

  1. Lets wrap this all in a script for convenience – Create a file called ~/sage_nb.sh and put these lines into it:
#!/bin/bash
cd /mnt/c/Documents\ and\ Settings/[your windows username]
/home/[your linux username]/sage-9.2/sage --notebook jupyterlab
  1. We need to make it executable – ie. so linux will run this file like it would run a program:
chmod ug+x sage_nb.sh
  1. Now go back to your home directory and try it again:
cd ~
./sage_nb.sh

The JupyterLab server should start, showing your windows files in the browser.

Create windows shortcut

This is a final nicety that lets you start the JupyterLab server in one click:

  1. Open Windows explorer, and type %APPDATA%\Microsoft\Windows\Start Menu\Programs
  2. Make a copy of any of the program icons – we’ll customise it to do what we want
  3. Right-click, and choose properties.
  4. On the General tab:
    • Change the name to whatever you want.
  5. On the Shortcut tab:
    • Change Target to: ubuntu.exe run ~/sage_nb.sh
    • Change Start in to: %USERPROFILE%
    • Change Run to: Minimised
    • Change the icon if you want – there are plenty of pieces of software that can create a .ico icon file one from an image file.
  6. Now hit the start button or key and type the name you gave it. it should appear in the list, and should load the server and fire up your browser when you click on it.

Software versions:

This is what I used when I wrote this post – you might get different results if your versions are significantly different.

  • Windows 10 Pro Version 2004 (OS Build 19041.264)
  • Ubuntu 20.10 (Originally installed 20.04 as 20.10 was not available in the Windows Store)
  • Sage 9.2

5 Comments

  1. jean-pierre Ortolland

    Hi
    Thank you very much for writing this process for installing SageMath in WSL. , your work is very impressive !

    sorry but I still meet one last pb, each time I try to ./sage_nb.sh
    I have to make a copy paste of the http with the tocken in a browser to launch JupyterLab

    Start : This command cannot be run due to the error: The system cannot find
    the file specified.
    At line:1 char:1
    + Start “file:///home/ortollj/.local/share/jupyter/runtime/nbserver-141 …

    and in fact I do not actually see a file that begins with
    nbserver-141 but rather nbserver-1441-open.html

    ortollj@DESKTOP-K30FLRP:~$ cd /home/ortollj/.local/share/jupyter/runtime
    ortollj@DESKTOP-K30FLRP:~/.local/share/jupyter/runtime$ ls
    nbserver-1209-open.html nbserver-1441-open.html nbserver-1672-open.html nbserver-1902-open.html notebook_cookie_secret
    nbserver-1209.json nbserver-1441.json nbserver-1672.json nbserver-1902.json
    ortollj@DESKTOP-K30FLRP:~/.local/share/jupyter/runtime$

    see my last comment (click on see more comment) on
    https://ask.sagemath.org/question/53294/installing-optional-packages-infinite-loop/

    someone had the same pb as mine on
    https://stackoverflow.com/questions/62814002/start-this-command-cannot-be-run-due-to-the-error-the-system-cannot-find-the

    ortollj@DESKTOP-K30FLRP:~$ jupyter notebook –generate-config

    Command ‘jupyter’ not found, but can be installed with:

    sudo apt install jupyter-core

    ortollj@DESKTOP-K30FLRP:~$ sage jupyter notebook –generate-config
    /home/ortollj/sage-9.2/local/bin/python3: can’t open file ‘jupyter’: [Errno 2] No such file or directory
    ortollj@DESKTOP-K30FLRP:~$

    do you have any idea what I need to do ?

    • jean-pierre Ortolland

      here below is the content of the file :jupyter_notebook_config.py

      # Configuration file for jupyter-notebook.

      #——————————————————————————
      # Application(SingletonConfigurable) configuration
      #——————————————————————————

      ## This is an application.

      ## The date format used by logging formatters for %(asctime)s
      #c.Application.log_datefmt = ‘%Y-%m-%d %H:%M:%S’

      ## The Logging format template
      #c.Application.log_format = ‘[%(name)s]%(highlevel)s %(message)s’

      ## Set the log level by value or name.
      #c.Application.log_level = 30

      #——————————————————————————
      # JupyterApp(Application) configuration
      #——————————————————————————

      ## Base class for Jupyter applications

      ## Answer yes to any prompts.
      #c.JupyterApp.answer_yes = False

      ## Full path of a config file.
      #c.JupyterApp.config_file = ”

      ## Specify a config file to load.
      #c.JupyterApp.config_file_name = ”

      ## Generate default config file.
      #c.JupyterApp.generate_config = False

  2. jean-pierre Ortolland

    I succeed creating file jupyterlab –generate-config :

    ortollj@DESKTOP-K30FLRP:~$ sage –notebook jupyterlab –generate-config
    ┌────────────────────────────────────────────────────────────────────┐
    │ SageMath version 9.2, Release Date: 2020-10-24 │
    │ Using Python 3.8.5. Type “help()” for help. │
    └────────────────────────────────────────────────────────────────────┘
    Please wait while the Jupyterlab server starts…
    Writing default config to: /home/ortollj/.sage//jupyter-4.1/jupyter_notebook_config.py
    ortollj@DESKTOP-K30FLRP:~$

    but what does he mean by : then change the following line: c.NotebookApp.use_redirect_file = False

    remove the # ?

  3. jean-pierre Ortolland

    below the content of /home/ortollj/.sage//jupyter-4.1/jupyter_notebook_config.py:

    # Configuration file for jupyter-notebook.

    #——————————————————————————
    # Application(SingletonConfigurable) configuration
    #——————————————————————————

    ## This is an application.

    ## The date format used by logging formatters for %(asctime)s
    #c.Application.log_datefmt = ‘%Y-%m-%d %H:%M:%S’

    ## The Logging format template
    #c.Application.log_format = ‘[%(name)s]%(highlevel)s %(message)s’

    ## Set the log level by value or name.
    #c.Application.log_level = 30

    #——————————————————————————
    # JupyterApp(Application) configuration
    #——————————————————————————

    ## Base class for Jupyter applications

    ## Answer yes to any prompts.
    #c.JupyterApp.answer_yes = False

    ## Full path of a config file.
    #c.JupyterApp.config_file = ”

    ## Specify a config file to load.
    #c.JupyterApp.config_file_name = ”

    ## Generate default config file.
    #c.JupyterApp.generate_config = False

    #——————————————————————————
    # NotebookApp(JupyterApp) configuration
    #——————————————————————————
    c.NotebookApp.use_redirect_file = False

    ## Set the Access-Control-Allow-Credentials: true header

  4. jean-pierre Ortolland

    all is ok Now !!! 😉

    in the file /home/ortollj/.sage//jupyter-4.1/jupyter_notebook_config.py, i added the line:
    #——————————————————————————
    # NotebookApp(JupyterApp) configuration
    #——————————————————————————
    c.NotebookApp.use_redirect_file = False

    and in the file ~/.bashrc I added the line, after installing Chrome Browser in W10:

    export BROWSER=’/mnt/c/Program Files/Google/Chrome/Application/chrome.exe’

    (not Program Files (x86)/ for me,

    my chrome.exe is located in Program Files/Google/Chrome/Application/)

    and now when launch my windows shortcut Jupyter_lab is lauching ;-).
    Thank you very much Sandy Scott.

Leave a Reply

Your email address will not be published. Required fields are marked *