Forty-six samples of urban topsoil were collected from Tripoli at a depth of 0–15 cm to determine the concentration of nine heavy metals, including Cd, Pb, Zn, Cu, Ni, Co, Fe, Mn, and Cr. The study aimed to quantify and identify their likely sources of environmental contamination indices and assess their ecological risk to humans. The mean concentrations of these metals were 3.943, 38.317, 170.941, 29.669, 19.699, 30.371, 27444.515, 235.812, and 195.92 mg.kg-1, respectively. These values are higher than the virgin soil background values and permissible limits set forth by WHO. The pollution index (PI) varied from 1.152 for Ni and 2.518 for Cr. The geoaccumulation index (Igeo) averaged 0.824 for Mn and 1.679 for Cr, the contamination factor (CF) averaged 1.28 for Mn and 2.686 for Cr, and the enrichment factor (EF) averaged 0.968 for Mn and 1.893 for Cr, respectively. Pollution indicators such as the Pollution Load Index (PLI), Contamination Degree (Cdeg), and Potential Ecological Risk (RI) suggest that the research region is moderately contaminated, with values ranging from 1.233–1.923, 12.099–18.469, and 47.241–145.067, respectively. Principal component analysis (PCA), correlation coefficient, and hierarchical cluster analysis (HCA) had used to identify the studied metals' possible sources. The results revealed that three sources of heavy metals were determined as indicated by a strong positive correlation between Cd, Pb, Cu, Cr, and Co, which suggested that the sources of these metals are mainly anthropogenic, such as agricultural practices, industrial activities, and vehicle emissions. While the possible sources of Mn, Fe, and Ni mainly originate from natural sources (parent rocks). The heavy metals' potential ecological risk was determined to be moderate. The RI ranged from 65 to 130.