I recently built an OCaml development environment from scratch and thought the steps I went through maybe of use to others. The final environment consists of ocaml, opam. jbuilder, vim and merlin.
Requirements
- Linux OS, this guide uses CentOS 7 but any modern Linux should be similar
- Standard Linux development tools
- curl / wget
- vim and enhanced vim
Note: if you have root access the guide covers the installation of the various tools
Preperation
For Centos 7 install a basic OS and then the development tools, curl, wget and vim-enhanced using yum. As root
yum groupinstall 'Development Tools'
yum install curl wget
yum install vim-enhanced
Note that in many cases distros will already install these as part of the base install.
The rest of the steps are run as a normal user.
Building the bootstrap compiler
Create a suitable directory to build and install the bootstrap compiler
mkdir -p ~/ocaml/bootstrap
cd ~/ocaml/bootstrap
Get latest ocaml, unpack and install in the bootstrap directory. Check for the latest version of the compiler (4.05 in this case) on the ocaml release page.
wget https://caml.inria.fr/pub/distrib/ocaml-4.05/ocaml-4.05.0.tar.gz
tar xzf ocaml-4.05.0.tar.gz
cd ocaml-4.05.0
./configure -prefix $(cd ..; pwd)/install
make world.opt
make install
cd ..
Install OPAM in a suitable location
Start a new shell ready to build opam with the bootstrap compiler as well as creating a suitable directory to install the opam executable in
bash
export PATH=$PATH:~/ocaml/bootstrap/install/bin
mkdir ~/ocaml/tools
Build the required libraries
wget http://ftp.gnu.org/gnu/glpk/glpk-4.63.tar.gz
tar xzf glpk-4.63.tar.gz
cd glpk-4.63
./configure --prefix=$(cd ..; pwd)/install --disable-shared --with-pic
make
make install
cd ..
Get the latest opam source (see opam releases for the current version) and prepare to build it (ignore any git repo errors)
wget https://github.com/ocaml/opam/releases/download/2.0.0-beta4/opam-full-2.0.0-beta4.tar.gz
tar xzf opam-full-2.0.0-beta4.tar.gz
cd opam-full-2.0.0-beta4
CPPFLAGS=-I$(cd ../install/include; pwd) LDFLAGS=-L$(cd ../install/lib;pwd) LIBS="-lm" ./configure --prefix=$(cd ~/ocaml/tools; pwd) --with-mccs
make lib-ext
Fix library dependency paths. Edit src_ext/mccs/src/jbuild and apply the following patch:
--- opam-full-2.0.0-beta4/src_ext/mccs/src/jbuild 2017-08-18 13:56:14.000000000 +0100
+++ opam-full-2.0.0-beta4.hack/src_ext/mccs/src/jbuild 2017-10-10 20:27:21.816265589 +0100
@@ -15,8 +15,8 @@
mccscudf
mccs_stubs))
(public_name mccs)
- (cxx_flags (:standard -I .))
- (c_library_flags (:standard -lglpk -lstdc++)) ;; dynlink version
+ (cxx_flags (:standard -I . -I_HOME_/ocaml/bootstrap/install/include))
+ (c_library_flags (:standard -L_HOME_/ocaml/bootstrap/install/lib -lglpk -lstdc++ -lm)) ;; dynlink version
;; (no_dynlink) (c_library_flags (:standard -lz -lltdl)) ;; static version
;; (library_flags (-cclib -static-libgcc -cclib -Wl,-l:libstdc++.a,-l:libglpk.a))
(wrapped false)
Replace _HOME_
with the directory the ocaml directory is in (~ in this case). This is required as the build process does not properly integrate LDFLAGS and CPPFLAGS. Now build and install opam
make
make install
Next, add ~/ocaml/tools/bin to your PATH in .bash_profile
. For now add the directory to your path:
export PATH=$PATH:~/ocaml/tools/bin
Initialise OPAM and prepare for use:
rm -rf ~/.opam
opam init
Prepare to build the compiler with opam
Exit from the bootstrap sub-shell and either logoff and on again to get the environment or run following commands
export PATH=$PATH:~/ocaml/tools/bin
. ~/.opam/opam-init/init.sh
Build the compiler using opam
List the available compiler versions
opam switch list-available
Select a suitable version and install with:
opam switch 4.05.0
eval $(opam env)
Cleanup the bootstrap process
cleanup the bootstrap directory
rm -rf ocaml/bootstrap
Vim integration
if your a gnome terminal or putty user (or any other terminals that shows TERM as xterm but support 256 colours) add the following to ~/.bash_profile
if [ "$TERM" == "xterm" ]; then
# No it isn't, it's gnome-terminal
export TERM=xterm-256color
fi
add better colours
mkdir -p ~/.vim/colors
curl http://www.vim.org/scripts/download_script.php?src_id=13397 > ~/.vim/colors/wombat256.vim
curl http://www.vim.org/scripts/download_script.php?src_id=13400 > ~/.vim/colors/wombat256mod.vim
curl https://raw.githubusercontent.com/rhysd/vim-color-spring-night/master/colors/spring-night.vim > ~/.vim/colors/sprint-night.vim
Install merlin, for a detailed description see the merlin wiki entry vim-from-scratch
opam install merlin
Edit ~/.vimrc and add
let g:opamshare = substitute(system('opam config var share'),'\n$','','''')
execute "set rtp+=" . g:opamshare . "/merlin/vim"
vim setting, season to taste
let maplocalleader="``"
syntax on
set shiftwidth=2
set expandtab
colorscheme wombat256
set autoindent
Install standard libraries and utop
This step depends on the libraries you are interested
opam install core utop
Next Steps
This completes the build process. See the jbuilder for a suitable build tool, the github entry includes a quick start guide that covers the basics