Context. The physical conditions in a collapsing cloud can be traced by observations of molecular lines. To correctly interpret these observations the abundance distributions of the observed species need to be derived. The chemistry in a collapsing molecular cloud is not in a steady state as the density and temperature evolve. We therefore need to follow chemical reactions, both in the gas phase and on dust grains, as well as gas-grain interactions, to predict the abundance distributions. Aims. Our aim is to model the abundances of molecules, in the gas phase and on grain mantles in the form of ice, from prestellar core collapse to disk formation. We want to investigate the need for grain surface reactions and compare our results with observed abundances, column densities, and ice-mantle compositions. Methods. We use a 2-dimensional hydrodynamical simulation as a physical model from which we take the density, temperature, and the flow of the gas. Trace particles, moving along with the gas, are used to follow the chemistry during prestellar core collapse and disk formation. We calculated abundance profiles and column densities for various species. The evolution of these abundances and the composition of ices on grain mantles were compared to observations and we tested the influence of grain surface reactions on the abundances of species. We also investigated the initial abundances to be adopted in more detailed modeling of protoplanetary disks by following the chemical evolution of trace particles accreting onto the disk. Results. Fractional abundances of HCO + , N 2 H + , H 2 CO, HC 3 N, and CH 3 OH from our model with grain surface reactions provide a good match to observations, while abundances of CO, CS, SO, HCN, and HNC show better agreement without grain surface reactions. The observed mantle composition of dust grains is best reproduced when we include surface reactions. The initial chemical abundances to be used for detailed modeling of a protoplanetary disk are found to be different from those in dark interstellar clouds. Ices with a binding energy lower than about 1200 K sublimate before accreting onto the disk, while those with a higher binding energy do not.