The Phoenicians established a small trading post in the now-upmarket suburb of Anfa in the 6th century BCE. In the 7th century AD, Anfa became a regional capital under the Barghawata, a confederation of Berber tribes. The Almohads destroyed it in 1188, and 70 years later, the Merenids took over.
In the early 15th century the port became a safe haven for pirates and racketeers. Anfa pirates became such a serious threat later in the century that the Portuguese sent 50 ships and 10,000 men to subdue them, leaving Anfa in a state of ruins. However, the local tribes continued to terrorise the trade routes, provoking a second attack by the Portuguese in 1515. Sixty years later the Portuguese arrived to stay, erecting fortifications and renaming the port Casa Branca (White House).
The Portuguese abandoned the colony in 1755 after a devastating earthquake severely damaged the walls of Casa Branca. Sultan Sidi Mohammed Ben Abdullah subsequently resettled and fortified the town, but it never regained its former importance. By 1830, there were only 600 or so inhabitants.
By the mid-1800s, Europe was booming and turned to Morocco for increased supplies of grain and wool. The fertile plains around Casablanca were soon supplying European markets, and agents and traders flocked back to the city. Spanish merchants renamed the city Casablanca and by the beginning of the 20th century the French had secured permission to build an artificial harbour.
Increased trade brought prosperity to the region, but the activities and influence of the Europeans also caused resentment. Violence erupted in 1907 when Europeans desecrated a Muslim cemetery. The procolonialist French jumped at the chance to send troops to quell the dispute; a French warship and a company of marines soon arrived and bombarded the town. By 1912, it was part of the new French protectorate.