The Barbary macaque (Macaca sylvanus), also known as Barbary ape or magot, is a macaque species native to the Atlas Mountains of Algeria, Libya, Tunisia and Morocco, along with a small introduced population in Gibraltar.

The Barbary macaque is of particular interest because males play an atypical role in rearing young. Because of uncertain paternity, males are integral to raising all infants. Generally, Barbary macaques of all ages and sexes contribute in alloparental care of young.

The diet of Barbary macaque consists primarily of plants and insects and they are found in a variety of habitats. Males live to around 25 years old while females may live up to 30 years. Besides humans, they are the only free-living primates in Europe. Although the species is commonly referred to as the "Barbary ape", the Barbary macaque is actually a true monkey. Its name refers to the Barbary Coast of Northwest Africa.

The population of the Barbary macaques in Gibraltar is the only one outside Northern Africa and the only population of wild monkeys in Europe. Barbary macaques were once widely distributed in Europe, as far north as Norfolk, England, from the Zanclean to the Late Pleistocene. About 300 macaques live on the Rock of Gibraltar. This population appears to be stable or increasing, while the North African population is declining.

The Barbary macaque is the only macaque species found outside Asia, and only African primate that survives north of the Sahara Desert. It lives mainly in fragmented areas of the Rif and the Middle and High Atlas mountain ranges of Morocco and the Grande and Petite Kabylie mountain region of Algeria. It has been recorded at altitudes of 400–2300 metres, though it seems to prefer higher elevations. The Moroccan and Algerian populations are around 700 km (430 mi) apart, although the gap was smaller during the Holocene.

Barbary macaques also occur in the British Overseas Territory of Gibraltar at the southern tip of Europe's Iberian Peninsula. Spanish historian from Gibraltar Alonso Hernández del Portillo noted in the early 17th century that the macaques had been present "from time immemorial". Most likely, the Moors introduced macaques from North Africa to Gibraltar during the Middle Ages. During World War II, British Prime Minister Winston Churchill ordered for more Barbary macaques to be introduced to Gibraltar to reverse population declines. Today, there are around 300 Barbary macaques in Gibraltar.

It can live in a variety of habitats, such as cedar, fir, and oak forests, grasslands, thermophilous scrub, and rocky ridges full of vegetation. The climate is Mediterranean and has seasonal extremes of temperature. In Morocco, most Barbary macaques inhabit Atlas cedar (Cedrus atlantica) forests, but this could reflect the present habitat availability rather than a specific preference for this habitat. In Algeria, the Barbary macaque is found mainly in Grande and Petite Kabylie, ranges that form part of the Tell Atlas mountain chain, but there is also an isolated population in Chréa National Park. It is found in mixed cedar and holm oak forests, humid Portuguese and cork oak forests, and scrub-covered gorges.

Fossil evidence indicates the species inhabited Southern Europe and all of North Africa. Historically, it was found across North Africa from Libya to Morocco. A Tunisian population was mentioned in the works of ancient Greek writer Herodotus (ca. 484–425 BCE), indicating the species has become extinct there within the last 2,500 years.