When Muslim Arabs arrived in Algeria in the mid-7th century, a large number of locals converted to the new faith. After the fall of the Umayyad Arab Dynasty in 751, numerous local Berber dynasties emerged. Amongst those dynasties were the Aghlabids, Almohads, Abdalwadid, Zirids, Rustamids, Hammadids, Almoravids and the Fatimids. The Berber people controlled much of the Maghreb region throughout the Middle Ages. The Berbers were made up of several tribes. The two main branches were the Botr and Barnès tribes, who were themselves divided into tribes, and again into sub-tribes. Each region of the Maghreb contained several tribes. All these tribes were independent and made territorial decisions.
In the early 16th century, Spain constructed fortified outposts on or near the Algerian coast. Spain took control of Mers el Kebir in 1505, Oran in 1509, and Tlemcen, Mostaganem, and Ténès, in 1510. In the same year, the merchants of Algiers handed over one of the rocky islets in their harbor, where the Spaniards built a fort. The presidios in North Africa turned out to be a costly and largely ineffective military endeavor that did not guarantee access for Spain's merchant fleet.
In 1516 the Muslim privateer brothers Aruj and Hayreddin Barbarossa, who operated successfully under the Hafsids, moved their base of operations to Algiers. When Aruj was killed in 1518 during his invasion of Tlemcen, Hayreddin succeeded him as military commander of Algiers. The Ottoman sultan gave him the title of beylerbey and a contingent of some 2,000 janissaries. With the aid of this force, Hayreddin subdued the coastal region between Constantine and Oran (although the city of Oran remained in Spanish hands until 1791).