We use individual-based stochastic simulations and analytical deterministic predictions to investigate the interaction between drift, natural selection and gene flow on the patterns of local adaptation across a fragmented species’ range under clinally varying selection. Migration between populations follows a stepping-stone pattern and density decreases from the centre to the periphery of the range. Increased migration worsens gene swamping in small marginal populations but mitigates the effect of drift by replenishing genetic variance and helping purge deleterious mutations. Contrary to the deterministic prediction that increased connectivity within the range always inhibits local adaptation, simulations show that low intermediate migration rates improve fitness in marginal populations and attenuate fitness heterogeneity across the range. Such migration rates are optimal in that they maximize the total mean fitness at the scale of the range. Optimal migration rates increase with shallower environmental gradients, smaller marginal populations and higher mutation rates affecting fitness.