To overcome stress, such as resource limitation, an organism often needs to successfully mediate competition with other members of its own species. This may favor the evolution of defective traits that are harmful to the species population as a whole, and that may lead to its dilution or even to its extinction (the tragedy of the commons). Here, we show that this phenomenon can be circumvented by cooperation plasticity, in which an individual decides, based on environmental conditions, whether to cooperate or to defect. Specifically, we analyze the evolution of density-dependent cooperation. In our model, the population is spatially subdivided, periodically remixed, and comprises several species. We find that evolution pushes individuals to be more cooperative when their own species is at lower densities, and we show that not only could this cooperation prevent the tragedy of the commons, but it could also facilitate coexistence between many species that compete for the same resource.