Population genetic studies of widespread Mediterranean shrubs are scarce compared with those of trees and narrow endemics or studies from phylogeographical perspectives, despite the key role these species may play in Mediterranean ecosystems. Knowledge on the effect of ecological factors in shaping their genetic patterns is also limited. In this study we investigate genetic diversity and population structure across 18 populations of Rosmarinus officinalis, a Mediterranean shrubland plant. Populations were sampled along two elevational gradients, one each on calcareous and siliceous soils in a mountain system in the eastern Iberian Peninsula, to decipher the effect of ecological factors on the genetic diversity and structure based on 11 microsatellite loci. We found overall high levels of genetic diversity and weak population structure. Genetic diversity increased with elevation, whereas population differentiation was stronger among populations growing on siliceous soils. The nested analysis of elevational gradients within soil types revealed that these general patterns were mostly driven by siliceous populations, whereas calcareous populations were more homogeneous along elevational belts. Bayesian analysis of population structure revealed genetic membership of lowland and high‐elevation populations to different genetic clusters and a higher admixture of intermediate‐elevation populations to both clusters. High‐elevation populations were less differentiated from a hypothetical ancestral cluster, suggesting the persistence of their gene pool during the Pleistocene glaciations. In contrast, lowland populations resulted from more recent divergence. We propose that life‐history and reproductive traits mostly contribute to explain the high levels of genetic diversity and weak population structure, whereas ecological and historical factors mostly contribute to the stronger differentiation of siliceous populations and a rapid expansion of R. officinalis on calcareous soils possibly mediated by human landscape transformations, © 2015 The Linnean Society of London, Botanical Journal of the Linnean Society, 2016, 180, 50–63.