Beginner issue: `ocamlc` not found in tree or in `PATH`

I just installed OCaml on my computer (macOS Monterrey, M1, which seems to be the cause of lots of issues). Following this guide, I ran:

brew install opam
opam init
eval `opam env`
opam switch create 4.13.1
eval `opam env`

Then created a new directory, went into it and

dune init exe helloworld
dune build

And got

Error: Program ocamlc not found in the tree or in PATH

Lots of things seem to be broken.

What did I do wrong?

Itā€™s eval "$(opam env)" (same as eval "`opam env`").

opam env alone prints a bunch of shell commands, which extend your PATH variable so that programs like ocamlc could be located. You need to run these commands, which is achieved with eval.

Edit: oh I see thatā€™s what you probably did but didnā€™t enclose the commands within multiple ticks. You would type ``eval `opam env` `` to render eval `opam env` .

there isnā€™t any issues for me on Monterrey/M1.

Trying to guess whatā€™s wrong here, did you change terminal between the last

eval `opam env`

and the ā€œError: Program ocamlc not foundā€ ?
If yes, what might have happened is that opam init asked you if you wanted to modify your ~/.zshrc file but the default is ā€œnoā€. See message as to why it is important:

<><> Required setup - please read <><><><><><><><><><><><><><><><><><><><><>  šŸ«

  In normal operation, opam only alters files within ~/.opam.

  However, to best integrate with your system, some environment variables
  should be set. If you allow it to, this initialisation step will update
  your zsh configuration by adding the following line to ~/.zshrc:

    [[ ! -r /Users/kit_ty_kate/.opam/opam-init/init.zsh ]] || source /Users/kit_ty_kate/.opam/opam-init/init.zsh  > /dev/null 2> /dev/null

  Otherwise, every time you want to access your opam installation, you will
  need to run:

    eval $(opam env)

  You can always re-run this setup with 'opam init' later.

Do you want opam to modify ~/.zshrc? [N/y/f]
(default is 'no', use 'f' to choose a different file)

I donā€™t personally know why it is ā€œnoā€ by default but maybe it can be improved.

2 Likes

the backticks are there in the original post, just not really well shown by Discourse.

Thanks for responding to my vague and hard to reproduce issueā€¦ And good call about not modifying the .zshrc. I had skipped that step, but then regretted it afterwards and added that line to my .zshrc manually.

In any case: I did rm -rf ~/.opam && brew remove opam, started a new shell and reran everything from the beginning, and itā€™s all working now.

1 Like

Iā€™ve opened a PR to make the default behaviour ā€œyesā€ by default Make yes the default answer to opam init's setup in interactive mode by kit-ty-kate Ā· Pull Request #4877 Ā· ocaml/opam Ā· GitHub

2 Likes