An Experimental Port of SageMath to Browser via WebAssembly (v86)

246 views
Skip to first unread message

Yanshu Wang

unread,
Feb 5, 2026, 11:00:24 PMFeb 5
to sage-devel
Hello.

I am happy to share my experimental port of SageMath to Browser in client side.

Try it here:


Screenshot on Chrome:

chrome.png

The drawback is that it will download 1.5GB memory state file. (It can use these cache later.) And the computation is quite slow. You may always open the browser console to see its downloading status.

This is a screenshot of doing integration:

integration.png
Source code and build guidance:


If you just want to host it or run a local server, simply clone this repository and upload to the static site hosting provider or run 'make run'.

Yanshu Wang

Dima Pasechnik

unread,
Feb 6, 2026, 8:02:36 AMFeb 6
to sage-...@googlegroups.com
in Firefox, it works, although the startup is very slow, it takes several minutes.
Then it's not too bad.
Is there a reason for using Sage 9.5, which is quite old?

Dima

--
You received this message because you are subscribed to the Google Groups "sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/sage-devel/09feb2d3-91c7-423a-aefa-07acaaac755cn%40googlegroups.com.

Yanshu Wang

unread,
Feb 6, 2026, 8:49:27 AMFeb 6
to sage-devel
Hello Dima.

Thanks for your feedback.

SageMath-in-Browser uses the v86 emulator to run Debian-12 with SageMath installed via apt. Currently, v86 only supports emulating the x86 architecture. For SageMath installation methods on Linux, as far as I know, only the Debian apt repository provides the prebuilt binary for the x86 architecture. (Conda-forge does not provide prebuilt binary for 32-bit x86 either, as I found.) So, the version is Sage 9.5, as provided in Debian-12 repository. You can see the Dockerfile used during the build process for more details.

I can try to adjust the Dockerfile and build SageMath from source. But that could be a bit more challenging.

Yanshu Wang

Georgi Guninski

unread,
Feb 7, 2026, 1:19:34 AMFeb 7
to sage-...@googlegroups.com
Nice :)
At the cost of performance, you get the security of not allowing
remote native code execution (like `system("rm -rf /ANYTHING")`) on
the server, right?

Yanshu Wang

unread,
Feb 7, 2026, 2:01:54 AMFeb 7
to sage-devel
Yes, you are right.

I tried to use Nix to install Sage 10.7 on x86 architecture and port to web in the same manner. But it fails. What is worse is that I heard Nix is dropping support for 32-bit platform and prebuilt pacakges (link). If anyone knows an easy method to install SageMath on x86 linux, please tell me and that could help me speed up the progress a lot.

Another benefit for WebAssembly port of SageMath is that it is cross platform and can be easily ported to Android/iOS/Windows using tools like capacitor or cordova or electron.

Yanshu Wang

Georgi Guninski

unread,
Feb 7, 2026, 4:36:12 AMFeb 7
to sage-...@googlegroups.com
If you need a set of static files served from a web server, then you
can install some 32 bit i386 distro in a virtual machine (very easy if
you find the distro) and build 32 bit sage there. Probably the newer
the distro, the easier is to build sage.

parisse

unread,
Feb 11, 2026, 7:42:18 AM (11 days ago) Feb 11
to sage-devel
Fascinating! 
What is the expected performance penalty? From a few tests, I'd say it's between 1 and 2 orders of magnitude slower than sage native code (25* to 50*) and 1 order of magnitude slower than Giac compiled to emscripten (https://www-fourier.univ-grenoble-alpes.fr/~parisse/xcas.html), once JIT compilation steps have been done (before that, some initial computations are very slow). Not surprising considering the fact that i386 native code must be run in the browser.

Yanshu Wang

unread,
Feb 13, 2026, 11:56:46 PM (8 days ago) Feb 13
to sage-devel
Hello.
I don't know the expected performance penalty. v86 has a profiling functionality (see https://github.com/copy/v86/blob/master/docs/profiling.md). I can try to measure that if I have time. v86 is x86-to-wasm JIT, though.
Also poor internet connection can affect the performance. v86 will load binary files on demand. In the folder images/debian-9p-rootfs-flat, you can see all the files in the rootfs of the docker used in the building process are extracted and compressed with name hashed. v86 will download the corresponding files when you launch a command that uses them. You can open the developer console and try typing factor(400), you will see it downloads some bin.zst files.

Update:
I managed to compile sage 10.8 to i386 target. Now SageMath-in-Browser is using Sage 10.8. I move the built artifact to gh-pages branch and only stores sources in the main branch so that cloning this project will be faster.
I compile sage using the classical build method (see build script), this seems to be an editable-install. I cannot remove the cloned source folder even after 'make install', otherwise some links are broken and sage will not launch. How to do non-editable install using the classical build method? That can reduce the size of the rootfs.
I have not tried compiling sage using meson and conda. I guess that would be more difficult for i386 target since many packages in conda-forge do not support i386 architecture.
sage10.8-chrome.png
You can even type 'exit' and go to the directory /root/build/sage (where I cloned sage to) to run doctest:
doctest.png

Georgi Guninski

unread,
Feb 14, 2026, 3:55:39 AM (8 days ago) Feb 14
to sage-...@googlegroups.com
> I don't know the expected performance penalty.

To measure the time, create `file.sage` with fast code.
In bash run: time sage file.sage # for native sage
and: time emulated_sage file.sage #for JS sage

To benchmark inside sage, one possibility is:
sage: timeit("code")

Yanshu Wang

unread,
Feb 14, 2026, 7:36:39 AM (8 days ago) Feb 14
to sage-devel
SageMath-in-Browser is about 2 orders of magnitude slower than native sage code.
I ran the following bench mark:
'from sage.misc.benchmark import *'
'_ = benchmark()'

Output of SageMath-in-Browser:
benchmark.png
Output of native sage:
bench_native.png

parisse

unread,
Feb 14, 2026, 9:28:10 AM (8 days ago) Feb 14
to sage-devel
Thanks for the confirmation.  
It’s a fascinating technical milestone, but if we are looking to resources conservation,  this is clearly not the right direction...

Tobia...@gmx.de

unread,
Feb 14, 2026, 1:09:54 PM (8 days ago) Feb 14
to sage-devel
This is cool! Thanks for your work.

Just as shot in the dark: perhaps the 32-bit version of archlinux allows to install all up-to-date sage dependencies. If that's the case, you could just build sage using `pip install .`, which is then non-editable.

@others: what do you think about adding this to the website as "Try SageMath", so that potential users can see if they like sage enough to install it on their PC?

Tobia...@gmx.de

unread,
Feb 14, 2026, 1:12:16 PM (8 days ago) Feb 14
to sage-devel
Alternatively, one could try to see if https://github.com/container2wasm/container2wasm works to get a 64bit-based version.

Dima Pasechnik

unread,
Feb 14, 2026, 1:21:54 PM (8 days ago) Feb 14
to sage-...@googlegroups.com
On Sat, Feb 14, 2026 at 12:09 PM 'Tobia...@gmx.de' via sage-devel <sage-...@googlegroups.com> wrote:
This is cool! Thanks for your work.

Just as shot in the dark: perhaps the 32-bit version of archlinux allows to install all up-to-date sage dependencies. If that's the case, you could just build sage using `pip install .`, which is then non-editable.

@others: what do you think about adding this to the website as "Try SageMath", so that potential users can see if they like sage enough to install it on their PC?

IMHO it's too slow to be a good ad for SageMath at the moment.

By the way, WebAssembly-built sympy, https://www.sympy.org/en/shell.html, seems to be considerably faster.
I don't know why. 

On Saturday, February 14, 2026 at 3:28:10 PM UTC+1 parisse wrote:
Thanks for the confirmation.  
It’s a fascinating technical milestone, but if we are looking to resources conservation,  this is clearly not the right direction...

On Saturday, February 14, 2026 at 1:36:39 PM UTC+1 wangyen...@gmail.com wrote:
SageMath-in-Browser is about 2 orders of magnitude slower than native sage code.
I ran the following bench mark:
'from sage.misc.benchmark import *'
'_ = benchmark()'

Output of SageMath-in-Browser:
benchmark.png
Output of native sage:
bench_native.png
On Saturday, February 14, 2026 at 4:55:39 PM UTC+8 Georgi Guninski wrote:
> I don't know the expected performance penalty.

To measure the time, create `file.sage` with fast code.
In bash run: time sage file.sage # for native sage
and: time emulated_sage file.sage #for JS sage

To benchmark inside sage, one possibility is:
sage: timeit("code")

--
You received this message because you are subscribed to the Google Groups "sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.

Yanshu Wang

unread,
Feb 14, 2026, 8:22:32 PM (7 days ago) Feb 14
to sage-devel
> Alternatively, one could try to see if https://github.com/container2wasm/container2wasm works to get a 64bit-based version.

Unfortunately, container2wasm will not work for image with size greater than 2GB (like SageMath). See this discussion (https://github.com/container2wasm/container2wasm/issues/230). Also 64-bit wasm can be even slower than 32-bit.

> By the way, WebAssembly-built sympy, https://www.sympy.org/en/shell.html, seems to be considerably faster.

I think this is because WebAssembly-built sympy uses pyodide, which is compiled to wasm directly, so it runs almost natively in your CPU. However, SageMath-in-Browser runs code in the CPU emulated by v86. I think in order to achieve the same performance as WebAssembly-built sympy, SageMath must be compiled directly to wasm using tools like emscripten. But that is far more challenging than the current SageMath-in-Browser approach.

Yanshu Wang

unread,
Feb 15, 2026, 8:52:20 AM (7 days ago) Feb 15
to sage-devel
As a proof of concept and an application of this WebAssembly port, I create a cordova/electron offline wrapper of SageMath-in-Browser for Windows x64.

You can download it from google drive: SageMath-Cordova-Wrapper-0.1.0-win-x64.zip or download the split zipped files from github release.

This should be the easiest way to install sagemath on windows: just unzip it and you can launch sagemath by executing "SageMath-Cordova-Wrapper.exe". No WSL, no
Administrator privileges needed and completely portable. However, similar to SageMath-in-Browser, its performance is worse than a native installation.

Souce:

parisse

unread,
Feb 15, 2026, 11:47:36 AM (7 days ago) Feb 15
to sage-devel
On Sunday, February 15, 2026 at 2:22:32 AM UTC+1 wangyen...@gmail.com wrote:
I think this is because WebAssembly-built sympy uses pyodide, which is compiled to wasm directly, so it runs almost natively in your CPU. However, SageMath-in-Browser runs code in the CPU emulated by v86. I think in order to achieve the same performance as WebAssembly-built sympy, SageMath must be compiled directly to wasm using tools like emscripten. But that is far more challenging than the current SageMath-in-Browser approach.
 
CPython can be compiled to wasm by emscripten, therefore you get an acceptable perfomance penalty. For example inverting a 50x50 matrix with integer coefficients in -99..99 takes about 3 seconds on my laptop with sympy live, vs about 1.3 second (native python3 interpreter) vs 0.01s (Giac/Xcas compiled to wasm) vs 0.004s (sage native) or 0.0012s (Giac/Xcas native). But sympy remains Python interpreted code and does not include advanced maths algorithms, so it will be slow except for small inputs.
Reply all
Reply to author
Forward
0 new messages