Ambient air pollution was known to cause central nervous system diseases and depressive symptoms. In this study, we examined the associations between air pollution exposure and the prevalence of insomnia in Taipei City of Taiwan. We applied the health information system of electrical medical records of Taipei City Hospital to collect a total of 5108 study subjects (insomniacs N = 912 and non-insomniacs N = 4196) over 18 years old from the family medicine and internal medicine outpatients of six branches of Taipei City Hospital. These patients were grouped into insomniacs and non-insomniacs following the primary insomnia diagnosis (ICD9:780.52, 780.54, 307.41, 307.42, ICD10: G47.00, G47.01, G47.09, F51.01, F51.09) and the prescription times of anxiolytics and hypnotics. We estimated one-year average concentrations of PM2.5, ozone, and NOx before the first date of insomnia diagnosis and the last date of outpatient visit for insomniacs and non-insomniacs, respectively, by using the data of nearest air quality monitoring stations relative to study subjects’ residential addresses. Logistic regression analysis was employed to examine the independent effects of air pollution concentrations on the risk of insomnia. One-year average PM2.5, ozone, and NOx levels for insomniacs was significantly higher than those of non-insomniacs. After adjusting for confounding factors, increase each 1(μg/m3) in one-year average PM2.5 showed a statistically significant association with insomnia (the odds ratio 1.610, 95% CI [1.562,1.660]). As to multi pollutants, one-year average PM2.5 (1.624, [1.570, 1.681] and ozone (1.198, [1.094, 1.311]) exposure showed a significant association with insomnia. Subgroup analysis revealed that the influence of PM2.5 and ozone on insomnia have significant risks in people with major chronic disease. This study demonstrated a positive association between PM2.5 and ozone exposure and the prevalence of hypnotic-treated insomnia. Especially, the people with major chronic diseases were with obvious effect of PM2.5 and ozone on risk of insomnia.