# Where does #load look for files?

**URL:** <https://discuss.ocaml.org/t/where-does-load-look-for-files/8400>\
**Category:** Community\
**Created:** [September 1, 2021, 3:34am UTC](https://discuss.ocaml.org/t/where-does-load-look-for-files/8400 "2021-09-01T03:34:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yurivict](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yurivict/32/3178_2.png) [@yurivict](https://discuss.ocaml.org/u/yurivict)\
**Post date:** [September 1, 2021, 3:34am UTC](https://discuss.ocaml.org/t/where-does-load-look-for-files/8400/1 "2021-09-01T03:34:41Z")

</div>

On my system `#load "nums.cma";;` finds `/usr/local/lib/ocaml/nums.cma` but `#load "zarith.cma";;` doesn’t find `/usr/local/lib/ocaml/site-lib/zarith/zarith.cma`.

What is the algorithm that `#load "xx"` uses to locate the requested file? I couldn’t find the `#load` documentation in the docs on [OCaml - The OCaml Manual](https://www.ocaml.org/manual/manual067.html) It isn’t mentioned under keywords.

`$(prefix)/lib/ocaml/site-lib/$(package)/` is supposed to be a place where individual packages are installed. Where is this configured?

(I am only interested in OS-level camel packages, not OPAM at this point.)

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [September 1, 2021, 4:01am UTC](https://discuss.ocaml.org/t/where-does-load-look-for-files/8400/2 "2021-09-01T04:01:48Z")

</div>

There’s a search-path. You can add to it with `#directory` and there’s a cmdline option for it. I don’t recall how to dump the contents of the current path, but I’ll bet there’s a way.

[https://ocaml.org/manual/toplevel.html](https://ocaml.org/manual/toplevel.html)

---

<div class="post-metadata">

**Author:** ![yurivict](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yurivict/32/3178_2.png) [@yurivict](https://discuss.ocaml.org/u/yurivict)\
**Post date:** [September 1, 2021, 4:57am UTC](https://discuss.ocaml.org/t/where-does-load-look-for-files/8400/3 "2021-09-01T04:57:27Z")

</div>

It mentions 4 steps how `#load` searches for files. None of them involve environment variables.

OPAM puts files under ~/.opam and sets environment variables. Either this documentation is incomplete, or otherwise how are OPAM modules found under ~/.opam ?

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [September 1, 2021, 5:14am UTC](https://discuss.ocaml.org/t/where-does-load-look-for-files/8400/4 "2021-09-01T05:14:28Z")

</div>

It’s late, and I don’t have that information at my fingertips. If I wanted to find it, I would search the sources of the toplevel (in the ocaml source distribution) for the `#directory` directive and find the variable that it manipulates. I’m betting it wouldn’t be too hard to find.

I would also look at the findlib sources and see which paths are managed there. And it might give an indication of which variables in the toplevel implementation are relevant, too.

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [September 1, 2021, 8:52am UTC](https://discuss.ocaml.org/t/where-does-load-look-for-files/8400/5 "2021-09-01T08:52:01Z")

</div>

The search path for toplevel directives is described in [OCaml - The toplevel system or REPL (ocaml)](https://www.ocaml.org/manual/toplevel.html#s%3Atoplevel-directives:)

1. In script mode, the directory containing the script currently executing; in interactive mode, the current working directory.
2. Directories added with the #directory directive.
3. Directories given on the command line with -I options.
4. The standard library directory.

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [September 1, 2021, 9:26am UTC](https://discuss.ocaml.org/t/where-does-load-look-for-files/8400/6 "2021-09-01T09:26:37Z")

</div>

> [@octachron](#):
>
> 1. Directories given on the command line with -I options.
> 2. The standard library directory.

I believe that between 3. and 4. the directories listed in the `OCAMLTOP_INCLUDE_PATH` variable are considered.

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [September 1, 2021, 9:38am UTC](https://discuss.ocaml.org/t/where-does-load-look-for-files/8400/7 "2021-09-01T09:38:51Z")

</div>

More precisely:

> OCAMLTOP\_INCLUDE\_PATH  
> Additional directories to search for compiled object code files (.cmi, .cmo and .cma). The specified directories are considered from left to right, after the include directories specified on the command line via -I have been searched. Available since OCaml 4.08.

---

<div class="post-metadata">

**Author:** ![yurivict](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yurivict/32/3178_2.png) [@yurivict](https://discuss.ocaml.org/u/yurivict)\
**Post date:** [September 1, 2021, 10:12am UTC](https://discuss.ocaml.org/t/where-does-load-look-for-files/8400/8 "2021-09-01T10:12:07Z")

</div>

Thank you for this info!

I am working on the FreeBSD OCaml ports. Each package installs into `$(prefix)/lib/ocaml/site-lib/$(package)/`.

ocaml fails to find .cma files in these per-package directories. This system was set up for a previous version of ocaml. Now with ocaml-4.12 it doesn’t work. Do you know how to make ocaml look into such per-package directories?

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [September 1, 2021, 2:40pm UTC](https://discuss.ocaml.org/t/where-does-load-look-for-files/8400/9 "2021-09-01T14:40:02Z")

</div>

I am not familiar with the FreeBSD setup. However, in a standard OCaml environment, I would _not_ expect `#load` to find package-specific by itself. Finding and loading package-specific `cma` is generally done with `ocamlfind` and `#require` (in the REPL).
