ocamlocaml-dune

How to install an executable with dune?


I am building my OCaml project with the following dune file.

(executable
 (name myapp)
 (libraries unix))

How to modify it so that when I run dune build (or many dune install) the executable is copied (without the .exe extension) into the folder of my choice (e.g., /usr/local/bin).


Solution

  • Your dune file can be given an (install) stanza to install your executable with your chosen name in a directory of your choice, such as ~/bin or ~/.local/bin: see https://dune.readthedocs.io/en/stable/dune-files.html#install.

    However, you will normally need to be root in order to install your executable in /usr/local/bin. If, like me, you have dune installed in an opam switch with user ownership then dune cannot do that for you in a way which I find satisfactory. There may be better ways to do it but in these circumstances I include a Makefile which has a build target which calls up dune build --profile release as user, and a PHONY install target which is intended to be called up as root and installs the executable in /usr/local/bin with the correct permissions, and which can give the binary any name it wants.