The gluon polarisation in the nucleon has been determined by detecting charm production via D0 meson decay to charged K and π in polarised muon scattering off a longitudinally polarised deuteron target. The data were taken by the COMPASS Collaboration at CERN between 2002 and 2006 and correspond to an integrated luminosity of 2.8 fb−1. The dominant underlying process of charm production is the photon–gluon fusion to a_(c barc) pair. A leading order QCD approach gives an average gluon polarisation of <Δg/g>_x=−0.49±0.27(stat)±0.11(syst) at a scale μ^2≈13 (GeV/c)^2 and at an average gluon momentum fraction <x>≈0.11. The longitudinal cross-section asymmetry for D0 production is presented in bins of the transverse momentum and the energy of the D0 meson.