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