The charge asymmetry in the production of top quark and antiquark pairs is measured in proton-proton collisions at a center-of-mass energy of 8 TeV. The data, corresponding to an integrated luminosity of 19.6 fb-1, were collected by the CMS experiment at the LHC. Events with a single isolated electron or muon, and four or more jets, at least one of which is likely to have originated from hadronization of a bottom quark, are selected. A template technique is used to measure the asymmetry in the distribution of differences in the top quark and antiquark absolute rapidities. The measured asymmetry is Acy=[0.33±0.26(stat)±0.33(syst)]%, which is the most precise result to date. The results are compared to calculations based on the standard model and on several beyond-the-standard-model scenarios.