The city was founded on opposite banks of the Fez River by Idris I in 789 and his son Idris II continued the work in 810. The first was the founder of the Idrisid dynasty, his son was born after he was assassinated. During Yahya ibn Muhammad's rule the Kairouyine mosque, one of the oldest and largest in Africa, was built, and the associated University of Al-Karaouine was founded in 859. Arab emigration to Fes, mostly from al-Andalus after a rebellion which took place in Cordoba in 818 and from Tunisia after another rebellion that took place in 824, gave the city a definite Arab character. 'Adwat al-Andalus and 'Adwat al-Qarawiyyin, the two main quarters of Fes, were called respectively after the two waves of Arab immigrants to the new city. After Ali ibn Umar (Ali II) came to power, the tribes of Madyuna, Gayatha and Miknasa, which were Sufrite Kharijites, formed a common front against the Idrisid and defeated Ali's armies and occupied Fes. Yahya ibn Al-Qassim, drove the Sufrites out of the city and declared himself Ali's successor.
The city was populated by Muslims from elsewhere in North Africa, the Middle East, Moriscos, as well as many Jews, who had their own quarter, or Mellah, in the city.