  Suppose $(X, g)$ is a compact, spin Riemannian 7-manifold, with Dirac
operator $D$. Let $G$ be SU$(m)$ or U$(m)$, and $E\to X$ be a rank $m$ complex
bundle with $G$-structure. Write ${\mathcal B}_E$ for the infinite-dimensional
moduli space of connections on $E$, modulo gauge. There is a natural principal
${\mathbb Z}_2$-bundle $O^D_E\to{\mathcal B}_E$ parametrizing orientations of
det$\,D_{{\rm Ad }A}$ for twisted elliptic operators $D_{{\rm Ad }A}$ at each
$[A]$ in ${\mathcal B}_E$. A theorem of Walpuski shows $O^D_E$ is
trivializable.
  We prove that if we choose an orientation for det$\,D$, and a flag structure
on X in the sense of Joyce arXiv:1610.09836, then we can define canonical
trivializations of $O^D_E$ for all such bundles $E\to X$, satisfying natural
compatibilities.
  Now let $(X,\varphi,g)$ be a compact $G_2$-manifold, with d$(*\varphi)=0$.
Then we can consider moduli spaces ${\mathcal M}_E^{G_2}$ of $G_2$-instantons
on $E\to X$, which are smooth manifolds under suitable transversality
conditions, and derived manifolds in general, with ${\mathcal
M}_E^{G_2}\subset{\mathcal B}_E$. The restriction of $O^D_E$ to ${\mathcal
M}_E^{G_2}$ is the ${\mathbb Z}_2$-bundle of orientations on ${\mathcal
M}_E^{G_2}$. Thus, our theorem induces canonical orientations on all such
$G_2$-instanton moduli spaces ${\mathcal M}_E^{G_2}$.
  This contributes to the Donaldson-Segal programme arXiv:0902.3239, which
proposes defining enumerative invariants of $G_2$-manifolds $(X,\varphi,g)$ by
counting moduli spaces ${\mathcal M}_E^{G_2}$, with signs depending on a choice
of orientation. This paper is a sequel to Joyce-Tanaka-Upmeier
arXiv:1811.01096, which develops the general theory of orientations on
gauge-theoretic moduli spaces, and gives applications in dimensions 3,4,5 and
6. A third paper Cao-Gross-Joyce arXiv:1811.09658 studies orientations on
moduli spaces in dimension 8.
