Islam is the majority religion in Algeria. The vast majority of citizens are Sunni Muslims belonging to Maliki school of jurisprudence, with a minority of Ibadi, most of whom live in the M'zab Valley region. Islam provides the society with its central social and cultural identity and gives most individuals their basic ethical and attitudinal orientation. Orthodox observance of the faith is much less widespread and steadfast than is identification with Islam. There are also Sufi philosophies which arose as a reaction to theoretical perspectives of some scholars.
Islam was first brought to Algeria by the Umayyad dynasty following the invasion of Uqba ibn Nafi, in a drawn-out process of conquest and conversion stretching from 670 to 711. The native Berbers were rapidly converted in large numbers, although some Christian and probably pagan communities would remain at least until Almoravid times. However, as in the Middle East itself, they sought to combine their new Islam with resistance to the Caliphate's foreign rule - a niche which the Kharijite and Shiite "heresies" filled perfectly. By the late 8th century, most of Algeria was ruled by the Rustamids, who professed the strictly puritanical but politically moderate Ibadhi sect and saw the Caliphs as immoral usurpers. They were destroyed by the Shia Fatimids in 909, but their doctrine was re-established further south by refugees whose descendants would ultimately found the towns of the M'zab valley in the Algerian Sahara, where Ibadhism still dominates.