Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I install OCaml with OPam on windows?

How can I install OCaml with OPam on windows?

like image 917
pinkdolphin Avatar asked Sep 18 '16 16:09

pinkdolphin


People also ask

Where are opam packages installed?

Your libraries are installed to the directory returned by opam var lib , which is by default ~/. opam/<switch>/lib .

How install VS code OCaml?

We recommend you use the plugin called OCaml Platform for OCaml support in VSCode. To install it, first run opam install ocaml-lsp-server from a terminal. Then from the View menu in VSCode select Extensions , then type in OCaml in the search box and this extension will show up: select OCaml Platform from the list.


2 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!

like image 175
Trung Ta Avatar answered Sep 24 '22 13:09

Trung Ta


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

  1. nano .ocamlinit (*add an extra i nano .ocamlint *)
  2. 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.

like image 45
Ash Avatar answered Sep 21 '22 13:09

Ash