Context : The Gaia Data Release 2 ( DR2 ) contains the first release of radial velocities complementing the kinematic data of a sample of about 7 million relatively bright , late-type stars . Aims : This paper provides a detailed description of the Gaia spectroscopic data processing pipeline , and of the approach adopted to derive the radial velocities presented in DR2 . Methods : The pipeline must perform four main tasks : ( i ) clean and reduce the spectra observed with the Radial Velocity Spectrometer ( RVS ) ; ( ii ) calibrate the RVS instrument , including wavelength , straylight , line-spread function , bias non-uniformity , and photometric zeropoint ; ( iii ) extract the radial velocities ; and ( iv ) verify the accuracy and precision of the results . The radial velocity of a star is obtained through a fit of the RVS spectrum relative to an appropriate synthetic template spectrum . An additional task of the spectroscopic pipeline was to provide first-order estimates of the stellar atmospheric parameters required to select such template spectra . We describe the pipeline features and present the detailed calibration algorithms and software solutions we used to produce the radial velocities published in DR2 . Results : The spectroscopic processing pipeline produced median radial velocities for Gaia stars with narrow-band near-IR magnitude G _ { \mathrm { RVS } } \leq 12 ( i.e . brighter than V \sim 13 ) . Stars identified as double-lined spectroscopic binaries were removed from the pipeline , while variable stars , single-lined , and non-detected double-lined spectroscopic binaries were treated as single stars . The scatter in radial velocity among different observations of a same star , also published in Gaia DR2 , provides information about radial velocity variability . For the hottest ( T _ { eff } \geq 7000 K ) and coolest ( T _ { eff } \leq 3500 K ) stars , the accuracy and precision of the stellar parameter estimates are not sufficient to allow selection of appropriate templates . The radial velocities obtained for these stars were removed from DR2 . The pipeline also provides a first-order estimate of the performance obtained . The overall accuracy of radial velocity measurements is around \sim 200 -300 \text { m~ { } s } ^ { -1 } , and the overall precision is \sim 1 \text { km~ { } s } ^ { -1 } ; it reaches \sim 200 \text { m~ { } s } ^ { -1 } for the brightest stars . Conclusions :