The OPERA detector at the Gran Sasso underground laboratory (LNGS) was used to measure the atmospheric muon charge ratio in the TeV energy region. We analyzed 403069 atmospheric muons corresponding to 113.4 days of livetime during the 2008 CNGS run. We computed separately the muon charge ratio for single and for multiple muon events in order to select different energy regions of the primary cosmic ray spectrum and to test the charge ratio dependence on the primary composition. The measured charge ratio values were corrected taking into account the charge-misidentification errors. Data have also been grouped in five bins of the vertical surface energy. A fit to a simplified model of muon production in the atmosphere allowed the determination of the pion and kaon charge ratios weighted by the cosmic ray energy spectrum.