Species interactions play a critical role in biological invasions. For example, exotic plant and microbe mutualists can facilitate each other's spread as they co-invade novel ranges. Environmental context may influence the effect of mutualisms on invasions in heterogeneous environments, however these effects are poorly understood. We examined the mutualism between the legume, Medicago polymorpha, and the rhizobium, Ensifer medicae, which have both invaded California grasslands. Many of these invaded grasslands are composed of a patchwork of harsh serpentine and relatively benign non-serpentine soils. We grew legume genotypes collected from serpentine or non-serpentine soil in both types of soil in combination with rhizobium genotypes from serpentine or non-serpentine soils and in the absence of rhizobia. Legumes invested more strongly in the mutualism in the home soil type and trends in fitness suggested that this ecotypic divergence was adaptive. Serpentine legumes had greater allocation to symbiotic root nodules in serpentine soil than did non-serpentine legumes and non-serpentine legumes had greater allocation to nodules in non-serpentine soil than did serpentine legumes. Therefore, this invasive legume has undergone the rapid evolution of divergence for soil-specific investment in the mutualism. Contrary to theoretical expectations, the mutualism was less beneficial for legumes grown on the stressful serpentine soil than on the non-serpentine soil, possibly due to the inhibitory effects of serpentine on the benefits derived from the interaction. The soil-specific ability to allocate to a robust microbial mutualism may be a critical, and previously overlooked, adaptation for plants adapting to heterogeneous environments during invasion.