The history of Málaga, shaped by the city's location in southern Spain on the western shore of the Mediterranean Sea, spans about 2,800 years, making it 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 their colony of Malaka ( ) (, Málaka) about 770BC. From the 6th centuryBC, it was under the hegemony of Carthage in present-day Tunisia. From 218BC, Malaca was ruled by the Roman Republic; it was federated with the Roman Empire at the end of the 1st century during the reign of Domitian. Thereafter it was governed under its own municipal code, the Lex Flavia Malacitana, which granted free-born persons the privileges of Roman citizenship.

The decline of the Roman imperial power in the 5th century led to invasions of Hispania Baetica by Germanic peoples, who were opposed 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 , which lasted until 624, when the Byzantines were expelled from the Iberian Peninsula. After the Muslim conquest of Spain (711–718), the city, then known as Mālaqah (), 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.

The siege of Mālaqa by Isabella and Ferdinand in 1487 was one of the longest of the Reconquista. The Muslim population was punished for its resistance by enslavement or death. Under Castillian domination, churches and convents were built outside the walls to unite the Christians and encourage the formation of new neighborhoods. In the 16th century, the city entered a period of slow decline, exacerbated by epidemics of disease, several successive poor food crops, floods, and earthquakes.

With the advent of the 18th century the city began to recover some of its former prosperity. For much of the 19th century, Málaga was one of the most rebellious cities of the country, contributing decisively to the triumph of Spanish liberalism. Although this was a time of general political, economic and social crisis in Málaga, the city was a pioneer of the Industrial Revolution on the Iberian Peninsula, becoming the first industrialised city in Spain. This began the ascendancy of powerful Málagan bourgeois families, some of them gaining influence in national politics. In the last third of the century, during the short regime of the First Spanish Republic, the social upheavals of the Cantonal Revolution of 1873 culminated in the proclamation of the Canton of Málaga on 22 July 1873. Málaga political life then was characterised by a radical and extremist tone. The federal republican (republicanismo federal) movement gained strong support among the working classes and encouraged insurrection, producing great alarm among the affluent.

A new decline of the city began in 1880. The economic crisis of 1893 forced the closing of the La Constancia iron foundry and was accompanied by the collapse of the sugar industry and the spread of the phylloxera blight, which devastated the vineyards surrounding Málaga. The early 20th century was a period of economic readjustment that produced a progressive industrial dismantling and fluctuating development of commerce. Economic depression, social unrest and political repression made it possible for petite bourgeois republicanism and the labor movement to consolidate their positions.

In 1933, during the Second Spanish Republic, Málaga elected the first deputy of the Communist Party of Spain, or Partido Comunista de España (PCE). In February 1937 the nationalist army, with the help of Italian volunteers, launched an offensive against the city under the orders of General Queipo de Llano, occupying it on 7 February. Local repression by the Francoist military dictatorship was perhaps the harshest of the civil war, with an estimated 17,000–20,000 citizens shot and buried in mass graves at the cemetery of San Rafael.

During the military dictatorship, the city experienced the rapid expansion of tourism from abroad on the Costa del Sol, igniting an economic boom in the city beginning in the 1960s. After the end of the Francoist military dictatorship, the first candidate for mayor on the ticket of the Spanish Socialist Workers Party or Partido Socialista Obrero Español (PSOE) was elected, and remained in office until 1995, when the conservative Popular Party or Partido Popular (PP) won the municipal elections and have governed since.

Prehistory and antiquity The territory now occupied by the Province of Málaga has been inhabited since prehistoric times, as evidenced by the cave paintings of the Cueva de la Pileta (Cave of the Pool) in Benaoján, artefacts found at sites such as the Dolmen of Menga near Antequera and the Cueva del Tesoro (Treasure Cave) near Rincón de la Victoria, as well as the pottery, tools and skeletons found in Nerja. Paintings of seals from the Paleolithic and post-Paleolithic eras found in the Nerja Caves and attributed to Neanderthals may be about 42,000 years old and could be the first known works of art, according to José Luis Sanchidrián of the University of Córdoba.

Phoenician Malake

The first colonial settlement in the area, dating from around 770BC, was made by seafaring Phoenicians from Tyre, on an islet in the estuary of the River Guadalhorce at Cerro del Villar (the coastline of Málaga has changed considerably since that time, as river silting and changes in river levels have filled the ancient estuary and moved the site inland).

Although the island was ill-suited for habitation, it is likely the Tyrians chose to settle it because of its strategic location, the possibilities for trade, and the excellent natural harbor. Sailboats heading towards the Strait of Gibraltar would have found protection there from powerful sea-currents and strong westerly winds. From Cerro del Villar, the Phoenicians began trading with coastal indigenous villages and the small community at present-day San Pablo near the mouth of the river Guadalmedina. Gradually the center of commerce was moved to the mainland and the new trading colony of Malake was founded, which was from the 8th century BC a vibrant commercial center.

The Phoenician colonial period lasted approximately from 770 to 550BC. Economic development in the colony at Cerro del Villar, later to become Malaka, included industries for the production of sea salt and of purple dye. The Phoenicians had discovered in the waters off the coast murex sea snails, the source of the famed Tyrian purple. As in other Phoenician colonies, Malaka's urban development was associated with religious devotion, demonstrated by the centrality accorded the religious emporium or temple before Malaka's urban core was laid out in this section of the city.

The dominance of the Phoenicians as a Mediterranean trading power waned after Babylonian king Nebuchadnezzar finally took the city of Tyre, sometime around 572BC, after a lengthy series of campaigns. According to the Cyropaedia, Xenophon's mostly fictitious biography of Cyrus the Great, the founder of the Achaemenid Empire conquered Phoenicia in 539BC, but many scholars believe this information is dubious. While the influence of the Phoenicians declined, it did not disappear entirely in the western Mediterranean, however, as their place was taken by the Carthaginians, whose capital city of Carthage had been founded as a Phoenician trading outpost in 814BC. It is likely that much of the Phoenician population migrated to Carthage and other colonies following the Persian conquest. Having gained independence around 650BC, Carthage soon developed its own considerable mercantile presence in the Mediterranean.

Greek Mainake

The Phoenician settlements were more densely concentrated on the coastline east of Gibraltar than they were further up the coast. Market rivalry had attracted the Greeks to Iberia, who established their own trading colonies along the northeastern coast before venturing into the Phoenician corridor. They were encouraged by the Tartessians, who may have desired to end the Phoenician economic monopoly. Herodotus mentions that around 630 BC, the Phocaeans established relations with King Arganthonios (670–550 BC) of Tartessos, who gave them money to build walls around their city. Later they founded Mainake (, Mainákē) on the Málaga coast (Strabo. 3.4.2).

Dominion of Carthage Nebuchadnezzar II had recaptured mainland Tyre and Sidon from Amasis II sometime between 572 and 562BC, with the intention of appropriating the rich Tyrian trade. After his successful siege of Tyre, and with the transition to Carthaginian domination of the western Mediterranean, Malaka became in 573BC a colony of the Punic empire of Carthage, which sent its own settlers. The mercantile nature of the city, which developed during Phoenician rule, had taken hold, as well as such idiosyncratic cultural features as the religious cults devoted to the gods Melqart and Tanit.

The second half of the sixth century BC marks the transition between the Phoenician and the Punic periods of Málaga.

When the Phoenician city-states of the eastern Mediterranean were assimilated into the Persian empire in the 6th century BC, Carthage took advantage of their diminishing control over maritime trade. For two hundred years the Phoenician settlements had maintained close relationships with the "mother cities" on the coast of Syria and Lebanon, but from the mid-6th century, these connections shifted to the north African city of Carthage as it expanded its hegemony. With the arrival of the Magonid dynasty around 550 BC, Carthaginian foreign policy seems to have changed dramatically. Carthage now took the lead, establishing itself as the dominant Phoenician military power in the western Mediterranean. Although a Punic-Etruscan fleet of 120 ships was defeated by a Greek force of Phocaean ships in the naval Battle of Alalia between 540 BC and 535 BC, and Carthage lost two more major naval battles with Massalia, it still managed to close the Strait of Gibraltar to Greek shipping and thus contained the Greek expansion in Spain by 480 BC.

Carthage proceeded to destroy Tartessos and to drive the Greeks from southern Iberia. It defended its trade monopoly in the western Mediterranean vigilantly, attacking the merchant ships of its rivals. During the 3rd century BC, Carthage made Iberia the new base for its empire and its campaigns against the Roman Republic. Although they had little influence in the hinterland behind the coastal mountains, the Carthaginians occupied most of Andalusia, expanding along the northern Mediterranean coast and establishing a new capital at Cartagena.

The Romans conquered the city as well as the other regions under the rule of Carthage after the Punic Wars in 218BC.

Roman Malaca