Perpignan has been inhabited since Roman times, but was founded at the start of the 10th century. Soon Perpignan became the capital of the counts of Roussillon. In 1172 Count Girard II bequeathed his lands to the Counts of Barcelona. Perpignan acquired the institutions of a partly self-governing commune in 1197. French feudal rights over Roussillon were given up by Louis IX in the Treaty of Corbeil (1258).
When James I, the Conqueror, king of Aragon and count of Barcelona, founded the Kingdom of Majorca in 1276, Perpignan became the capital of the mainland territories of the new state. The succeeding decades are considered the golden age in the history of the city. It prospered as a centre of cloth manufacture, leather work, goldsmiths' work, and other luxury crafts. King Philip III of France died there in 1285, as he was returning from his unsuccessful crusade against the Aragonese Crown.
In 1344 Peter IV of Aragon annexed the Kingdom of Majorca and Perpignan once more became part of the County of Barcelona. A few years later it lost approximately half of its population to the Black Death. It was attacked and occupied by Louis XI of France in 1463; a violent uprising against French rule in 1473 was harshly put down after a long siege, but in 1493 Charles VIII of France, wishing to conciliate Castile in order to free himself to invade Italy, restored it to Ferdinand II of Aragon.
Again besieged and captured by the French during the Thirty Years' War in September 1642, Perpignan was formally ceded by Spain 17 years later in the Treaty of the Pyrenees, and from then on remained a French possession.