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

Context

I would like to run mathematica scripts on a remote cluster in order to produce some plots. This cluster does not have X11 installed (the job is submitted to a queuing system).

Problem

I have a script file which, say contains

 Print["starting"]; pl= Plot[x,{x,0,1}]; Export["test.eps",pl];


in a test.m file;

But the line command

 math -script test.m


seems to require a display.

A few versions of mathematica ago (<7?) it would work with the option

  pl= Plot[x,{x,0,1},DisplayFunction->Identity];


cf this page. But this trick does not seem to work anymore, as mathematica seems to require a front-end to do the plot, even if it is not displayed in practice.

Question

With Mathematica 10.0 or 11.0 how does one produce plots without display?

Update

The issue is not with finding the Mathematica Kernel. The script runs fine until the lack of display is a problem.

starting
Can't open display "localhost:14.0"

• FWIW this works for me with DISPLAY unset, so it is not directly using the X server. Can you put Print[AbsoluteOptions@pl] in there to see if the issue is with Plot or with Export Jan 30 '17 at 20:47
• DISPLAY is the linux shell variable that has the X server address. Looking at your error I think the problem is you have DISPLAY set to localhost:14.0 when it should not be set at all if there is no server available. (I can reproduce your error if I set DISPLAY to something invalid) Jan 30 '17 at 21:04
• try unsetenv DISPLAY .. It needs to be removed, not just empty. It seems to be Export that is causing the problem by the way. Jan 30 '17 at 21:12
• Ah, i see. math falls back to a default display :0.0 in the event you have no DISPLAY variable set. That is not normal behavior, but at least explains why my fix doesn't work. Jan 30 '17 at 22:20
• It seems that M11.3 no longer requires xvfb, but M11.2 does. Jan 28 '19 at 13:23

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).

• Thanks. It's a pity Wolfram broke this with recent version of Mathematica. Jan 31 '17 at 9:38
• @chris It is not recent. It has been this way since version 6. Jan 31 '17 at 9:38
• To sympathize a bit with @chris: I know it's been a while, but I still consider versions 6 and above as "recent"... :D Jan 31 '17 at 9:40
• Well, version 5 and earlier converted Graphics to PostScript, which was rendered by a separate process (not by the front end). We did not have rotatable 3D graphics or high quality 3D rendering. We also didn't have antialiasing, but that could be fixed by using Ghostscript to render the PostScript. Jan 31 '17 at 9:41
• @chris I'm sorry, I haven't done this in years ... I really don't remember the details. I would also have to start from scratch. I agree that it is a pain to do this. Wikipedia has some hints. Wolfram should provide more documentation on this and preferably even ship Xvfb with Mathematica (if the license allows) along with a method to use it automatically. Jan 31 '17 at 9:46

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

• Thanks for our answer. However the issue is not with finding the kernel. My script runs all right but fails at the display stage. Jan 30 '17 at 20:43
• What exactly do you want to display? In the kernel without the front end I don't think you can run the pl= Plot[x,{x,0,1}] line to get a visual output. But in you minimal code there is also a ";" in the end which prevents the graphical output. But shouldn't you get the .eps export from the kernel which you can open?
– Rico
Jan 30 '17 at 20:46
• Hm i just tested it on my university server and I received the .eps file output with your code just using the kernel via math -script test.m. Are you sure the correct kernel is linked to the "math" function? I tried it with mathematica 10 as well as 11.
– Rico
Jan 30 '17 at 20:59
• No im not exporting DISPLAY. It should not be needed if you just run the kernel and not the GUI of mathematica?!
– Rico
Jan 30 '17 at 21:11