This is a walkthrough on how to setup a project so we can run some http requests against the Twitter API with OCaml. If you already now how to do this, you can skip to the next articles in this series. Have at it!
Setting up the OCaml Postman Twitter project
Download
opamif you don't have it. Check the install instructions on the link on the opam home page.After you have it installed make a director for this project and
cdinto it:mkdir ocaml-twitter-postman && cd ocaml-twitter-postman.We need to create a compiler specific to this project which we will do in our root directory which is the
/ocaml-twitter-postmandir, by runningopam switch create . ocaml-base-compiler.4.08.1. This part takes a while so go for a walk around the block. Thisswitchis the one we will use to compile ourOCamlcode in all the examples. After the process finishes lets do what the output is telling us and runeval $(opam env)to update the current shell environment so it knows to use this compiler.We are going to need
duneto run some commands so let's install it with:opam install dune.Now let's make a directory for first example,
mkdir hello-postman && cd !$then create adunefile and initiatedune. Thedune installcommand we run below creates adune-projectfile we wont ever look at again. Run:
touch dune && dune install
# running `tree` in your terminal gets you:
.
├── _build
│ └── log
├── dune
└── dune-project
1 directory, 3 files
Check out dune.build to get started there if you don't know that its not just a movie.
- Create a
hello_postman.mlfile in the root with the following contents:
Lwt_main.run (Lwt_io.printf "Hello, postman!\n")
- Configure the
dunefile by adding the following to it:
(executable
(name hello_postman)
(libraries lwt.unix))
- Lets add a
Makefileto simplify some running commands. Runtouch Makefileand the following to it:
INSTALL_ARGS := $(if $(PREFIX),--prefix $(PREFIX),)
default:
dune build
install:
dune install $(INSTALL_ARGS)
uninstall:
dune uninstall $(INSTALL_ARGS)
reinstall: uninstall install
clean:
dune clean
.PHONY: default install uninstall reinstall clean
I don't remember where I stole this file from but thank you, nonetheless.
- To build and run the project run the following commands from the
./hello-postmandirectory.makewill run the script at the top of the make file then thedefaultargument in which is thebuildcommand. This will install any dependencies we listed in ourdunefile then build the project. This is doing the same thing as when we randune installabove. So if you runmake cleanit will get rid of all the generated files. You can then just runmakeand your deps will be installed anddune-projectwill be created. #NoteToSelf: Its worth remembering that we are using the compiler we created in the root repository.
make clean
make
dune exec ./hello_postman.exe
# output
➜ hello-postman make
dune build
Info: Creating file dune-project with this contents:
| (lang dune 2.1)
➜ hello-postman dune exec ./hello_postman.exe
Hello, postman!
- Add a
OCaml.gitignorefile. You can find thegithubtemplate here
The source code for this post is here.
That was quick. Check out Part 2 to get a bit more serious.


Top comments (0)