Abstract A central problem in the theory of the dynamics of open quantum systems is the derivation of a rigorous and computationally tractable master equation for the reduced system density matrix. Most generally, the evolution of an open quantum system is described by a completely positive linear map. We show how to derive a completely positive Markovian master equation (the Lindblad equation) from such a map by a coarse-graining procedure. We provide a novel and explicit recipe for calculating the coefficients of the master equation, using perturbation theory in the weak-coupling limit. The only parameter external to our theory is the coarse-graining time-scale. We illustrate the method by explicitly deriving the master equation for the spin-boson model. The results are evaluated for the exactly solvable case of pure dephasing, and an excellent agreement is found within the time-scale where the Markovian approximation is expected to be valid. The method can be extended in principle to include non-Markovian effects.