23

Running

open Graphics;;

in OCaml returns an error, saying it is an unbound module. Running it in terminal (ocaml) returns the same thing.

Does this mean my Graphics Module was somehow not installed with the OCaml package? If so, how can I install the module?

On Fedora.

glennsl
  • 28,186
  • 12
  • 57
  • 75
Secret
  • 3,291
  • 3
  • 32
  • 50

2 Answers2

40

This error also appears often on Mac OS X. With Homebrew this module is disabled by default on installation, so brew install ocaml will not install the Graphics module, probably due to the XQuartz dependency.

If you run brew info ocaml, it will tell you that there's a flag, namely --with-x11, that will "Install with the Graphics module". So to install/reinstall ocaml you'll have to run:

brew install Caskroom/cask/xquartz
brew [re]install ocaml --with-x11

Finally remember to check that the instance of ocaml that is running is the one in /usr/local/Cellar/objective-caml/x.yy.z[_w]/bin, and if it isn't then prepend that url to your PATH environment variable. Also remember to restart your computer after the XQuartz installation.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • 3
    I came here with this exact issue. This is a very complete answer IMO and a joy to read. Thank you! – james-see Sep 22 '16 at 01:38
  • Don’t prepend that path to your `PATH` variable; use `/usr/local/opt/ocaml/bin` instead. This path won’t change on OCaml upgrades. – bfontaine Dec 07 '16 at 18:58
  • I recently installed Ocaml 4.06.0 and utop 2.0.2 on MacOS Sierra 10.12.6, and this didn't work (initially). I believe, but can't prove, that my problem was that my xquartz was out of date (and somehow incompatible). I checked `brew doctor` and cleaned up a few issues (including upgrading xquartz), but I did *not* reboot at first. After much hair-pulling an article prompted me to reboot. Then, completely clean and reinstall ocaml + opam + utop, and `open_graphics` is working. hth someone. – Jim Reesman Dec 09 '17 at 12:17
  • 1
    This approach fails on OSX 10.14.5 since there is no long the option `--with-x11`, any alternatives than compiling the source? – xtt Jun 28 '19 at 04:41
  • The `--with-x11` flag no longer exists. I'm on OSX 10.13.6 – solstice333 Dec 28 '19 at 20:46
20

Graphics module is not ready by default. You need to load it manually. In toplevel:

$ ocaml
       OCaml version blahblah
# #load "graphics.cma";;
# open Graphics;;

or you can specify it at the command line:

$ ocaml graphics.cma
       OCaml version blahblah
# open Graphics;;

I do not know about Fedora but if the above fails, graphics is not really installed in your environment.

camlspotter
  • 8,990
  • 23
  • 27
  • 6
    It returns a "Cannot find graphics.cma" Message. I'm guessing it wasn't installed with the ocaml package. Any tips for me? – Secret Aug 26 '13 at 03:55
  • 1
    Then I suspect Fedora's OCaml binary package does not include graphics module in it. And it was actually an issue: https://bugzilla.redhat.com/show_bug.cgi?id=468506 If I were you I would install OCaml including graphics by myself. – camlspotter Aug 26 '13 at 04:17