To date, there have been few reports investigating the relationship between tuberculosis (TB) and gastric cancer. We conducted a nationwide population-based matched cohort study using data retrieved from Taiwan's National Health Insurance Research Database to determine the incidence of and risk factors for TB in patients diagnosed with gastric cancer. From 2000 to 2011, we identified 36,972 gastric cancer patients and normal subjects from the general population matched for age, sex, and comorbidities at a 1:1 ratio. The data were analyzed using Cox proportional hazards models. Compared with the matched cohort, gastric cancer patients exhibited a higher risk for TB (adjusted hazard ratio [HR] 1.50, 95% confidence interval [CI] 1.41-1.79, P<0.001), and those with TB exhibited higher mortality (adjusted HR 2.29, 95% CI 2.03-2.29, P<0.001). Old age (adjusted HR 2.40, 95% CI 1.92-2.99, P<0.001), male sex (adjusted HR 2.13, 95% CI 1.76-2.57, P<0.001), diabetes mellitus (adjusted HR 1.28, 95% CI 1.05-1.56, P=0.013), and chronic obstructive pulmonary disease (COPD) (adjusted HR 1.44, 95% CI 1.19-1.75, P<0.001) were identified as independent risk factors for TB in gastric cancer patients. Dyslipidemia was an independent protective factor for both TB (adjusted HR 2.13, 95% CI 1.73-2.62, P<0.001) and mortality (adjusted HR 1.11, 95% CI 1.08-1.15, P<0.001) in gastric cancer patients. Old age, male sex, diabetes mellitus, and COPD were independent risk factors for TB in gastric cancer. High-risk gastric cancer patients, especially those in TB-endemic areas, should be regularly screened for TB.