Switching between arm64 and x86_64 architectures for Opam on Mac M1

On a Mac M1, it is possible to install two versions of the OCaml toolchain with opam: either compiled for the x86_64 architecture (and run using Rosetta 2) or for the arm64 architecture (and run nativelly). The former is what happens, IIUC, if you run the install process from a Rosetta-2 terminal.

Is it possible to have both versions installed in two different opam switches ?

1 Like

It’s possible but unwise and untested.

You can try the following (untested) script:

#!/bin/sh

set -e

if test $# -lt 1 ; then
  echo "$0 requires at least 1 argument."
  exit 1
fi

arch=$1
shift

opam var --global "arch=$arch"
arch "-$arch" opam "$@"

which sets the arch opam variable and lunch opam with whichever argument you gave using Rosetta2 if needed.

Examples:

./custom_opam.sh x86_64 list # equivalent to opam list under Rosetta2
./custom_opam.sh arm64 list # basically just opam list

You could also switch automatically to the appropriate switch in this script.

An alternative would be to use two different OPAMROOT to separate x86_64 and arm64 installations

2 Likes

Ok. Thanks for the script !