The inclusive J/ψ production has been studied in Pb–Pb and pp collisions at the centre-of-mass energy per nucleon pair sNN=5.02 TeV, using the ALICE detector at the CERN LHC. The J/ψ meson is reconstructed, in the centre-of-mass rapidity interval 2.5>y<4 and in the transverse-momentum range pT<12 GeV/c, via its decay to a muon pair. In this Letter, we present results on the inclusive J/ψ cross section in pp collisions at s=5.02 TeV and on the nuclear modification factor RAA. The latter is presented as a function of the centrality of the collision and, for central collisions, as a function of the transverse momentum pT of the J/ψ. The measured RAA values indicate a suppression of the J/ψ in nuclear collisions and are then compared to our previous results obtained in Pb–Pb collisions at sNN=2.76 TeV. The ratio of the RAA values at the two energies is also computed and compared to calculations of statistical and dynamical models. The numerical value of the ratio for central events (0–10% centrality) is 1.17±0.04(stat)±0.20(syst). In central events, as a function of pT, a slight increase of RAA with collision energy is visible in the region 2