Although the gut microbiome plays important roles in host physiology, health and disease1, we lack understanding of the complex interplay between host genetics and early life environment on the microbial and metabolic composition of the gut. We used the genetically diverse Collaborative Cross mouse system2 to discover that early life history impacts the microbiome composition, whereas dietary changes have only a moderate effect. By contrast, the gut metabolome was shaped mostly by diet, with specific non-dietary metabolites explained by microbial metabolism. Quantitative trait analysis identified mouse genetic trait loci (QTL) that impact the abundances of specific microbes. Human orthologues of genes in the mouse QTL are implicated in gastrointestinal cancer. Additionally, genes located in mouse QTL for Lactobacillales abundance are implicated in arthritis, rheumatic disease and diabetes. Furthermore, Lactobacillales abundance was predictive of higher host T-helper cell counts, suggesting an important link between Lactobacillales and host adaptive immunity.