Tuareg can't find ocaml

I’ve just installed tuareg in my GNU Emacs 27.2 (running on macOS), and it does syntax highlighting in tuareg-mode, but I can’t successfully start ocaml in an inferior shell with ‘M-x run-ocaml’. It says ‘no such file or directory: ocaml’. I can start run ocaml just fine by opening a shell window with ‘C-c C-s’ and typing ‘ocaml’.

Any ideas about what could be wrong?

Is this GUI emacs? (i.e., a normal macOS window and not emacs running in a terminal)

GUI applications in macOS have a very different environment than your shell does, including the PATH variable. exec-path-from-shell is an example of a package that tries to fix this, so it might be worth giving that a try.

I had this problem when I was working with a Mac, too. IIRC, I figured out the command-line path to invoke Emacs.app from a Terminal, and then invoked it after setting environment variables properly; then everything worked properly.

Alternatively, surely there’s an emacs variable you can set to for the path-to-ocaml-binary, no?

But even if you did that, if you want to use opam switches, you’ll want to set environment variables before running ocaml. For that I found the “inopam” script that someone posted a while back to be very useful:


set -e


mkdir -p /tmp/debugopam
if [ "$1" = "opam" ] ; then
  cmd="$@ --debug-level 2 2> /tmp/debugopam/err_$switch"
OPAMDEBUGSECTIONS="GSTATE SYSTEM" # to retrieve only a subset of debug infos
exec opam exec --switch "$switch" --set-switch --set-root -- "$cmd"
# clean debug files as everything succeed ; or remove them by hand to not automatise an rm
if [ $? -eq 0 ]; then
  rm /tmp/debugopam/err_*

Here’s the original post: Newbie tip: using multiple projects/switches from command line and emacs
My version of the script is longer, b/c there was a bug, and one of the opam maintainers suggested this modification as a fix.