Vertebrates harbour diverse communities of symbiotic gut microbes. Host diet is known to alter microbiota composition, implying that dietary treatments might alleviate diseases arising from altered microbial composition (dysbiosis). However, it remains unclear whether diet effects are general or depend on host genotype. Here we show that gut microbiota composition depends on interactions between host diet and sex within populations of wild and laboratory fish, laboratory mice and humans. Within each of two natural fish populations (threespine stickleback and Eurasian perch), among-individual diet variation is correlated with individual differences in gut microbiota. However, these dietmicrobiota associations are sex dependent. We document similar sex-specific dietmicrobiota correlations in humans. Experimental diet manipulations in laboratory stickleback and mice confirmed that diet affects microbiota differently in males versus females. The prevalence of such genotype by environment (sex by diet) interactions implies that therapies to treat dysbiosis might have sex-specific effects.