For a Calabi–Yau 4-fold (X,ω), where X is quasi-projective and ω is a nowhere vanishing section of its canonical bundle KX, the (derived) moduli stack of compactly supported perfect complexes MX is −2-shifted symplectic and thus has an orientation bundle Oω→MX in the sense of Borisov–Joyce [11] necessary for defining Donaldson–Thomas type invariants of X. We extend first the orientability result of Cao–Gross–Joyce [23] to projective spin 4-folds. Then for any smooth projective compactification Y, such that D=Y﹨X is strictly normal crossing, we define orientation bundles on the stack MY×MDMY and express these as pullbacks of Z2-bundles in gauge theory, constructed using positive Dirac operators on the double of X. As a result, we relate the orientation bundle Oω→MX to a gauge-theoretic orientation on the classifying space of compactly supported K-theory. Using orientability of the latter, we obtain orientability of MX. We also prove orientability of moduli spaces of stable pairs and Hilbert schemes of proper subschemes. Finally, we consider the compatibility of orientations under direct sums.