Context. Multi-planet systems are excellent laboratories to test planet formation models, since all planets are formed under the same initial conditions. In this context, systems transiting bright stars can play a key role, since planetary masses, radii, and bulk densities can be accurately measured. Aims. GJ 9827 (K2-135) has recently been found to host a tightly packed system consisting of three transiting small planets whose orbital periods of 1.2, 3.6, and 6.2 days are near the 1:3:5 ratio. GJ 9827 hosts the nearest planetary system (d = 30.32 ± 1.62 pc) detected by Kepler and K2. Its brightness (V = 10.35 mag) makes the star an ideal target for detailed studies of the properties of its planets. Methods. Combining the K2 photometry with high-precision radial-velocity measurements gathered with the FIES, HARPS, and HARPS-N spectrographs we revise the system parameters and derive the masses of the three planets.Results. We find that GJ 9827 b has a mass of M b = 3.74 +0.50 −0.48 M ⊕ and a radius of R b = 1.62 +0.17 −0.16 R ⊕ , yielding a mean density of ρ b = 4.81 +1.97 −1.33 g cm −3 . GJ 9827 c has a mass of M c = 1.47 +0.59 −0.58 M ⊕ , radius of R c = 1.27 +0.13 −0.13 R ⊕ , and a mean density of ρ c = 3.87 +2.38 −1.71 g cm −3 . For GJ 9827 d we derive M d = 2.38 +0.71 −0.69 M ⊕ , R d = 2.09 +0.22 −0.21 R ⊕ , and ρ d = 1.42 +0.75 −0.52 g cm −3. Conclusions. GJ 9827 is one of the few known transiting planetary systems for which the masses of all planets have been determined with a precision better than 30%. This system is particularly interesting because all three planets are close to the limit between super-Earths and mini-Neptunes. We also find that the planetary bulk compositions are compatible with a scenario where all three planets formed with similar core/atmosphere compositions, and we speculate that while GJ 9827 b and GJ 9827 c lost their atmospheric envelopes, GJ 9827 d maintained its atmosphere, owing to the much lower stellar irradiation. This makes GJ 9827 one of the very few systems where the dynamical evolution and the atmospheric escape can be studied in detail for all planets, helping us to understand how compact systems form and evolve.