We consider Lagrangians that are defined only on the horizontal distribution of a sub-riemannian manifold. The associated Hamiltonian is neither strictly convex nor coercive. We prove a result on homogenization of the Hamilton–Jacobi equation following the approach in Contreras et al (2015 Calc. Var. Partial Differ. Equ. 52 237–52). To establish the result, we need to extend weak KAM and Aubry–Mather theories to the sub-riemannian setting. We obtain a Tonelli theorem to dispend of the Lagrangian dynamics tools used in standard weak KAM theory. In the way towards our main result we observe that, the long time convergence of the Lax-Oleinik semigroup, and the vanishing discount convergence of the discounted value function hold.