Environnement for OCaml


This page with minor modifications is translated from French from this webpage.

Instructions/suggestions to work comfortably with Ocaml

Installation of OPAM - A Package Manager for OCaml

Firstly, you need to install OPAM (see on that page explanations how to install it on Windows).

Beware, several of the following commands require confirmation with a prompt like:

  > Do you want to continue? [Y/n]
  
You need to answer "y" (yes) to confirm or "n" (no) to abort, then press the "Enter" key. The uppercase letter indicates the default choice if you simply press "Enter". Therefore, launch the commands below one by one and check if questions are asked!

Before using opam for the first time, it needs to be initialized. To do this, execute the following commands:

  opam init -y --shell-setup
  eval $(opam env)
  
Then, you can install opam packages with the command opam install. Install packages that will be useful for the course:
  CHECK_IF_PREINSTALLED=false opam install -y ocamlbuild
  opam install -y menhir merlin graphics dune ocamlformat tuareg ocp-index user-setup ocaml-lsp-server
  opam user-setup install
  eval $(opam env)
  
To use Down (an improved toplevel; does not work on Windows):
  opam install down
  touch ~/.ocamlinit && echo '#use "down.top";;' >> ~/.ocamlinit
  
Now you can comfortably work with OCaml, especially with an editor like Emacs or VIM (see below). If you want to use Visual Studio Code, follow the instructions below.

Visual Studio Code

Before installing/using VSC for OCaml, remember to install ocaml-lsp-server package with a package manager of your choice: OPAM. Installation instructions by package manager are available here. Start by installing Visual Studio Code if you don't have it on your machine. You can also do it using the following commands (on linux-x64):
curl -sSL "https://code.visualstudio.com/sha/download?build=stable&os=linux-x64" -o vscode-client-linux-x64.tar.gz
tar zxf vscode-client-linux-x64.tar.gz
rm vscode-client-linux-x64.tar.gz
echo "export PATH=\$HOME/VSCode-linux-x64/bin/:\$PATH" >> $HOME/.profile

Now, click on and search for extension "OCaml Platform" by typing "OCaml". Install it. Now you can use it.

Click on "Open folder" to choose a folder to work on. Add a file in that folder, e.g. insertion.ml. The file should be recognized by the VSC as an OCAml file (a small orange icon with a camel. It will by type-checked on a fly dynamically and the types/errors will be shown. See more details, in particular for using VSC OCaml on windows, at the Details description of the Ocaml Platform extension.

To run an OCaml program, open a new terminal (Terminal menu) then run a command like:

ocamlopt insertion.ml -o insertion && ./insertion
(or make if we use a Makefile, or dune runtest if using dune, etc.)

It is also possible to send the whole file to a toplevel OCaml with Ctrl-A (select all) then Shift-Enter.

Configuration of Visual Studio Code

It's in the File menu, then Preferences, and Settings, or simply Ctrl-,. There is a search bar.

For OCaml, it is essential to modify the default behavior of VSCode's automatic completion, which chooses the first possible completion when the Enter key is pressed. Without this, the keyword in at the end of the line is automatically replaced by in_channel_length! Look for the option called Editor: Accept Suggestion On Enter and set it to off.

2. To work on your machines

Below, specific instructions are provided for different platforms. Then, you can follow the instructions from part 1 related to opam and Visual Studio Code.

Windows

Follow the installation instructions on the OCaml for Windows page. Choose the graphical installer, in either 32-bit or 64-bit version depending on your machine. This installer allows you to choose the Cygwin packages to install. If you intend to use emacs, make sure to select the emacs-w32 and emacs packages. You need to request the complete list (Full) of packages to see them appear. After the installation (which may take some time), you will have access to a UNIX-like working environment by launching Cygwin.

To use the Cygwin environment within Visual Studio Code (and thus have access to OCaml from VSCode), you need to launch it with ocaml-env. You can create a shortcut (a .lnk file) to automate this (you then need to launch VSCode with this shortcut), with a command similar to "C:\Ocaml64\usr\local\bin\ocaml-env-win.exe" -- "C:\\Program Files\\Path To\\VisualStudioCode.exe". To find the location of the VSCode executable on your machine, you can refer to this StackOverflow link.

Alternative: If the above installation does not work, you can use a Linux virtual machine running on Windows (not ideal, a bit slow, but should still work). Download the course Debian live CD and install VirtualBox. Launch VirtualBox, create a new Debian 32-bit installation, start it and choose to boot from the downloaded image. Then, proceed as described below for a Debian system.

OSX

Install Homebrew and then execute the following command in a terminal:
  brew install opam emacs

Debian or Ubuntu (recent)

In a terminal, run (as a superuser) the following command:
  apt-get install opam emacs

3. Editors

It is suggested to use an editor in which OCaml is well-integrated, such as Emacs, Vim, or Visual Studio Code. All three are available for Linux, Mac, and Windows and are already installed at ENS.

Above, we explained how to configure Visual Studio Code for OCaml.

Emacs

If you want to use emacs, you need to install the Tuareg and Merlin modes for Emacs with the command opam install tuareg merlin and follow the instructions displayed on the screen (see the previous section).

Emacs configuration is done in a .emacs file located at the root of your account. The above installation will have added some lines to this file. In the end, it may look like something like this.

The Merlin mode compiles your program every time you save (to report errors) but does not build an executable.

To build an executable, you can call the OCaml compiler directly from Emacs with the Meta-x compile command (Meta is obtained with the Alt or Esc key, as preferred). You can rerun the same command with Meta-x recompile. If the OCaml compiler reports an error, you can navigate directly to the corresponding position with Meta-x next-error. You can simplify the keyboard shortcuts for these three commands by inserting, for example, the following lines into your ~/.emacs file:

(global-set-key [f5] 'compile)
(global-set-key [f6] 'recompile)
(global-set-key [f7] 'next-error)
This is a very effective way to work. (And you can use these shortcuts for anything, not just OCaml programming.)

In the source code, you can access the type inferred by the compiler with Ctrl-c Ctrl-t and use completion with M-x completion-at-point. Refer to the documentation for more details.

If you prefer Ctrl-C, Ctrl-V, Ctrl-X shortcuts for copy-pasting in Emacs, you can add the line

      (custom-set-variables '(cua-mode t nil (cua-base)))
    
to your .emacs file to activate Cua Mode for Emacs. This is done in the proposed .emacs file.

Vim

To use Vim, you need to install the Merlin mode for Vim with the command opam install merlin. Then, add the following lines to your Vim configuration file (for example, ~/.vimrc):
    let g:opamshare = substitute(system('opam config var share'),'\n$','','''')
    execute "set rtp+=" . g:opamshare . "/merlin/vim"
    
    filetype plugin on
    filetype indent off
    syntax enable
    
The first two lines allow Vim to find the location of Merlin files.

You can initiate a compilation with :make (if the compilation is handled by a Makefile). In case of an error, the cursor will be placed at the corresponding location.

In the source code, you can access the type inferred by the compiler with \ t and use completion with Ctrl-x Ctrl-o. Refer to the documentation.

back to the course page