Installing Sage

  • Go to the Official Sage Installation Page.
  • In the line Linux macOS Windows, click on your operating system.
  • The instructions below are for your convenience only. Compare to the official documentation when running, and let me know if there are any changes, differences, or issues.
CoCalc: Creating an Online Account

You can use Sage online for free at CoCalc

  1. Create a new account

    • Select "student" role
    • Use a personal email address so you can continue using it after graduation!
    • Pick a username and password.
  2. Log into your account

  3. Create a new "Project".

  4. Create a new File

    • Jupyter Notebook
    • SageMath Kernel
  5. Click on Explorer in the menu on the left to navigate between files.

macOS: Installing Sage

Follow the instructions at https://doc.sagemath.org/html/en/installation/index.html

It is easist to use the binary build files. According to SageMath, it is signed and notarized for use with the macOS ecosystem.

  1. Click on the macOS version of the instructions.

  2. Click on binary build of SageMath

  3. Determine your processor type by going to Apple icon > About This Mac and looking at the Chip field.

    • If your chip says Apple M..., select the ...arm64.dmg file
    • If your chip says Intel select ...x86_64.dmg

After downloading, install it by opening it, and dragging the SageMath icon to the Applications folder.

  • Double click on the program icon to run Sage.
  • Open as a notebook
  • Select a directory where you want your worksheet files saved.
Windows: Installing Sage

Follow the instructions at https://doc.sagemath.org/html/en/installation/index.html

This involves installing and activating the Windows Subsystem for Linux (WSL). This will take a little time, but will provide you access to a full linux distribution inside Windows.

  1. Install Ubuntu Linux on WSL

    • Option 1: Open the Windows Store and search for Ubuntu
    • Option 2: Open the PowerShell application and run

      wsl --install
      
    • Reboot your machine if needed

  2. Search your Start Menu for Ubuntu. Right click and select add link to taskbar so you can find this more easily. Then left click to open it.

  3. Inside Ubuntu run the commands from the official Sage Math installation page. The code at the time of writing is given below, but check the official page and use their code instead

    curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
    bash Miniforge3-$(uname)-$(uname -m).sh
    

    There will be several questions

    - accept the license terms
    - save to the default directory
    - **Important:** Select **yes** when asked whether to proceed with initialization.
    

    If accidentally say no to initialization, you can just run miniforge3/condabin/conda init You may also need to restart the Ubuntu application (called the shell). Then run the following:

    conda create -n sage sage python=3.12
    

    Again you will need to type y to proceed. Download and installation will take a little while. When the installation ends, run:

    conda activate sage
    
  4. To run Sage, first open Ubuntu. Then run the following command:

    jupyter lab --NotebookApp.token='Risheequai3diuGhaiMuano4EeCh7geB'
    

    Once its finished setting up, you can click on the following link to open the Jupyter Notebook application

    http://localhost:8888/lab?token=Risheequai3diuGhaiMuano4EeCh7geB

    That command includes a hardcoded authentication token, which should be secure enough for local use with a single user. For more security, run

    jupyter lab
    

    You will then need to read through the output to find the randomly generated token as below

    To access the server, ...
    Or copy and paste one of these URLs:
        http://localhost:8888/lab?token=b2b6a...43a
    

    Copy the entire address into a web browser, and load it.

Running Sage in a Jupyter Notebook

Starting Sage

If prompted when opening Sage, you want to run it using a Jupyter Notebook.

CoCalc: Logging into Sage Online
  1. Log into your account

  2. Click on Explorer in the menu on the left to navigate between files, or click on Projects to change projects.

  3. Open an existing file, or create a new file using

    • Jupyter Notebook

    • SageMath Kernel

macOS: Running Sage
  • Click on the application icon.

  • The launch screen will appear. Open as a notebook. In the first dropdown, select JupyterLab. Finally, click Select and navigate to your Download or Documents directory.

    A screenshot of the macOS Sage Launcher.

When Jupyter opens, you will see your a user directory.

  • macOS, this will be the directory you selected.

You can open an existing notebook (with a .ipynb extension), or you can create a new one.

  • Use a Sage kernel to run Sage code.

  • Use a Python kernel to run pure Python code.

Windows: Running Sage

When Jupyter opens, you will see your a user directory.

  • Windows, this will be a hidden directory you cannot access.
    You will need to download worksheets to a normal directory.

You can open an existing notebook (with a .ipynb extension), or you can create a new one.

  • Use a Sage kernel to run Sage code.

  • Use a Python kernel to run pure Python code.

Understanding Jupyter Lab

Home Screen

When you start Sage, you will see a screen with the word Jupyter at the top. Below, you will see a (possibly empty) folder with unfamiliar contents.

  • Once you have created notebooks, you can open them by clicking on them here, and you can organize them into directories.

On the right side of the screen you will see several options:

  • New

    • Select Python 3 to run standard Python code

    • Select SageMath 10.7 to run Sage Math code

  • Upload a file from your local hard drive.

Inside a Notebook
  • File commands:

    • save file

    • download -- This is the only way to share worksheet files if you are using Windows.

  • Cell commands

    • insert blank cell

    • cut/copy/paste cell

    • run current cell

  • Runtime commands

    • stop execution

    • restart kernel

    • restart and run all cells

  • There is a Dropdown that lets you select code or markdown.

    • Code cells contain code that is executed when you click run

    • Markdown cells contain text that is nicely formatted when you click run

  • At the far right, you will see SageMath 10.7. This tells you the existing Kernel. You use a Sage kernel to run Sage code, and a Python kernel to run pure Python.

Production Quality Documents with Quarto

Installing Quarto

CoCalc

No additional steps are required - Quarto is already installed in the CoCalc terminal.

macOS: Installing Quarto
Windows: Installing Quarto
  • In Jupyter, click on File > New > Terminal

  • In the Terminal, run the following command

    wget -O /tmp/quarto.deb https://quarto.org/download/latest/quarto-linux-amd64.deb
    sudo apt install /tmp/quarto.deb          
    

Using Quarto

Quarto commands are run in the Terminal, and not in any Jupyter Worksheet.

The most convenient workflow is to open the worksheet and the terminal at the same time.

CoCalc: Preparing your Screen

First, open the Worksheet (.ipynb file)

Next, split the screen. At the top right, click the picture of one box on top of another box

A screenshot showing the location of the 'split screen button' at the top of the window.

Finally, change the bottom screen to a Terminal. In the bottom screen that opened, click on the words Jupyter on the left side of the screen, then click Change Type, then click Terminal

A screenshot showing Jupyter > Change Type > Terminal menu.

macOS: Preparing your Screen

Make sure you are running Jupyter Lab. This will happen as long as you select JupyterLab (as opposed to Jupyter Notebook) in the launcher screen.

First, open the Worksheet (.ipynb file)

Next, Open a Terminal by

  • Click on the File menu

  • Click New

  • Click Terminal

Finally,

  • Drag the terminal tab into the bottom half of the main work area
  • drop it when the blue highlight appears
Windows: Preparing your Screen

Make sure you are running Jupyter Lab. This will happen if you open Jupyter using the following commands

conda activate sage
jupyter lab

First, open the Worksheet (.ipynb file)

Next, Open a Terminal by

  • Click on the File menu

  • Click New

  • Click Terminal

Finally,

  • Drag the terminal tab into the bottom half of the main work area
  • drop it when the blue highlight appears

Once you have set up your Jupyter Lab interface, you can convert your file into many different output formats using variations of the command quarto render filename.ipynb

Turn a Worksheet into a Word File

Make sure you have two screens open:

  • The Notebook filename.ipynb to be compiled (top of screen)
  • The Terminal (bottom of screen)

Inside the terminal, run the following command. It

quarto render filename.ipynb --to docx
  1. Asks quarto to render the worksheet filename.ipynb
  2. and to convert it to the docx format

To download your file

  • Look at the directory listing
  • You should see filename.docx
  • Right click and download
Set Document Metadata

Other output formats like slides and html require additional parameters.

  1. Create a new cell at the top of your notebook
  2. Change the Type of your cell from Code to Raw

Copy/paste the following into the cell

---
execute:
    echo: false
format:
    html:
        embed-resources: true
        self-contained-math: true
    revealjs:
        embed-resources: true
        self-contained-math: true
--- 

By default, Quarto prints both the contents of your code cells and their output.
echo: false overrides this behavior, printing only the output.
If you want to print the code contents, set echo: true instead.

Turn a Worksheet into a Slideshow

First, complete the steps in "Set Document Metadata"

Then make sure you have two screens open:

  • The Notebook filename.ipynb to be compiled (top of screen)
  • The Terminal (bottom of screen)

Inside the terminal, run the following command. It

quarto render filename.ipynb --to revealjs
Turn a Worksheet into a Webpage

First, complete the steps in "Set Document Metadata"

Then make sure you have two screens open:

  • The Notebook filename.ipynb to be compiled (top of screen)
  • The Terminal (bottom of screen)

Inside the terminal, run the following command. It

quarto render filename.ipynb --to html