Very basic dune question

Hi, sorry for the basic question but I’m having a very hard time finding my way through the dune documentation. From time to time I write a one-file binary to process corpus data for scientific purposes. This time I decided to compile it with dune so it’s easier to make it grow later, and I would like dune to take care of making it available to me in an easy way, without having to care about what’s in _build.

Ideally, I would want a way of making dune put the executable, without a .exe extension, in an arbitrary place on my system (such as /home/myname/.local/bin/). Otherwise I would settle for having it in the root directory of my project so I can conveniently put it where I want. Can these things be done?

I had a look at the rule stanza but it seems to require a file as target name (there is no sense in which this applies here). I also tried the install stanza with misc as section but then dune complains about the lack of .opam file (at any rate the documentation for misc does not clearly say what it does, it looks like you’re not supposed to use it).

Here’s a small gist I’ve made:

You’ve got a hello-world program, a dune file:

 (public_name my_program))

As well as a dune-project file (which is auto-generated, but it’s a good idea to commit it), and an empty my_program.opam file. And the last one is the key to making an installable package. That’s the convention, for dune to know that the public artifacts are installable.

With this you can do dune build && dune install and it will install it by default into the current opam switch. However you can pass --prefix=./local to install it into an alternative directory.

In the gist I’ve included an example Makefile that allows you to do the standard-ish make PREFIX=./local install.


Thanks a lot for your answer. So it looks like what I was missing was the empty .opam file.

I’m still not super satisfied with the result because it installs some metadata I don’t care about under /lib. Is there a way to not have that?

(Other than that, I have to say, this looks like a lot of “bureaucracy” to me. I would have hoped it was possible to use dune while ignoring opam and the notion of package entirely.)

1 Like

Don’t know how to ignore the lib metadata, unfortunately. I agree with your critique, the need for an .opam file tripped me up as well the first time I needed it. But I must say that things are improving. It used to be that you had to run dune build @install and that was a gotcha for many (including me), but now you can just dune build.