Can't build base compiler (and some other things) with OPAM

I haven’t used OCaml or OPAM in a couple months, and in the intervening time, I upgraded Ubuntu 22.04 to 24.04. Not sure if that matters, but thought you should know.

Anyway I tried to install the server for VSCoq today and got lots of errors.

An example:

$ opam pin add coq 8.18.0                                     
[misc messages]
[ERROR] The compilation of ocaml.4.11.2 failed at "ocaml /home/ninjaaron/.opam/4.11.2+flambda/share/ocaml-config/gen_ocaml_config.ml 4.11.2 ocaml".

#=== ERROR while compiling ocaml.4.11.2 =======================================#
# context     2.2.1 | linux/x86_64 | ocaml-variants.4.11.2+flambda | https://opam.ocaml.org#08323f3c377fcf2b812d13ae4c3581a6f29f3e59
# path        ~/.opam/4.11.2+flambda/.opam-switch/build/ocaml.4.11.2
# command     ~/.opam/opam-init/hooks/sandbox.sh build ocaml /home/ninjaaron/.opam/4.11.2+flambda/share/ocaml-config/gen_ocaml_config.ml 4.11.2 ocaml
# exit-code   1
# env-file    ~/.opam/log/ocaml-38724-f8e142.env
# output-file ~/.opam/log/ocaml-38724-f8e142.out
### output ###
# bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted


#=== ERROR while compiling conf-pkg-config.3 ==================================#
# context     2.2.1 | linux/x86_64 | ocaml-variants.4.11.2+flambda | https://opam.ocaml.org#08323f3c377fcf2b812d13ae4c3581a6f29f3e59
# path        ~/.opam/4.11.2+flambda/.opam-switch/build/conf-pkg-config.3
# command     ~/.opam/opam-init/hooks/sandbox.sh build pkg-config --help
# exit-code   1
# env-file    ~/.opam/log/conf-pkg-config-38724-e254a6.env
# output-file ~/.opam/log/conf-pkg-config-38724-e254a6.out
### output ###
# bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted


#=== ERROR while compiling conf-gmp.4 =========================================#
# context     2.2.1 | linux/x86_64 | ocaml-variants.4.11.2+flambda | https://opam.ocaml.org#08323f3c377fcf2b812d13ae4c3581a6f29f3e59
# path        ~/.opam/4.11.2+flambda/.opam-switch/build/conf-gmp.4
# command     ~/.opam/opam-init/hooks/sandbox.sh build sh -exc cc -c $CFLAGS -I/usr/local/include test.c
# exit-code   1
# env-file    ~/.opam/log/conf-gmp-38724-da4620.env
# output-file ~/.opam/log/conf-gmp-38724-da4620.out
### output ###
# bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted



<><> Error report <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
┌─ The following actions failed
│ λ build conf-gmp        4
│ λ build conf-pkg-config 3
│ λ build ocaml           4.11.2
└─ 
┌─ The following changes have been performed (the rest was aborted)
│ ⊘ remove    ocaml        4.11.2
│ ↻ recompile ocaml-config 1
└─ 
# Run eval $(opam env) to update the current shell environment
[NOTE] Pinning command successful, but your installed packages may be out of sync.

The former state can be restored with:
    /usr/local/bin/opam switch import "/home/ninjaaron/.opam/4.11.2+flambda/.opam-switch/backup/state-20241105083416.export"

Didn’t feel great about that. Tried upgrading all my packages.

$ opam update
[exits without error]
$ opam upgrade
[truncated]
#=== ERROR while compiling ocaml-base-compiler.5.1.0 ==========================#
# context     2.2.1 | linux/x86_64 |  | https://opam.ocaml.org#08323f3c377fcf2b812d13ae4c3581a6f29f3e59
# path        ~/.opam/5.1.0/.opam-switch/build/ocaml-base-compiler.5.1.0
# command     ~/.opam/opam-init/hooks/sandbox.sh build ./configure --prefix=/home/ninjaaron/.opam/5.1.0 --docdir=/home/ninjaaron/.opam/5.1.0/doc/ocaml -C
# exit-code   1
# env-file    ~/.opam/log/ocaml-base-compiler-41587-95f2fc.env
# output-file ~/.opam/log/ocaml-base-compiler-41587-95f2fc.out
### output ###
# bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted



<><> Error report <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
┌─ The following actions failed
│ λ build ocaml-base-compiler 5.1.0
└─ 
╶─ No changes have been performed

Don’t like that much either. Tried building the latest switch, since rebuilding my current compiler seems to be a no-go.

$ opam switch create 5.2.0 

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

<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><>
⬇ retrieved ocaml-config.3  (cached)
∗ installed base-bigarray.base
∗ installed base-threads.base
∗ installed base-unix.base
∗ installed host-arch-x86_64.1
∗ installed host-system-other.1
∗ installed ocaml-options-vanilla.1
⬇ retrieved ocaml-base-compiler.5.2.0  (cached)
[ERROR] The compilation of ocaml-base-compiler.5.2.0 failed at "./configure --prefix=/home/ninjaaron/.opam/5.2.0 --docdir=/home/ninjaaron/.opam/5.2.0/doc/ocaml -C".

#=== ERROR while compiling ocaml-base-compiler.5.2.0 ==========================#
# context     2.2.1 | linux/x86_64 |  | https://opam.ocaml.org#08323f3c377fcf2b812d13ae4c3581a6f29f3e59
# path        ~/.opam/5.2.0/.opam-switch/build/ocaml-base-compiler.5.2.0
# command     ~/.opam/opam-init/hooks/sandbox.sh build ./configure --prefix=/home/ninjaaron/.opam/5.2.0 --docdir=/home/ninjaaron/.opam/5.2.0/doc/ocaml -C
# exit-code   1
# env-file    ~/.opam/log/ocaml-base-compiler-43790-7416e1.env
# output-file ~/.opam/log/ocaml-base-compiler-43790-7416e1.out
### output ###
# bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted



<><> Error report <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
┌─ The following actions failed
│ λ build ocaml-base-compiler 5.2.0
└─ 
┌─ 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-x86_64      1
│ ∗ install host-system-other     1
│ ∗ install ocaml-options-vanilla 1
└─ 
# Run eval $(opam env --switch=5.2.0) to update the current shell environment
Switch initialisation failed: clean up? ('n' will leave the switch partially installed) [y/n] y

So, I apparently can’t compile compilers. What should I do?

Found more information about this problem: