OCaml By Examples



let add_one i = i + 1


A library (or set of dune libraries) can be published to an opam repository. For this, an opam package file needs to be created with these fields.
opam-version: "2.0"
name: "my_lib"
version: "0.2.0"
synopsis: "A library to do things"
maintainer: "your_email@gmail.com"
authors: "your_email@gmail.com"
license: "Apache-2.0"
homepage: "https://github.com/mimoo/my_lib"
bug-reports: "your_email@gmail.com"
depends: [
  "ocaml" {>= "4.12.0" & < "4.13"}
  "core" {>= "v0.14.0" & < "v.0.15"}
build: ["dune" "build" "-p" name "-j" jobs]
dev-repo: "git+https://github.com/mimoo/my_lib.git"


The corresponding dune file must have a `public_name` field sharing the same name. This `public_name` field will be used by other dune files to list their dependency on your library, while the `name` will be used by OCaml code that wants to interact with the module. (In theory the `name` field can be anything but you should avoid that.)
 (name my_lib)
 (public_name my_lib)
 (libraries core))


An opam package can be a set of dune libraries. This sub-library will have to be imported as `my_lib.thing` by other dune files, but will be used as `my_lib_thing` by OCaml code.
 (name my_lib_thing)
 (public_name my_lib.thing)
 (libraries core))


Before publishing an opam package you need a license in your repository.
whatever license you prefer


And a README, we're not fools.
# My Lib

That's my cool lib!


You will also need to keep track of a changelog for each new version of your library.
## 0.2.0 (2020-05-18)

New minor version... Nothing should break.

## 0.1.0 (2020-05-14)

Wow, first version!


To publish a package you can install `dune-release`.
$ opam install dune-release
Make sure your package is correctly set up
$ dune-release lint
This will automatically detect the new version (in `CHANGES.md` and your opam package file `my_lib.opam`) and tag the repository accordingly.
$ dune-release tag
Finally, you can use this command to push the tag to your remote repository and create a pull request to the opam repository.
$ dune-release
next: pins