Much of our knowledge of galaxies comes from analysing the radiation emitted by their stars, which depends on the present number of each type of star in the galaxy. The present number depends on the stellar initial mass function (IMF), which describes the distribution of stellar masses when the population formed, and knowledge of it is critical to almost every aspect of galaxy evolution. More than 50 years after the first IMF determination, no consensus has emerged on whether it is universal among different types of galaxies. Previous studies indicated that the IMF and the dark matter fraction in galaxy centres cannot both be universal, but they could not convincingly discriminate between the two possibilities. Only recently were indications found that massive elliptical galaxies may not have the same IMF as the Milky Way. Here we report a study of the two-dimensional stellar kinematics for the large representative ATLAS 3D sample of nearby early-type galaxies spanning two orders of magnitude in stellar mass, using detailed dynamical models. We find a strong systematic variation in IMF in early-type galaxies as a function of their stellar mass-to-light ratios, producing differences of a factor of up to three in galactic stellar mass. This implies that a galaxy's IMF depends intimately on the galaxy's formation history.