The history of Málaga, shaped by the city's location in the south of Spain on the western shore of the Mediterranean Sea, spans about 2,800 years, making Málaga one of the oldest cities in the world. The first inhabitants to settle the site may have been the Bastetani, an ancient Iberian tribe. The Phoenicians founded the colony of Malaka here about 770 BC, and from the 6th century BC it was under the hegemony of ancient Carthage in north Africa. From 218 BC the city was ruled by the Roman Republic and then at the end of the 1st century during the reign of Domitian it was federated with the Roman Empire as Malaca (Latin). Thereafter it was governed under its own municipal code of law, the Lex Flavia Malacitana, which granted free-born persons the privileges of Roman citizenship.
The Romanization of Málaga was, as in most of southern Hispania Ulterior, effected peacefully through the foedus aequum; a treaty recognizing both parties as equals, obligated to assist each other in defensive wars or when otherwise summoned. During this period, under the rule of the Roman Republic, the Municipium Malacitanum became a transit point on the Via Herculea, revitalising the city both economically and culturally by connecting it with other developed enclaves in the interior of Hispania and with other ports of the Mediterranean Sea.
The decline of the Roman imperial power in the 5th century led to invasions of Hispania Baetica by Germanic peoples and by the Byzantine Empire. In Visigothic Spain, the Byzantines took Malaca and other cities on the southeastern coast and founded the new province of Spania in 552. Malaca became one of the principal cities of the short-lived Byzantine Provincia Spaniae (Latin); it lasted until 624, when the Byzantines were expelled from the peninsula. After the Muslim Arab conquest of Hispania (711–718), the city, then known as Mālaqa (مالقة), was encircled by walls, next to which Genoese and Jewish merchants settled in their own quarters. In 1026 it became the capital of the Taifa of Málaga, an independent Muslim kingdom ruled by the Hammudid dynasty in the Caliphate of Córdoba, which existed for four distinct time-periods: from 1026 to 1057, from 1073 to 1090, from 1145 to 1153 and from 1229 to 1239 when it was finally conquered by the Nasrid Kingdom of Granada.