  We prove that if $M$ is a closed $n$-dimensional Riemannian manifold, $n \ge
3$, with ${\rm Ric}\ge n-1$ and for which the optimal constant in the critical
Sobolev inequality equals the one of the $n$-dimensional sphere $\mathbb{S}^n$,
then $M$ is isometric to $\mathbb{S}^n$. An almost-rigidity result is also
established, saying that if equality is almost achieved, then $M$ is close in
the measure Gromov-Hausdorff sense to a spherical suspension. These statements
are obtained in the ${\rm RCD}$-setting of (possibly non-smooth) metric measure
spaces satisfying synthetic lower Ricci curvature bounds.
  An independent result of our analysis is the characterization of the best
constant in the Sobolev inequality on any compact ${\rm CD}$ space, extending
to the non-smooth setting a classical result by Aubin. Our arguments are based
on a new concentration compactness result for mGH-converging sequences of ${\rm
RCD}$ spaces and on a Polya-Szego inequality of Euclidean-type in ${\rm CD}$
spaces.
  As an application of the technical tools developed we prove both an existence
result for the Yamabe equation and the continuity of the generalized Yamabe
constant under measure Gromov-Hausdorff convergence, in the ${\rm
RCD}$-setting.
