# How is it possible to define a potential energy of a magnetic dipole if $\bf{B}$ is not conservative?

The magnetic field $\mathbf{B}$ is not conservative (it is not even irrotational). Nevertheless, considering a small loop (of area $S$ ) with electric current $i$ (equivalent to a magnetic dipole) in uniform $\mathbf{B}$, it is defined a potential energy of the loop as $$\mathrm{U_p}={-S \,\,i}\,\, \hat{\mathbf{n}} \times \mathbf{B}$$

And the equilibrium positions are the ones with minimum $\mathrm{U_p}$.

How is this possible? Isn't that in complete contrast with the non conservativity of $\mathbf{B}$?

A quantity $\mathbf S$ is conservative if and only if $\nabla\times\mathbf S = 0$. Lets look at some equations: $$\mu_0\mathbf J = \nabla\times\mathbf B \\ 0 = \nabla\cdot\mathbf B$$
In the case of the magnetic field, it has non-null rotational equal to the current density. But, we can define a potential energy valid only in the points of space where there is no current density, that is $\mathbf J = 0$. Because then, in those points, magnetic field is irrotational and then conservative, and then there exists potential function. The potential energy of the current loop assumes there is no current out of the loop.