28

I could use Python Kernel with Jupyter. I am looking for a way to use sagemath inside Jupyter.I couldnt see a way for installing it. How to do that?

user567879
  • 5,139
  • 20
  • 71
  • 105

4 Answers4

38

I have just installed SageMath kernel on my existing Jupyter installation. It's super easy.

$ sudo jupyter kernelspec install ./SageMath/local/share/jupyter/kernels/sagemath

Where SageMath is your root sagemath directory. Hope this will help someone.

If you don't have sudo rights you won't be able to install system-wide, but you can still install for yourself using the --user flag:

$ jupyter kernelspec install --user ./SageMath/local/share/jupyter/kernels/sagemath
Samuel Lelièvre
  • 3,212
  • 1
  • 14
  • 27
Layek
  • 481
  • 4
  • 4
  • 11
    If needed, additionally edit the `kernel.json` file to make the SageMath Jupyter kernel aware of SageMath's location, by adding `"env":{"SAGE_ROOT":"/path/to/sage"}` at the end of the dictionary in `kernel.json`. You will find the location of `kernel.json` by executing `jupyter kernelspec list`. (Replace `/path/to/sage` by the actual path to the `sage` executable.) – Samuel Lelièvre Jan 19 '17 at 13:48
  • 1
    Thanks for the information. Also, sage executable should be in the PATH and the easiest way to do this is creating a symlink in /usr/local/bin/sage – Layek Jan 20 '17 at 14:34
  • I've had success with this method with anaconda jupyter. Here's one more tip: to get jsmol and threejs working in this SageMath kernel, be sure to create symlinks to SageMath/local/share/jsmol and SageMath/local/share/threejs as /anaconda/share/jupyter/nbextensions/jsmol and /anaconda/share/jupyter/nbextensions/threejs respectively (change the path to jupyter/nbextensions as necessary for your installation of jupyter). – j.c. Mar 02 '18 at 19:44
  • I had success with it. I had to set `"env":{"SAGE_ROOT":"/path/to/sage"}` in `kernel.json` as described in another answer. Make sure `/path/to/sage` is the actual root path (for me on macOS this was `/Applications/SageMath-9.1.app/Contents/Resources/sage`) not the path to the actual executable. – Wizard Oct 24 '20 at 17:33
11

The answer suggesting

$ jupyter kernelspec install --user <path to SageMath/local/share/jupyter/kernels/sagemath>

works only if you provide the environment variable SAGE_ROOT in the kernel spec. This requires adding an "env" item to the kernel.json. The line that must be added is:

"env":{"SAGE_ROOT":"<Path to sage root>"}

An example specific to an installed mac app for version 8.7 is:

"env":{"SAGE_ROOT":"/Applications/SageMath-8.7.app/Contents/Resources/sage"}

See this question for general information and a complete example.

Jonathan Gutow
  • 305
  • 2
  • 8
  • 2
    For conda, I also had to set `SAGE_LOCAL` to the same path as `SAGE_ROOT`. But it works now. – Mike Nov 19 '19 at 19:54
7

You are going about it backwards; Sage includes the Jupyter notebook inside of it, and you can use it with that kernel that way. (As well as others.) Use

$ sage --notebook ipython

I believe. See also here.

(I think there are also some people who have had success redirecting their Jupyter to "see" the Sage kernel by editing some file or configuration. On Arch Linux this is apparently supported ... ?)

kcrisman
  • 4,374
  • 20
  • 41
  • 2
    But that sagemath notebook does not have features to publish. But Jupyter notebook does. – user567879 Sep 02 '16 at 17:07
  • 1
    I'm not talking about the sagenb notebook, but the Jupyter notebook, which you should be able to invoke as in my edited post... – kcrisman Sep 02 '16 at 19:29
  • 1
    Arch Linux prompted me to ask this question. Thanks it worked. – user567879 Sep 03 '16 at 04:19
  • 7
    This seems contrary to the model other Jupyter kernels follow. I already have an instance of Jupyter installed and configured, I just want to add sage as an extra kernel in addition to my regular Python/Julia/R kernels. – Asad Saeeduddin Nov 28 '16 at 01:25
  • That may be contrary, but it's simply the way it must be without a lot of extra effort, because of how Sage's Python is put in place to make sure things work properly. – kcrisman Nov 28 '16 at 02:34
  • 2
    Let me try to reconcile the last two comments: the way things are now, SageMath installs its own Python and Jupyter. So kcrisman says you can install additional kernels in that Jupyter installation, and you don't need to install another Jupyter. The other answer (by Layek) points out that if you have another Jupyter installed in your system, you can also make it aware of the SageMath Jupyter kernel. You could do both, making your system-wide Jupyter aware of the SageMath Jupyter kernel, and making SageMath's Jupyter aware your other kernels! – Samuel Lelièvre Apr 12 '17 at 16:43
  • 2
    On Ubuntu 20.04, installing sagemath as a package seems to allow to concile both perspectives: you can install sagemath without the 'recommended' dependency sagemath-jupyter ( sudo apt install sagemath sagemath-jupyter- ). In this case the jupyter kernel is not installed nor jupyter. However, if it occurs that the jupyter package is already installed and you do not exclude sagemath-jupyter in the above apt command, no new jupyter will be installed, and the sage kernel will seamlessly be added to the installed jupyter. @moderator, please erase previous comment. – gcousin Aug 14 '21 at 00:05
1

I know this is an old question, but I stumbled onto it and ended up making a docker container to accomplish this for anyone who is interested.

https://hub.docker.com/r/sharptrick/sage-notebook

The dockerfile may be of interest as it has explicit instructions which install the kernel to the official "jupyter/minimal-notebook".

https://github.com/sharpTrick/sage-notebook/blob/master/Dockerfile

patrick
  • 194
  • 1
  • 3