How can I install OCaml with OPam on windows?
-
1OCaml has many builds for Windows. Cygwin, MinGW, MSVC and now even for Bash on Windows. Each has its own glitches. You have to specify which one you want to use, and what kind of problems you actually have. – camlspotter Sep 19 '16 at 01:11
-
1The question, even though poorly formulated, is interesting, because I (and I know I'm not the only one) wasn't able to get any of the above working properly. I'm interested in having feedback from users who managed to get it right. – Richard-Degenne Sep 23 '16 at 07:33
-
It appears no one ever invested the time and effort to do a real native port of (any?) ML language to the windows platform. Only work arounds using unix-emulation environments seem to be available. Now that F# (also a language from the ML family) is gaining a wider user base it would still be awesome to be able to "go native", maybe for performance reasons. As there is also always the "Batteries" problem, I fancy it would be even cooler if a native windows ML/OCaml port had batteries following the spirit of .NET Framework, so cross porting would become easy. – BitTickler Apr 04 '18 at 04:06
3 Answers
I have been able to setup OCaml 4.03.0 in Windows 10, using the Opam package manager, by following the tutorial from this website: http://fdopen.github.io/opam-repository-mingw/. Here are the detailed steps that I did:
Install OCaml, Opam and Cygwin:
Download the installation package from this link: http://fdopen.github.io/opam-repository-mingw/installation/. There are both the 32 bit and 64 bit version, but I suggest to install the OCaml 64bit .
When running the graphical installation file, it will automatically install OCaml 4.02.3, Cygwin, Opam for you.
After installing, a shortcut for OCaml and Cygwin will be created on your Windows’ desktop.
Now, open the Cygwin terminal from the shortcut in your Windows’ desktop and do the followings:
Install some required libraries for Opam:
opam install depext
opam install depext-cygwinports
Upgrade your OCaml to 4.03.0 by using Opam:
opam switch 4.03.0+mingw64
eval `opam config env`
There you have OCaml 4.03.0 on Windows!

- 1,582
- 1
- 16
- 25
-
1I am also fond of that Windows installer, since it's often the shortest route to a working OPAM on Windows. It still requires Cygwin though, which may keep some people away from it. Maybe someday a version based on the Windows Subsystem for Linux will be available, but currently fdopen's repository is the one that worked best for me. – anol Sep 26 '16 at 13:03
-
Yeah, this repository comes as a relief to me. I used to struggle a lot to install the newest compiler as well as other libraries of OCaml in Windows. – Trung Ta Sep 26 '16 at 15:52
-
The graphical installer failed for me, saying curl failed. I was able to get it going after the Cygwin setup by running `echo insecure >> ~/.curlrc` . – John Vandenberg Oct 27 '21 at 11:18
I was able to run OCaml and Opam in Windows 10 using Windows Subsystem for Linux (WSL) without any problem.
Here's a very detail description and instruction OCaml on Windows: The easy way
Note: 2 typos in the article OCaml on Windows: The easy way as of this post, when you come across these two instructions please use the following
nano .ocamlinit
(*add an extra inano .ocamlint
*)open Core.Std;;
(*remove # from#open Core.Std;;
*)
Hope this helps someone!
**The initial link was removed, so here's the content
OCaml on Windows: The easy way
There have been a number of ways to run OCaml on Windows in the past, and there is an extensive list here. However, they all have downsides, whether that involves depending on someone else to update installer binaries, or having to deal with installing and managing Cygwin just to run OCaml.
Fortunately, with the release of Windows Subsystem for Linux (WSL), it’s possible to use the standard Ubuntu OCaml/OPAM easily from within windows, and integrate it with a windows GUI code editor. This guide assumes that you already have WSL set up - if not come back after setting it up by following this guide.
Given how fundamental modules such as Core by Jane Street are to doing any real work with OCaml we’re going to install the OPAM package manager and Core along with just the compiler. The end result is exactly the set up you need to begin the excellent Real World OCaml guide.
Firstly, we need to download and install ocaml and opam we need to add a personal package archive (ppa) to apt-get because the official Ubuntu repos have occasional issues. The code below adds this private repo to apt-get.
sudo add-apt-repository ppa:avsm/ppa
sudo apt-get update
Then just use apt-get to install ocaml, opam and m4 (a macro processor also required for the setup to work properly)
sudo apt-get install ocaml opam m4
The next step is to set up opam. Start with opam init, which will ask you to give it permission to update your .ocamlinit and .profile files. You can simply answer yes to both to put the changes through.
This lets you run the basic REPL loop with the ocaml command. However, a couple of extra bits and pieces are needed to get to the start of the Real World OCaml guide. We install these extras through opam with the command:
opam install core utop
Normally we’d now add the environmental variables to run the environment with the command:
eval 'opam config env'
Which should add the variables you need as explained here and here. However, when I tried it on WSL it didn’t work, I think as a side effect of the difference between the way windows and linux handles new lines. You can see this for yourself by running the command and then printenv to see all of the environmental variables - none of the variables have been set.
Instead if we use this command:
eval "$(opam config env)"
Then all of the commands are passed to ‘eval’ as a block, and so the whole thing evaluates cleanly, and if you run printenv you should see all of the required environmental variables.
The only thing that remains is to set up OCaml with the custom utop by editing the .ocamlinit file, which will be in your home directory.
Side Note: If you’re new to bash you might be confused that it doesn’t appear if you run ls in your home directory. This is because of the . in front of the filename which hides it by default. If you run ls -a
you will be able to see it.
Open this file with whatever your favourite text editor is, e.g. nano .ocamlinit
, and then add these lines. This is some OCaml code, which will execute on initialisation, i.e. every time you start up the OCaml environment.
#use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;
open Core.Std;;
Everything is now doing what it’s supposed to do - if you run ocaml you will get utop with the Core module loaded and be ready to start working through the excellent Real World OCaml guide.
However, there is one last wrinkle. Setting environmental variables this way is temporary - if you exit WSL and then restart it with bash and then rerun printenv you will see that you have lost the environmental variables, and the ocaml command will now fail - unable to find the path for topfind because the environmental variables have gone.
To fix this we can add the command eval "$(opam config env)"
to the bottom of your .bashrc file in your home folder, which is run by bash every time you log in. This ensures that it runs automatically, so that you can just login and run ocaml to have a fully functional ocaml environment set up. If you switch compiler through the opam switch command, you will need to either re-run the command or restart the shell with exit and then bash to push the changes to the environment variables and update the version used by OCaml.
You’re now ready to start Real World OCaml - enjoy! This gives you the REPL loop under bash, which is good for learning. However, for more serious programs you need to write an .ml file - the next part (coming soon) covers building using the bash OCaml environment from a GUI code editor in windows, such as Visual Studio Code.

- 657
- 9
- 16
-
3The provided links are dead, could you consider posting the steps here instead? – LuqJensen Feb 20 '18 at 10:24
-
1I used the instruction steps to install but did not make a copy of it... if I find it I'll try to post it – Ash Mar 05 '18 at 19:32
-
2This link works: https://web.archive.org/web/20170827013714/http://themargin.io/2017/02/02/OCaml_on_win/ – Lucian Wischik Apr 29 '18 at 01:22
Windows 10 CygWin64 standard C toolchain OCaml 4.02.3 + BuckleScript-1 ** WebAssembly spec/interpreter compiling from source OPam
install CygWin 64-bit setup-x86_64.exe
- choose your path (in the following abbreviated with cwd), everything else only default or next
- move the installer to cwd
install required packages for standard C toolchain from windows cmd prompt
cwd>setup-x86_64.exe -q -P wget -P gcc-g++ -P make -P diffutils -P libmpfr-devel -P libgmp-devel -P libmpc-devel
edit cwd/home\username\.bash_profile
, add
export SHELLOPTS
set -o igncr
start via Icon Cygwin64 Terminal
choose your path for installing the BuckleScript git (in the following abbreviated with bwd)
cd /cygdrive/bwd
git config --global core.eol lf
git config --global core.autocrlf false
git clone --recursive https://github.com/bloomberg/bucklescript
(cloning to /cygdrive/bwd/bucklescript
)
edit /cygdrive/bwd/bucklescript/.gittattribute
, add
* text=auto
choose your path for OCaml compiler to build (in the following abbreviated with owd)
configure and make
cd /cygdrive/owd/bucklescript/ocaml
./configure -prefix /owd
make world.opt
make install
install OPam
create switch (open issue, wait for answer)
compile bsc.exe (open issue, wait for answer)
install WebAssembly spec interpreter (WASM WAST switching)
issue WebAssembly/spec/interpreter/1052
... to be completed!

- 316
- 3
- 13