The bulk viscosity, ηb, of the hard sphere (HS) fluid is computed by equilibrium and nonequilibrium molecular dynamics (NEMD) simulations, the latter using an adaptation of the time-stepping method for continuous potential systems invented by Hoover et al. [Phys. Rev. A 21, 1756 (1980)], which employs an imposed cyclic density variation on the system by affine scaling of the particle coordinates. The time-stepping method employed for HS is validated against exact event-driven hard sphere methodology for a series of equilibrium quantities over a wide density range, including the pressure, singular parts of the hard sphere viscosities, and the nonsingular parts of the shear viscosity time correlation functions. The time steps used are typically only a little smaller than those employed in continuous potential simulations. Exact pressure tensor fluctuation expressions are derived for the singular (or infinite limiting frequency) equilibrium parts of the viscosities, which were employed in the simulations. The values obtained agree well with the predictions of the Enskog theory for all densities considered. The bulk viscosity obtained by NEMD is shown to be noticeably frequency dependent for densities in excess of ∼0.8, decaying approximately exponentially to the Enskog and equilibrium simulation values at all densities considered for frequencies in excess of ∼5 in hard sphere units. Temperature profiles during the cycle and the effects of strain amplitude on the computed frequency dependent bulk viscosity are presented. The bulk viscosity increases with the maximum density amplitude.