Biofilms are spatially structured communities of microbes whose function is dependent on a complex web of symbiotic interactions. Localized interactions within these assemblages are predicted to affect the coexistence of the component species, community structure and function, but there have been few explicit empirical analyses of the evolution of interactions. Here we show, with the use of a two-species community, that selection in a spatially structured environment leads to the evolution of an exploitative interaction. Simple mutations in the genome of one species caused it to adapt to the presence of the other, forming an intimate and specialized association. The derived community was more stable and more productive than the ancestral community. Our results show that evolution in a spatially structured environment can stabilize interactions between species, provoke marked changes in their symbiotic nature and affect community function.