Instalation of graphics on Ubuntu 22.04

I am trying to install graphics on my computer so I typing on Terminal

sudo apt install ocaml 
opam install graphics 

then I create with this code :

open Graphics;; 

then I compiled

ocamlc -o TEST 

But I received this message

File "", line 1, characters 5-13: 
1 | open Graphics;; 

Error: Unbound module Graphics 

For information i am using Ubuntu 22.04

thank you for your replies


The compiler doesn’t really know about OPAM so it needs to be told where to find the libraries used by your program (in this case: graphics). Several ways to do it:

  • The old-school way:
$ ocamlc -I $(opam var graphics:lib) graphics.cma -o TEST
  • If you install ocamlfind using OPAM, you can alternatively do:
$ ocamlfind ocamlc -package graphics -linkpkg -o TEST
  • Finally, if you install dune using OPAM, you can also use it to build your project. You will need to create a file called dune-project next to containing
(lang dune 3.8)

and another file called dune containing

 (name main)
 (libraries graphics))

You can then build your program by typing dune build. The built executable will be available at the path _build/default/main.exe.



i install ocaml find and use

$ ocamlfind ocamlc -package graphics -linkpkg -o TEST

It’s doesn’t work i have this message :

ocamlfind: Package `graphics' not found

But the second way using

$ ocamlc -I $(opam var graphics:lib) graphics.cma -o TEST

work perfectly

Thanks for helpin me.

I can add that all added libraries must be linked with something like “nojb” has proposed, excepted the built in OCaml library (OCaml library). The open line doesn’t load any library. It just enable you to use a value from a loaded library without prefixing the module name.

Then, open Graphics won’t load Graphics, but if it is loaded, it will enable you to type open_graph instead of Graphics.open_graph. That’s it.

I tend to prefer prefixing my functions each time and avoid open. The main exception is probably Base or Core since they are designed as a replacement of Stdlib. I should add also Infix modules since they add some operators which would be too clumsy to prefix.