Immigration has rapidly changed the demographic profile of most European societies, increasing ethnic, linguistic and religious diversity. A large literature based mainly on observational evidence claims that high levels of ethnic diversity undermine cooperation and the provision of public goods in modern societies. Yet, experimental tests of the proposition that ethnic diversity leads to a lower capacity to solve collective action problems have produced inconclusive results. Moreover, it is unclear whether the mechanisms that are known to be effective in stabilizing cooperation in homogeneous groups work similarly in heterogeneous groups. We conducted a public goods experiment with Italian and immigrant residents of the metropolitan city of Milan to examine the effects of diversity on cooperation. Results show that cooperation is initially lower in mixed than in homogeneous Italian groups because of both compositional as well as contextual effects. We also find that one of the most frequently citied mechanisms sustaining high-levels of cooperation in homogeneous groups - peer sanctioning - is less effective in bringing about cooperation in some heterogeneous groups but not others, calling for a more careful examination of the composition of diverse groups and more generally, the mechanisms that can restore cooperation in the presence of diversity.