We consider the problem of counting matchings on graphs that converge locally to trees. Examples include random regular graphs and uniformly random graphs with bounded average degree. Inspired by techniques from random matrix theory, we rigorously prove the validity of the cavity method for the computation of the free entropy density. At a fixed temperature, the cavity equations become equations for the local marginals of the Boltzmann Gibbs distribution. Our main new idea is to consider instead these equations in a functional space, where the marginals are now seen as functions of the temperature. We are also able to define a determinental process on (possibly) infinite trees which is the limit at positive temperature of the matchings on the graph as its size diverges.