Panax ginseng C.A. Meyer, known as the "King of Herbs," has been used as a nutritional supplement for both food and medicine with the functions of relieving fatigue and improving immunity for thousands of years in China. In agricultural planting, soil environments of different geographical origins lead to obvious differences in the quality of ginseng, but the potential mechanism of the differences remains unclear. In this study, 20 key differential metabolites, including ginsenoside Rb1, glucose 6-phosphate, etc., were found in ginseng from 10 locations in China using an ultra-high performance liquid chromatography-quadrupole time-of-flight mass spectrometry (UHPLC-QTOF-MS)-untargeted metabolomics approach. The soil properties were analyzed and combined with metagenomics technology to explore the possible relationships among microbial elements in planting soil. Through Spearman correlation analysis, it was found that the top 10 microbial colonies with the highest abundance in the soil were significantly correlated with key metabolites. In addition, the relationship model established by the random forest algorithm and the quantitative relationship between soil microbial abundance and ginseng metabolites were successfully predicted. The XGboost model was used to determine 20(R)-ginseng Rg2 and 2'(R)-ginseng Rg3 as feature labeled metabolites, and the optimal ginseng production area was discovered. These results prove that the accumulation of metabolites in ginseng was influenced by microorganisms in the planting soil, which led to geographical differences in ginseng quality.