1

I'm trying to get back into programming with OCaml after a long time of not using it, and since I don't have Linux on my current computer, I've been trying to do it through WSL (Windows Subsystem for Linux).

After doing everything as I would have in Linux, whenever I try to compile a piece of code using the Graphics module, I get the following outcomes:

  • If I try to use the open_graph command, I get the following error:

Exception: Graphics.Graphic_failure "Cannot open display ".

I get that exact error if the argument of open_graph is an empty string, or starts with a space. If the string argument is not empty and doesn't start with a space (which, from what I've read, shouldn't be used anyway), the error message includes said argument as follows:

Exception: Graphics.Graphic_failure "Cannot open display [argument of open_graph]".

  • If the code only uses base OCaml commands and Graphics commands that doesn't require an open graph (like rgb), everything compiles normally.

I found this thread where users raised the question of the DISPLAY variable possibly being "wrong". The original poster of said thread has a different issue, but I felt it is relevant, as I was thinking that perhaps using WSL could have made my DISPLAY variable "off", or could require me to use a different value for DISPLAY than I would use on a complete Linux OS - but, to be perfectly honest, I have no idea what that would be.

I tried my best to understand everything I read and to explain my issue clearly, but I only have moderate experience with Linux and programming in general, so I'm sorry if I misunderstood something or if my explanations weren't the clearest. In that case, please let me know, and I will try my best to rectify it.

Cheers!


Example of complete code and exact error message:

#use "topfind";; #require "graphics";; Graphics.open_graph "";;

Error message returned:

Exception: Graphics.Graphic_failure "Cannot open display ".

Vatinas
  • 13
  • 3
  • A simple solution is to install [Debian](https://debian.org/) or [Ubuntu](https://ubuntu.com/) on your laptop. Most Ocaml developers are running some Linux distribution... And you could also be interested by [RefPerSys](http://refpersys.org/). Then contact me by email to `basile@starynkevitch.net` near Paris in France – Basile Starynkevitch Dec 05 '21 at 17:24

1 Answers1

1

According to https://github.com/ocaml/graphics/issues/21 you need to install a X11 server in your WSL environment.

octachron
  • 17,178
  • 2
  • 16
  • 23
  • 2
    Indeed, that was it! Thanks a lot for the help. In case anyone from the future is reading this thread and has a similar issue and, like me, doesn't know how to install such a server, [here](https://www.youtube.com/watch?v=4SZXbl9KVsw) is a helpful video explaining how to do it. – Vatinas Dec 06 '21 at 15:19
  • It works, thanks! But make sure to write in bash `export DISPLAY=172.28.240.1:0:0` and `export LIBGL_ALWAYS_INDIRECT=1` every time before running the OCaml program with the command `ocamlrun ` or `./`. Or edit the `~/.bashrc` file by adding the lines `export DISPLAY=$(ip route list default | awk '{print $3}'):0` and `export LIBGL_ALWAYS_INDIRECT=1` to it, as explained here https://stackoverflow.com/questions/61110603/how-to-set-up-working-x11-forwarding-on-wsl2 – Jeyrome Sapin Feb 09 '22 at 13:10