Under the name of Karalis it was one of a string of Phoenician trading colonies in Sardinia, including Sulcis, Nora, and Tharros, that were founded from Tyre in the 7th century BC. It passed with the rest of the island first to the control of Carthage and then to Rome in 238 BC when the Romans defeated the Carthaginians.
Subsequently ruled in turn by the Vandals and the Byzantine Empire, it became the eponymous capital of an independent kingdom or giudicato, ruled by a giudice or judike (literally "judge"). However, there is some evidence that during this period of independence from external rule, Cagliari was deserted because it was too exposed to attacks by Moorish pirates from the sea. Apparently many people left Cagliari and founded a new town (named Santa Igia) in an area close to the Santa Gilla swamp on the west of Cagliari, but distant from the sea. The "giudicato" of Karalis included a large area of the Campidano plain, the mineral resources of the Sulcis region and the mountain region of Ogliastra. Apart from Karalis, there were three more independent and autonomous kingdoms, or "giudicati", in Sardinia: Torres in the north west, Gallura in the north east, and the most famous and long-lived Arborea, with Oristano as capital.
During the 11th century, the Pisan republic which had previously seized the Sulcis region in the south east, conquered the kingdom of Karalis and re-built the town of Cagliari. Pisa was one of the four Italian "maritime republics" that during the middle ages fought for control of the Mediterranean sea and its commercial routes. The other maritime republics were the short-lived Amalfi, Genoa and Venice. Pisa and Genoa had a keen interest in Sardinia because it was a perfect strategic base for controlling the commercial routes between Italy and North Africa.
Some of the fortifications that still surround the current district of Castello (Casteddu 'e susu in the Sardinian language) were built by the Pisans, most notably the two remaining white limestone towers designed by architect Giovanni Capula (originally there were three towers that guarded the three gates that gave access to the district). Together with the district of Castello, Cagliari includes the districts of Marina (with the port), Stampace and Villanova. Marina and Stampace were guarded by walls, while Villanova, which mainly hosted peasants, was not.
During the 14th century the kingdom of Aragon conquered Cagliari after a battle against the Pisans and advanced its plan to conquer all of Sardinia. When Sardinia was finally conquered by Aragon, Cagliari (during the Catalan domination the city was named Càller), became the administrative capital of the vice-kingdom of Sardinia, which later came under the rule of the Spanish empire. Many agree that the Spanish domination was a period of decadence for Cagliari and Sardinia.