Host–microbiota interactions play a critical role in the hosts’ biology, and thus, it is crucial to elucidate the mechanisms that shape gut microbial communities. We leveraged threespine stickleback fish (
Gasterosteus aculeatus
) as a model system to investigate the contribution of host and environmental factors to gut microbiota variation. These fish offer a unique opportunity for experiments in naturalistic conditions; we reared benthic and limnetic ecotypes from three different lakes in experimental ponds, allowing us to assess the relative effects of shared environment (pond), geographic origin (lake-of-origin), trophic ecology and genetics (ecotype) and biological sex on gut microbiota α- and β-diversity. Host ecotype had the strongest influence on α-diversity, with benthic fish exhibiting higher diversity than limnetic fish, followed by the rearing environment. β-diversity was primarily shaped by rearing environment, followed by host ecotype, indicating that environmental factors play a crucial role in determining gut microbiota composition. Furthermore, numerous bacterial orders were differentially abundant across ponds, underlining the substantial contribution of environmental factors to gut microbiota variation. Our study illustrates the complex interplay between environmental and host ecological or genetic factors in shaping the stickleback gut microbiota and highlights the value of experiments conducted under naturalistic conditions for understanding gut microbiota dynamics.