# Precompiling OCaml for Windows

**URL:** <https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212>\
**Category:** Ecosystem\
**Tags:** windows\
**Created:** [January 19, 2023, 10:29pm UTC](https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212 "2023-01-19T22:29:30Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ysalmon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ysalmon/32/4168_2.png) [@ysalmon](https://discuss.ocaml.org/u/ysalmon)\
**Post date:** [January 19, 2023, 10:29pm UTC](https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212/1 "2023-01-19T22:29:30Z")

</div>

Alas, I will need the OCaml compiler on an array of Windows machines. The installation procedure with Diskuv is very long and I am not even sure I would have the sufficient authorisations to run it on said computers.

However, I do not actually need the opam, dune, etc. machinery : just the ocaml REPL and ocamlc.

Is this a good (or at least not disastrous) and feasible idea to install OCaml via Diskuv on one machine, and then drop the compiled compiler and its libs to that array of machines, like a “portable” application ?

---

<div class="post-metadata">

**Author:** ![jbeckford](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jbeckford/32/3027_2.png) [@jbeckford](https://discuss.ocaml.org/u/jbeckford)\
**Post date:** [January 19, 2023, 10:48pm UTC](https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212/2 "2023-01-19T22:48:46Z")

</div>

In principle, that approach would probably work as long as you used the exact same paths on all machines. Additionally, you would need to add `DiskuvOCamlHome` and `DiskuvOCamlVersion` to the environment variables, in addition to two directories prefixed to the PATH environment variables.

But I see two problems that you’d have to overcome:

- how would you have the exact same paths if you don’t have authorizations on other machines? Usually that is difficult without Administrator rights on Windows. Hopefully you have a shared drive that others can access.
- the tricky thing is actually Visual Studio. I don’t think you can just copy that. And you need Admin to install it. (Also, that has licenses around redistribution you have to respect). You should try that first to make sure it works.

Easier way: Just run `winget install Diskuv.OCaml` on the remote PCs. And assuming there is a Windows domain, you can do that across a fleet.

---

<div class="post-metadata">

**Author:** ![ysalmon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ysalmon/32/4168_2.png) [@ysalmon](https://discuss.ocaml.org/u/ysalmon)\
**Post date:** [January 19, 2023, 10:50pm UTC](https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212/3 "2023-01-19T22:50:12Z")

</div>

Why do I need Visual Studio ?

---

<div class="post-metadata">

**Author:** ![jbeckford](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jbeckford/32/3027_2.png) [@jbeckford](https://discuss.ocaml.org/u/jbeckford)\
**Post date:** [January 19, 2023, 10:55pm UTC](https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212/4 "2023-01-19T22:55:56Z")

</div>

DKML uses Visual Studio. That is explained in [GitHub - diskuv/dkml-installer-ocaml: The Windows-friendly distribution of OCaml](https://github.com/diskuv/dkml-installer-ocaml#dkml-installer-for-diskuv-ocaml) (first paragraph), with alternatives if that is not your cup of tea.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [January 19, 2023, 11:09pm UTC](https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212/5 "2023-01-19T23:09:15Z")

</div>

You may want to have a look at the `README.win32.adoc` file in the source distribution, e.g. [this table](https://github.com/ocaml/ocaml/blob/trunk/README.win32.adoc#release-notes-for-the-microsoft-windows-ports-of-ocaml) indicates what you need depending on what you want.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [January 19, 2023, 11:58pm UTC](https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212/6 "2023-01-19T23:58:31Z")

</div>

From that page,

> The native-code compiler (`ocamlopt` ) and static linking of OCaml bytecode with C code (`ocamlc -custom` ) require a Microsoft Visual C/C++ Compiler and the `flexlink` tool (see [above](https://github.com/ocaml/ocaml/blob/trunk/README.win32.adoc#bmflex)).

But @ysalmon you mentioned you need only `ocaml` (REPL) and `ocamlc` (bytecode compiler)? If that’s really all you need, then you might not actually need MSVC++…

---

<div class="post-metadata">

**Author:** ![jbeckford](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jbeckford/32/3027_2.png) [@jbeckford](https://discuss.ocaml.org/u/jbeckford)\
**Post date:** [January 20, 2023, 12:19am UTC](https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212/7 "2023-01-20T00:19:26Z")

</div>

@yawaramin is right; totally missed that you are only talking about the bytecode. (@dbuenzli 's link has good info, even if it looks terribly outdated.) If you stick to copying that it should work without Visual Studio.

And file a ticket at [Issues · diskuv/dkml-installer-ocaml · GitHub](https://github.com/diskuv/dkml-installer-ocaml/issues). For example, [Release 4.14.0-v1.1.0-prerel1 · diskuv/dkml-component-ocamlrun · GitHub](https://github.com/diskuv/dkml-component-ocamlrun/releases/tag/4.14.0-v1.1.0-prerel1) has downloadable `ocamlrun.exe` and the standard library. I think you are just asking for that + `ocaml.exe` + `ocamlc.exe`, probably in `.zip` format for normal Windows use. If you can say how that will be sufficient for you in the ticket, that will be helpful in deciding if a “light” version is warranted.

_Edit_: And you’ll need to set environment variables with that approach. `OCAMLLIB` to `<dir>\lib\ocaml` and add the `<dir>\lib\ocaml\stublibs` to your PATH.

---

<div class="post-metadata">

**Author:** ![ysalmon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ysalmon/32/4168_2.png) [@ysalmon](https://discuss.ocaml.org/u/ysalmon)\
**Post date:** [January 20, 2023, 8:27am UTC](https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212/8 "2023-01-20T08:27:06Z")

</div>

Thank you so much everybody !

I was aware Visual Studio was used by DKML to compile the OCaml compiler ; I was not aware the resulting `ocaml.exe` and `ocamlc.exe` would also need it to run.

The context is a light course where we use OCaml for an introduction to functional programming : our lab sessions will consist in programming some functions in eg. Emacs+Tuareg and testing them on some test examples ; students will never produce a full program and never compile it (the notion of a compiler is already above the scope of the course). Come to think about it, I do not even need a `ocamlc.exe` : if I need to give some `.cmo` files to my students, I can prepare them on my machine, where I have OPAM.

The technical context is :

- in the lab, I have write access to some shared drive but not administrative access to the machines, so I plan to drop an OCaml compiler and a well-configured Emacs+Tuareg somewhere (I think I can eschew setting PATH by configuring Tuareg to run ocaml with the appropriate `-I` parameters) ;
- it is also a good thing if students can get things running on their own machine by unzipping a file rather than several steps of installation/compilation (despite taking this course, they are principally math students and not always tech savvy at all…).

---

<div class="post-metadata">

**Author:** ![ysalmon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ysalmon/32/4168_2.png) [@ysalmon](https://discuss.ocaml.org/u/ysalmon)\
**Post date:** [January 20, 2023, 8:43am UTC](https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212/9 "2023-01-20T08:43:45Z")

</div>

I see that [OCaml for Windows - Installation](https://fdopen.github.io/opam-repository-mingw/installation/), while being announced as discontinued since Aug 2021, still allows to get a 4.14.0 compiler, which is entirely sufficient (and so much better than the 4.0.1 which is currently available at our lab).

Which one can run from Windows by saying `C:\OCaml64\usr\local\bin\ocaml-env.exe exec ocaml` and it seems to find its stdlib.

---

<div class="post-metadata">

**Author:** ![spop](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/spop/32/797_2.png) [@spop](https://discuss.ocaml.org/u/spop)\
**Post date:** [January 20, 2023, 6:10pm UTC](https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212/10 "2023-01-20T18:10:39Z")

</div>

Vous êtes professeur à Rennes semble-t-il (désolé si c’est incorrect …)

Connaissez-vous la [Page web de Jean Mouric](https://jean-mouric.pagesperso-orange.fr/index.html#MacCaml) ?  
Il serait professeur de mathématiques à Rennes et propose depuis de nombreuses années  
des distributions binaires OCaml pour Windows 10 ou 11 x64, GNU/Linux et MacOS.

Et ces distributions vont de caml-light (si,si, j’ai essayé, ça marche) jusqu’à la dernière version OCaml 5.0.0 (sortie il y a 15 jours,pas encore essayé…) La version 4.14.0 est OK.  
Il y a un travail hallucinant sur ce site (pas toujours évident à suivre mais bon !).

Rem:  
Ce programme (WinCaml) est utilisé en classe de MP2I au lycée Clémenceau, Nantes (2022-2023)  
pour l’enseignement d’OCaml.

Vive la Bretagne 🏴󠁦󠁲󠁢󠁲󠁥󠁿

---

<div class="post-metadata">

**Author:** ![jbeckford](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jbeckford/32/3027_2.png) [@jbeckford](https://discuss.ocaml.org/u/jbeckford)\
**Post date:** [January 20, 2023, 7:01pm UTC](https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212/11 "2023-01-20T19:01:42Z")

</div>

The [https://jean-mouric.pagesperso-orange.fr/archives/ocamlmgw64-win10-4140.zip](https://jean-mouric.pagesperso-orange.fr/archives/ocamlmgw64-win10-4140.zip) works well as long as you unzip it directly in your `C:\` drive root directory. And it looks well maintained. It has `graphics` and `zarith` bundled as well. Seems like a great option for your use case!

---

<div class="post-metadata">

**Author:** ![ysalmon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ysalmon/32/4168_2.png) [@ysalmon](https://discuss.ocaml.org/u/ysalmon)\
**Post date:** [January 20, 2023, 7:06pm UTC](https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212/12 "2023-01-20T19:06:20Z")

</div>

Oui, je connais, et le travail de Jean Mouric est ce qui nous a permis de survivre jusqu’à aujourd’hui ! Mais les distrib qu’il publie ont le défaut de ne fonctionner que si on les installe sous C:\. Et moi c’est dans K:\ que je peux écrire.

Anyway, I think I got a working recipe :

- on a Windows machine with sufficient privileges (in my case a VM),
- use [OCaml for Windows - Installation](https://fdopen.github.io/opam-repository-mingw/installation/) or the manual installation instruction there to get a Cygwin with a working opam (note that using the installer to install elsewhere than the default path caused an error pertaining to curl not being available…)
- mount a network share to the path that will be accessible live
- in cygwin, create an opam switch for the desired compiler _in the path of the network share_
- in that switch, install the required compiler and maybe modules (in my case, I installed utop)

Since setting PATH correctly on the machines may or may not be an option, create `.bat` files. In my case, I have a utop.bat with the following

```plaintext
@echo off
set CAML_LD_LIBRARY_PATH=K:\Applications_INFO\OCaml\win10\_opam\lib\stublibs;K:\Applications_INFO\OCaml\win10\_opam\lib\ocaml\stublibs;K:\Applications_INFO\OCaml\win10\_opam\lib\ocaml
set OCAMLLIB=K:\Applications_INFO\OCaml\win10\_opam\lib\ocaml
set OCAML_TOPLEVEL_PATH=K:\Applications_INFO\OCaml\win10\_opam\lib\toplevel

K:\Applications_INFO\OCaml\win10\_opam\bin\utop.exe %*

```

Now I am off configuring Emacs…

---

<div class="post-metadata">

**Author:** ![brab](https://avatars.discourse-cdn.com/v4/letter/b/839c29/32.png) [@brab](https://discuss.ocaml.org/u/brab)\
**Post date:** [January 21, 2023, 11:10am UTC](https://discuss.ocaml.org/t/precompiling-ocaml-for-windows/11212/13 "2023-01-21T11:10:59Z")

</div>

For the record, there is a “for-teachers” channel in the OCaml discord ([OCaml](https://discord.gg/cCYQbqN)). You might also find some help there.
