Install OCaml

The OCaml compiler and libraries can be installed in several ways. Broadly, the options are:

  • use OPAM, a package manager specific to OCaml
  • use a package manager supported by your platform (Windows, Linux, Mac OS X, ...)
  • install from source code.

The following sections explain how to use each of these methods. For additional information about finding and installing OCaml libraries, see the libraries page.

Multi-Platform Package Managers

Some dedicated package managers are available for OCaml, that can be used on multiple platforms:

OPAM

OPAM is a package manager for OCaml. It is the recommended method to install the OCaml compiler and OCaml packages. The default OPAM repository is actively maintained and serves as the de facto master set of OCaml packages. OPAM also provides features that would be unavailable with the other methods, such as the ability to have multiple versions of OCaml installed at the same time, or have multiple universes of packages for different projects with conflicting package dependencies.

Note that there is a bootstrapping issue, since OPAM is itself implemented in OCaml. To address this, binary OPAM packages are made available on many platforms. See here and below, in the platform specific sections, for installation instructions. Alternatively, you can compile from source by grabbing one of the recent releases. More information is available in the Quick Install and Advanced Install pages.

Once you have OPAM installed, the switch and install commands are the ones you're likely to need first. For example:

opam switch 4.02.1
eval `opam config env`

will compile OCaml 4.02.1 and install it in a location managed by OPAM. The second line assures you are now using this version of the compiler. You can skip this step if you're happy with the version of OCaml available on your system , but this lags substantially on many platforms.

Next you can install packages, e.g.

opam install batteries core

will install Batteries and Core, two widely used standard libraries. See the main repository to find hundreds more, and visit OPAM's main page to learn about many additional features.

OPAM is based on the CUDF library developed by the Mancoosi project, which is, among other things, used by Debian to manage their packages. It works well on Unix, Linux, and Mac OS X systems. Windows support is comming soon. OPAM is written and maintained by OCamlPro and OCaml Labs, and is free software (commercial support is available).

Oasis-DB

See here.

ODB

See here.

ocamlbrew

See here.

Linux Distributions

OCaml is directly available in most Linux distributions, through their package managers. We provide details here for the most popular ones.

Debian

OCaml is very easy to install on Debian.

apt-get install ocaml

If you are not going to develop graphical applications, you may want to install ocaml-nox; otherwise install the ocaml package.

We also recommend you to install the following packages:

  • ocaml-native-compilers to be able to compile native binaries (this is not available on some more obscure architectures).
  • ocaml-doc to have the reference manual.
  • tuareg-mode, an Emacs mode for OCaml.
  • ocaml-findlib and oasis to install and use libraries (and their dependencies) easily.
  • libpcre-ocaml-dev which is a binding to PCRE.

Check the packages available in Debian.

Ubuntu

Ubuntu is a Debian derivative and uses the same package manager, so Debian instructions are applicable to it too.

You can view the list of OCaml packages in Ubuntu repositories here.

Ubuntu's official repositories often lag substantially behind the latest official releases of OCaml and OPAM. More recent versions are made available by Anil Madhavapeddy in his PPAs. You can mimic the opam-repository's test script to add these PPAs on your own Ubuntu box. Please note PPAs are not reviewed at all by Ubuntu's security team and you are trusting the PPA's distributor by adding them to your system.

Fedora

Since Fedora 8, Fedora has excellent support for OCaml in the basic distribution. There is an active group of maintainers who keep up to date with the latest OCaml, and there is a wide range of packages available.

To install the OCaml compiler just do:

yum install ocaml

To install an OCaml library called 'foo', and any dependencies it needs, you would do:

yum install ocaml-foo-devel

A good set of basic development libraries can be installed by doing:

yum install ocaml-camlp4-devel ocaml-ocamldoc ocaml-findlib-devel \
   ocaml-extlib-devel ocaml-calendar-devel

To list all OCaml packages use:

yum search ocaml

Packaging policy and mailing lists

Gentoo

In order to get the basic tools under Gentoo, execute:

emerge ocaml  

while being logged in as a user that is a member of the portage group (e.g. root). If you want to see what other related packages are available execute

emerge -S ocaml 

SuSE

OCaml can be installed from the repositories on both OpenSuSE and SLES:

zypper install ocaml

Mageia

Mageia has some support for OCaml in the distribution.

To install the OCaml compilers just do:

urpmi ocaml-compiler

ocaml-compiler has no dependency on X11 libs, to install the libs that have some (for example the module Graphics):

urpmi ocaml-x11

To install camlp4:

urpmi camlp4

To install an OCaml library called 'foo', and any dependencies it needs, you would do:

urpmi ocaml-foo-devel

A good set of basic development libraries can be installed by doing:

urpmi camlp4-devel ocaml-doc ocaml-findlib-devel \
  ocaml-extlib-devel ocaml-extlib-doc ocaml-batteries-devel \
  ocaml-ounit-devel ocaml-sexplib-devel ocaml-xml-light-devel \
  ocamlmakefile

To list OCaml packages, use:

urpmq --list | grep ocaml

Packaging policy

  • OCaml packaging policy in Mageia. All Mageia OCaml packages have to obey this policy, ensuring a minimum standard for all OCaml packages we ship.

Windows

Under Windows, two solutions are available to use OCaml: the official OCaml distribution relies on Cygwin, while OCamlPro provides OCPWin, a binary distribution working without Cygwin.

OCaml on Cygwin

Three ports of OCaml for Microsoft Windows are currently available. For additional information, please consult the list of portability issues or the Windows release notes.

  • Cygwin-based native Win32 port (4.00.1). A self installer. The interactive loop comes with a simple graphical user interface. Some features require the Cygwin environment, which the installer can fetch for you. However, the compilers are true Win32 executables, and binaries they generate do not require Cygwin to run too.
  • Microsoft-based native Win32 ports. No binary distributions available yet; download the source distribution and compile it. Build instructions including required tools (Cygwin required) and download links in the source distribution (README.win32).
    • Configurations supported include 32-bit/64-bit;
    • Microsoft toolset support provided by Windows 7 SDK;
    • Tested with XP/Windows 7/Windows 8.1.
    • On Windows 8.1
      • Cygwin chmod 0600 appears broken : comment L367 of Makefile.nt (@chmod -w utils/config.ml);
      • This same problem with Cygwin chmod also affects ssh configuration setup (may affect interaction with github projects). See this page for a suggested workaround.
  • Cygwin-based port. Requires Cygwin. No graphical user interface is provided. The compilers generate executables that do require Cygwin. The precompiled binaries are part of the Cygwin distribution; you can install them using the Cygwin setup tool. Alternatively, download the source distribution and compile it under Cygwin.

To install libraries, you may use Wodi or OPAM.

OCPWIN, Self-Contained OCaml for Windows

OCPWin is a self-contained binary distribution of OCaml for Windows. It supports both 32-bit and 64-bit Windows platforms, and can compile both bytecode and native code applications, directly from a Windows terminal, without installing other software. The license agreement allows both commercial and non-commercial use, as long as the compiler parts themselves are not redistributed. A binary installer is provided for OCaml 4.01.0. Some additional features have been added to OCaml for better support of Windows.

Mac OS X

On Mac OS X, there are, at least for the base package, a few different ways to go: Use OPAM; Install the binary package from Inria; Install via Fink; Install via MacPorts; or build it manually from sources. For each of these approaches, you will need to have at least the OS X developer tools installed — any other requirements will be discussed below.

Inria's binary package

This is the easiest way to set up a basic OCaml installation for OS X. Simply download Intel disk image, mount the disk image, double-click on the ocaml.pkg file contained within, and follow the instructions from there. Requirements for this package are OS X 10.7.x (Lion) or later, with XCode tools (v4.3.3 or later) installed. Earlier versions may or may not work.

The package only includes command-line tools and does not include any graphical applications.

Fink

Fink is the most prevalent package management system for OS X, and is based on Debian's package management system. Fink can be installed by downloading and building the source release (you will need to have the OS X Developer Tools installed before building Fink). After Fink is installed, it must be configured to use the unstable branch of the distribution tree -- this is where the OCaml packages reside.

Once Fink is installed, you can use it to install the following packages:

  • ocaml The basic installation of the ocaml tools including labltk.
  • lablgl The OCaml interface to OpenGL
  • lablgtk The OCaml interface to gtk+
  • ledit An optional line editor for OCaml to make it easier to work with the interactive toplevel in the terminal.

It is likely that Fink will need to download and install a number of other packages required to build the OCaml packages, but this will occur mostly automatically.

MacPorts

MacPorts, formerly known as DarwinPorts, is a package management system for Mac OS X based on the BSD ports system. You will need to have the OS X Developer Tools installed before installing MacPorts. MacPorts can be used to download and build the following packages:

  • ocaml The basic installation of the ocaml tools including labltk.
  • lablgl The OCaml interface to OpenGL
  • lablgtk The OCaml interface to gtk+
  • lablgtk2 The OCaml interface to gtk+ 2.x
  • cryptokit An OCaml interface to several cryptographic functions.
  • ocaml-mode.el An EMACS major mode for editing OCaml programs.
  • tuareg-mode.el A GNU Emacs/XEmacs major mode for editing OCaml programs.

Homebrew

Homebrew is a new and upcoming package management system for Mac OS X and has a very large community. Homebrew requires the command line tools for Xcode and either bash or zsh to install.

After installing Homebrew, you can install OCaml by issuing the following command:

brew install ocaml

You can then install OPAM, the OCaml package manager, which will give you access to all its packages by running:

brew install opam

Building from sources

Unfortunately, the packages available for both Fink and DarwinPorts don't yet include some of the more useful additions to the OCaml collection, such as Markus Mottl's PCRE-Ocaml library or Stolpmann's findlib system. In order to get those packages, one must currently build them from source.

The following packages are the most beneficial to install:

If additional components, such as OpenGL and Gtk+ or Gtk+2 have already been installed, packages such as lablgl, lablgtk, etc. can be built as well. The most flexible solution for doing this is often using Fink to install the required dependencies, and build the OCaml packages from source distributions.

Using labltk with OS X

There are a couple of different ways one can go if they wish to use labltk with OS X. One could download and build the Tcl/Tk libraries themselves and use those libraries. However, there are two easier ways to proceed, using Fink to install Tcl/Tk, or using the Tcl/Tk Aqua Framework. There are two very important distinction between the two that one needs to consider before installing OCaml and labltk:

  1. The fink libraries require X11 to be running when any program using labltk is executed, while the Tcl/Tk Aqua libraries will execute (and look and feel) like a native OS X application
  2. As of the time of writing this, it appears as if labltk applications built with the Tcl/Tk Aqua libraries must be built as native OS X applications (and require an extra step at build time). If one requires the portability of bytecode, or needs the interactive environment of a labltk enabled toplevel, then they must use the fink libraries.

Installing the fink libraries

Once Fink is installed and configured, install the Tcl/Tk libraries with the command fink install tcltk tcltk-dev tcltk-shlibs and wait for the build and install process to compete. Once this is done, OCaml's configure script should be able to locate the libraries and header files required to build labltk (provided fink is set up to install its packages in the /sw directory). Running the standard OCaml build will also build labltk.

Installing the Tcl/Tk Aqua libraries

This framework comes with OS X 10.4 and later. Configure the OCaml build by using the following flags:

./configure -tkdefs \
  "-I/Library/Frameworks/Tcl.framework/Headers \
  -I/Library/Frameworks/Tk.framework/Headers" \
  -tklibs "-framework Tcl -framework Tk" 

Now building OCaml will also build labltk using the Tcl/Tk Aqua libraries.

As mentioned above, any programs using labltk must be compiled using ocamlopt, and an additional step must be performed before the application can be used. For example, with the simple program, hello.ml:

open Tk;;
let hello () = print_endline "Hello!"; flush stdout in let top = openTk () in let hb = Button.create ~text:"Hello" ~command:hello top in pack [hb]; mainLoop ()

One would compile this program with the command ocamlopt -o hello -I +labltk labltk.cmxa hello.ml

Now, to make the program work properly, one must perform one of two additional steps: adding a resource fork to the executable, or building a Mac OS X .app structure.

To add a resource fork, one needs to use the program Rez, included in the OS X Developer tools. This can be done with the following command:

/Developer/Tools/Rez -t APPL -o hello ~/dev/mac.r \
  -i /Library/Frameworks/Tcl.framework/Headers \
  -i /Library/Frameworks/Tk.framework/Headers   

Where mac.r is a Rez source file. It is part of the FLTK distribution. This will add the required resource fork to the hello application. The program can be run either by typing hello at the command line or by double-clicking the app's icon (note that if it is launched by double-clicking, hello will send its output to the OS X console rather than the terminal).

Unfortunately, files with resource forks can present a problem in that utilities like cp, mv, tar, etc. will strip the resource fork from the file, breaking the application. To aleviate this problem, one can build an OS X application bundle to wrap the compiled executable.

To do this, after compiling with ocamlopt -o hello -I +labltk labltk.cmxa hello.ml, the following steps will build the bundle:

mkdir hello.app
mkdir hello.app/Contents
mkdir hello.app/Contents/MacOS
mv hello hello.app/Contents/MacOS  

Next, create a file hello.app/Contents/Info.plist with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
 <dict>
   <key>CFBundleExecutable</key>
   <string>hello</string>
 </dict>
</plist>        

The hello application can now be run from the command line by typing open hello.app, or by double-clicking on the app's icon (again, this will send hello.app's output to the console).

Tips

Documentation. To get quick access to the documentation of a module (whatever the editor you use), use Quicksilver to index the libref/ directory of ocaml's documentation. Since ocamldoc generates the documentation of a module M in a file M.html, you can access it by invoking Quicksilver, type an abbreviation of the module's name you want and hit return to get its html file loaded in your preferred browser.

Profiling. To profile native-code programs do not compile them with the option -p (this is unsupported) simply use Shark. Shark can be invoked directly from the command line as follows :

shark -i -1 -q myprogram.opt args   

This will write a .mshark file in the directory that you can open with Shark.app.

open *.mshark  

Note that if your executable doesn't run for long enough Shark won't be able to take any samples and won't report any statistics. More function names will show up in the profiles if you compile with -g.

FreeBSD

FreeBSD had a great support for OCaml development for a long time. There are easy to use packages available for most popular platforms (i386, amd64, powerpc, sparc64) for both the core languages and supplimentary libraries, documentation, examples and development tools. There is a framework available to make adding new ocaml application and libraries easy.

To install the OCaml compiler from packages, do:

pkg_add -r ocaml

or

pkg_add -r ocaml-nox11

to install OCaml without X11-dependent libraries (e.g. to deploy on a headless server).

To install the developer documentation, examples and emacs editing macros use the ocaml-doc, ocaml-examples and ocaml-mode.el packages.

The same packages can be installed via the port system. There is a large set of libraries available in the devel category of the ports system as well as in the specific topic-related categories.

From Source

Download the source for your preferred OCaml release (or take the development version using Subversion or Git) and follow the instructions included therein.