Setting up ocaml on a new Ubuntu 22.04 virtual machine

I'm using a x86_64 linux virtual machine:

$ uname -ms
Linux x86_64

If you're running on a Mac with Apple Silicon (M1/M2) you'll need to look for ways to run x86_64 images. I can suggest looking at UTM or finch, which are free and open source software that can run emulated virtual machines across architectures. In particular, if the output of your uname -ms command is Darwin arm64 or Linux arm64, you can do the first exercises, but you will have trouble completing the compiler assignments, as we will be generating x86_64 assembly, and producing linux elf x86_64 executables.

I'm using Ubuntu Jammy Jellyfish (22.04), which has long term support for at least 5 years.

I'll install ocaml, and the opam package manager, which we can use to manage our ocaml environment. I also install build-essential to get make, gcc, and other tools for compiling.

$ sudo apt-get install --yes build-essential opam ocaml

Now we can setup opam:

$ opam init --yes --shell-setup
$ eval $(opam env --switch=default)

Because I'm a troglodyte, I'll be using emacs a lot, so I'll install emacs and setup ocaml support for emacs. You don't have to use emacs (it's a pain to learn), but you should look into how to setup your own editor to work with ocaml source code.

$ sudo apt-get install --yes emacs-nox
$ opam --yes user-setup install

Opam will set up multiple editors if it can find them in your path. In my case it setup both emacs and vi.

In class I'll be doing a lot of testing and live-coding. Plain ocaml is kind of spare, so I'll install utop, which is a tool for writing ocaml code at the command line:

$ opam --yes install utop

Later on, we'll be installing more tools with opam.