SharedUse-Python3-based-SageMath-in-a-CoCalc-project.mdOpen in CoCalc
Authors: Frédéric Chapoton, Samuel Lelièvre
Views : 3
Description: Use Python3-based SageMath in CoCalc

Python3-based SageMath in CoCalc

SageMath is being ported to Python 3.

This is an ongoing effort, and some things might not work in the Python3-based SageMath.

If you already want to try it however, there are several ways to go:

  • just copy a folder to a CoCalc project of yours,
  • or build from source in CoCalc,
  • or build from source elsewhere.

Here we document the first two options.

For the third option, see the "Installation from source" section of the Sage installation manual:

Get started by copying a folder to your project

In CoCalc, just copy this folder to your project:

In a CoCalc terminal, you can then use Python3-based SageMath by typing:

$ sage/sage

which will give you the famous Sage prompt

sage:

at which you can type any Sage command.

To add a "Sage Py3" Jupyter kernel to your project, we follow instructions adapted from the "Custom Jupyter Kernel" page on the CoCalc wiki:

Here, just run the following in a CoCalc terminal

$ cd
$ OLD_KERNEL=/ext/jupyter/kernels/sage-develop
$ NEW_KERNEL=/home/user/.local/share/jupyter/kernels/sage_py3
$ cp -arv $OLD_KERNEL $NEW_KERNEL
$ cd $NEW_KERNEL
$ open kernel.json

and then replace the content of kernel.json by:

{
  "display_name": "Sage Py3",
  "argv": [
    "/home/user/sage/sage",
    "--python",
    "-m",
    "sage.repl.ipython_kernel",
    "--matplotlib=inline",
    "-f",
    "{connection_file}"
  ]
}

Now if you open a Jupyter notebook worksheet (file ending in .ipynb), you can select the "Sage Py3" kernel from the "Kernel > Change kernel" menu.

Or build from source

Instead of copying a ready-to-go Sage folder as above, you might prefer to build from source. Here is how to do that.

In a CoCalc project with internet access and some extra disk space, open a terminal.

If your project has many cores (say 4) and you want to build in parallel, first run the following command (replacing 4 by the number of cores available):

$ export MAKE='make -j4'

Or you could edit your .bashrc, adding the following line to it.

export MAKE='make -j4'

and then either close the terminal and open it again, or run

source /home/user/.bashrc

Once you have set MAKE as above (or skipped that), run:

$ cd
$ mkdir bin
$ git clone https://github.com/sagemath/sage.git
$ cd sage
$ make configure
$ ./configure --with-python=3
$ make build
$ ln -s /home/user/sage/sage /home/user/bin

You can then try:

$ sage --version
$ sage --python --version
$ sage -q   # start Sage in quiet mode (no banner)
sage: print(version())      # SageMath version
sage: print(sys.version)    # Python version

and try your favourite Sage commands, see if they work!

You can also install a "Sage Py3" Jupyter kernel, by following the instructions at the end of the previous section.