Overexploitation of forests by humans can lead to highly fragmented populations of forest-dependent species that have poor dispersal abilities. We tested the influence of habitat quality, landscape structure and human pressure on densities of the Barbary macaque Macaca sylvanus in the largest wild population, in the Middle Atlas of Morocco. We surveyed 14 forest fragments of 5–142 km2 that are separated from each other by an inhospitable matrix. We estimated the habitat quality of these forest patches by analysing the vegetation structure and the intensity of human pressure. We studied the distribution of forest patches and estimated macaque densities by combining line-transect sampling and complete group counts. We estimated mean density of individuals to be 9 km−2 (range 0.2–23 km−2). Differentiation of forest patches by a principal component analysis did not show any significant relationship between vegetation type and macaque density. A linear regression model showed that human pressure had a negative impact on density and that density responded positively to patch size. Patch shape, connectivity and altitude did not explain variation in population density. The size of this population is estimated to be c. 5,000 individuals. The survival of several small subpopulations is seriously threatened. As the Middle Atlas is the stronghold of this Endangered species, we recommend modifying forestry practices, reducing overgrazing by livestock within forests, and halting clear-cutting of holm oaks.