A measurement of J/ψ and ψ(2S) production is presented. It is based on a data sample from Pb+Pb collisions at √sNN = 5.02 TeV and pp collisions at √s = 5.02 TeV recorded by the ATLAS detector at the LHC in 2015, corresponding to an integrated luminosity of 0.42 nb-1 and 25 pb-1 in Pb+Pb and pp, respectively. The measurements of per-event yields, nuclear modification factors, and nonprompt fractions are performed in the dimuon decay channel for 9 < pTμμ < 40 GeV in dimuon transverse momentum, and -2 < yμμ < 2 in rapidity. Strong suppression is found in Pb+Pb collisions for both prompt and non-prompt J /ψ, increasing with event centrality. The suppression of prompt ψ(2S) is observed to be stronger than that of J /ψ, while the suppression of non-prompt ψ(2S) is equal to that of the nonprompt J /ψ within uncertainties, consistent with the expectation that both arise from b-quarks propagating through the medium. Despite prompt and non-prompt J /ψ arising from different mechanisms, the dependence of their nuclear modification factors on centrality is found to be quite similar.