Exporting plots on a remote machine without display in script mode?

Update: Starting with Mathematica 11.3, it is no longer necessary to have a running X server. Everything works out of the box.


Some operations require the front end, the most notable example being graphics rendering. On Linux, the front end requires an X server. Note that this does not mean that it requires a connected display.

If you are running interactively in an ssh session, the easiest way to provide an X server is to forward it from your own machine using ssh -X or ssh -Y.

If you are running it non-interactively, as it is usually the case on a cluster, then the typical solution is to install and use Xvfb. This is an X server that does not require a display. I have installed this in the past privately for my user, without root access.

If you are running on a HPC cluster, often the most practical solution is to just not export any graphics. Often (but not always) it is more practical to only export data on the cluster, and visualize it on your own machine, separately.

UPDATE:

Indeed, after installing Xvfb, the following works

xvfb-run MathematicaScript -script test.m

Though interestingly not with Export for mathematica 10.3 (but ok with 11.0 or 9.0).


Sometimes you might need to tell the system directly where the mathematica kernel is situated if it is not connected to the "math" command already (I had to set that up on my macbook manually, too).

Here is a good tutorial on that: http://reference.wolfram.com/language/tutorial/WolframLanguageScripts.html