The spectral density of various ensembles of sparse symmetric random matrices is analyzed using the cavity method. We consider two cases: matrices whose associated graphs are locally tree-like, and sparse covariance matrices. We derive a closed set of equations from which the density of eigenvalues can be efficiently calculated. Within this approach, the Wigner semicircle law for Gaussian matrices and the Marcenko-Pastur law for covariance matrices are recovered easily. Our results are compared with numerical diagonalization, finding excellent agreement.