Cancer remains a leading cause of morbidity and mortality worldwide, and the incidence of new cancer cases is rising. While established lifestyle factors such as smoking, obesity, and diet contribute to cancer risk, the gut microbiome has emerged as a hallmark of cancer. However, the role of the gut microbiome in cancer has mainly been established based on case-control studies of individuals with cancer at the time of microbiome sampling (prevalent cancer) and through functional studies. Here, we investigated whether the gut microbiome could identify individuals at increased risk of being diagnosed with cancer during follow-up (incident cancer), potentially many years after microbiome sampling. Using shotgun metagenomic sequencing of stool samples from 5,997 participants in the population-based Lifelines cohort and the Dutch Microbiome Project linked to longitudinal cancer registry data, we developed a microbiome-derived risk score based on 28 microbial species. Individuals with higher microbiome risk scores had a higher risk of receiving a cancer diagnosis during up to 10 years of follow-up (adjusted HR per 1-SD increase, 1.62; 95% CI, 1.44-1.82; P=1.54e-15), after adjusting for established cancer risk factors. In independent external datasets comprising 1,879 patients with cancer at the time of microbiome sampling and 5,341 cancer-free controls, the microbiome risk score also distinguished cancer cases from controls. In the independent population-based FINRISK cohort, the score further showed a consistent trend toward reduced cancer-free survival in 1,105 individuals with incident cancer and 5,670 individuals without cancer during follow-up. Together, these findings demonstrate that the gut microbiome harbors detectable signatures years before cancer diagnosis and highlight its potential as a biomarker for early cancer risk stratification and its potential role in cancer development.