How populations adapt to their environment is a fundamental question in biology. Yet, we know surprisingly little about this process, especially for endangered species, such as nonhuman great apes. Chimpanzees, our closest living relatives, are particularly notable because they inhabit diverse habitats, from rainforest to woodland-savannah. Whether genetic adaptation facilitates such habitat diversity remains unknown, despite it having wide implications for evolutionary biology and conservation. By using newly sequenced exomes from 828 wild chimpanzees (388 postfiltering), we found evidence of fine-scale genetic adaptation to habitat, with signatures of positive selection in forest chimpanzees in the same genes underlying adaptation to malaria in humans. This work demonstrates the power of noninvasive samples to reveal genetic adaptations in endangered populations and highlights the importance of adaptive genetic diversity for chimpanzees. Adaptation to different environments can include responding to a myriad of pressures, from diseases to differences in water abundance. Of the great apes, chimpanzees are most similar to humans in that they inhabit a range of environments from savannahs to rainforests. Ostridge et al. sequenced exomes from 388 chimpanzees using fecal samples to investigate how selection has acted on these animals. Signatures of selection differed by environment, with forest-dwelling chimpanzee populations bearing variants in genes associated with disease resistance. This study demonstrates the utility of environmentally collected DNA in an endangered species and provides insights into adaptation in our closest living relative. ?Corinne Simonti