Opam create switch - error fetching sources ocaml-base-compiler - mkdir failed on /private/var/folders/zz/

I’ve been successfully using opam switches for years, but haven’t in a while. Tried to create a switch now and got an error. Can’t find anyone else getting this online. Help appreciated!

Setup: Mac M1 OS 15.7.1, opam 2.4.1.

$ opam switch create fall-25 4.14.2

<><> Installing new switch packages <><><><><><><><><><><><><><><><><><><><><><>
Switch invariant: ["ocaml-base-compiler" {= "4.14.2"} | "ocaml-system" {= "4.14.2"}]

<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><>
-> retrieved ocaml-config.2  (cached)
-> installed base-bigarray.base
-> installed base-threads.base
-> installed base-unix.base
-> installed host-arch-arm64.1
-> installed host-system-other.1
-> installed ocaml-options-vanilla.1


#=== ERROR while fetching sources for ocaml-base-compiler.4.14.2 ==============#
OpamSolution.Fetch_fail("/opt/homebrew/bin/opam: \"mkdir\" failed on /private/var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/opam-82366-99e369: Permission denied")


<><> Error report <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
+- The following actions failed
| - fetch ocaml-base-compiler 4.14.2
+-
+- The following changes have been performed (the rest was aborted)
| - install base-bigarray         base
| - install base-threads          base
| - install base-unix             base
| - install host-arch-arm64       1
| - install host-system-other     1
| - install ocaml-options-vanilla 1
+-
Switch initialisation failed: clean up? ('n' will leave the switch partially installed) [Y/n]

Did you run opam update before starting the install?

Thanks. I have not. But it doesn’t help:

$ opam update

<><> Updating package repositories ><><><><><><><><><><><><><><><><><><><><><><>
[ERROR] Could not update repository "coq-released": /opt/homebrew/bin/opam: "mkdir" failed on
        /private/var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/opam-91488-b1944a: Permission denied
[ERROR] Could not update repository "default": /opt/homebrew/bin/opam: "mkdir" failed on
        /private/var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/opam-91488-2b1e04: Permission denied

It looks like you don’t have permissions on temporary directories. Not sure exactly which condition could lead to that.

Well, I’m running this on a non-admin user. Probably should have mentioned that. Does opam switch create only work with admin permissions?

Here’s a screenshot of the problematic directory:

No in fact it is recommended not to run opam as an admin.
I’m not exactly sure how opam creates temporary directory/files. But do you have the TMPDIR or TMP variables set to anything special by any chance ?

Hmm, this is indeed the TMPDIR:

$ echo $TMPDIR
/var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/

(TMP is not set to anything, or set to the empty string.)

I am unfamiliar with these variables. What should they be set to on a standard setup?

I have similar stuff but I do have permissions on these:

> stat $TMPDIR
16777232 248994 drwx------ 220 dbuenzli staff 0 7040 "Oct 16 01:07:42 2025" "Oct 19 15:34:41 2025" "Oct 19 15:34:41 2025" "Mar  8 12:02:25 2023" 4096 0 0x100000 /var/folders/9p/bzrzxtv53zsdsb88p7lbcsk80000gn/T/

(Maybe try to reboot)

(Reboot didn’t help.)

I tried to prefix the command with a different value for the variable and got a different error:

$ TMPDIR="~/tmp" opam switch create fall-25 4.14.2

<><> Installing new switch packages <><><><><><><><><><><><><><><><><><><><><><>
Switch invariant: ["ocaml-base-compiler" {= "4.14.2"}]

<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><>
-> retrieved ocaml-config.2  (cached)
-> installed base-bigarray.base
-> installed base-threads.base
-> installed base-unix.base
-> installed ocaml-options-vanilla.1
-> retrieved ocaml-base-compiler.4.14.2  (cached)
[ERROR] The compilation of ocaml-base-compiler.4.14.2 failed at "./configure
        --prefix=/Users/doe/.opam/fall-25 --docdir=/Users/doe/.opam/fall-25/doc/ocaml -C CC=cc
        ASPP=cc -c".

#=== ERROR while compiling ocaml-base-compiler.4.14.2 =========================#
# context     2.4.1 | macos/arm64 |  | https://opam.ocaml.org#5bf570252bd9fee71938b828ca0b86573c1e3355
# path        ~/.opam/fall-25/.opam-switch/build/ocaml-base-compiler.4.14.2
# command     ~/.opam/opam-init/hooks/sandbox.sh build ./configure --prefix=/Users/doe/.opam/fall-25 --docdir=/Users/doe/.opam/fall-25/doc/ocaml -C CC=cc ASPP=cc -c
# exit-code   65
# env-file    ~/.opam/log/ocaml-base-compiler-91884-ec7407.env
# output-file ~/.opam/log/ocaml-base-compiler-91884-ec7407.out
### output ###
# /Users/doe/.opam/opam-init/hooks/sandbox.sh: line 9: cd: ~/tmp: No such file or directory
# sandbox-exec: empty subpath pattern



<><> Error report <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
+- The following actions failed
| - build ocaml-base-compiler 4.14.2
+- 
+- The following changes have been performed (the rest was aborted)
| - install base-bigarray         base
| - install base-threads          base
| - install base-unix             base
| - install ocaml-options-vanilla 1
+- 
Switch initialisation failed: clean up? ('n' will leave the switch partially installed) [Y/n]

It seems the opam sandboxing mecanism doesn’t allow you to write to these paths, not sure how/if that can be tweaked.

But that being said you likely have a larger problem here. It’s unlikely that opam will be the only system to fail if you can’t write to your temporary directory.

OK, I managed to fix it, thanks for the help!

Turns out that if you reboot with the terminal open and have “reopen windows when logging back in” then the permission problem persists through the reboot; but simply quitting and restarting the terminal fixed the issue.

1 Like