Why are some animals socially gregarious while others keep to themselves? Evolutionary models of gregarious behavior typically treat benefits and costs of social interaction qualitatively. Some of these benefits and costs are contagious: for example, socializing may allow individuals to share useful information with their neighbors, but also expose them to dangerous infectious diseases. Here, we present a model the evolution of sociality in the presence of beneficial and costly social contagion processes. We characterize a socially optimal level of social interaction, and show that evolutionary dynamics produce a social dilemma: individuals maximizing their fitness drive the population to a level of sociality at which all individuals are worse off. In some cases, social behavior can disappear entirely – even when any level of socializing would be advantageous for the species as a whole.