Ibn Khaldūn gives a detailed account of his education, listing the main books he read and describing the life and works of his teachers. He memorized the Qurʾān, studied its principal commentaries, gained a good grounding in Muslim law, familiarized himself with the masterpieces of Arabic literature, and acquired a clear and forceful style and a capacity for writing fluent verse that was to serve him well in later life when addressing eulogistic or supplicatory poems to various rulers. Striking by their absence are books on philosophy, history, geography, or other social sciences; this does not mean that he did not study these subjects—scholars know that he wrote summaries of several books by the 12th-century Arab philosopher Averroës—but it is to be presumed that Ibn Khaldūn acquired most of his very impressive knowledge in these fields after he had completed his formal education.
This came at age 20, when he was given a post at the court of Tunis, followed three years later by a secretaryship to the sultan of Morocco in Fez (Fès). By then he was married. After two years of service, however, he was suspected of participation in a rebellion and was imprisoned. Released after nearly two years and promoted by a new ruler, he again fell into disfavour, decided to leave Morocco, and crossed over to Granada, for whose Muslim ruler he had done some service in Fez and whose prime minister, the brilliant writer Ibn al-Khaṭīb, was a good friend. Ibn Khaldūn was then 32 years old.
The following year Ibn Khaldūn was sent to Sevilla to conclude a peace treaty with Pedro I of Castile. There he saw “the monuments of my ancestors.” Pedro “treated me with the utmost generosity, expressed his satisfaction at my presence and showed awareness of the preeminence of our ancestors in Sevilla.” Pedro even offered him a post in his service, promising to restore his ancestral estates, but Ibn Khaldūn politely declined. He gladly accepted the village that the sultan of Granada bestowed on him, however, and, feeling once more secure, brought over his family, whom he had left in safety in Constantine. But, to quote him once more, “enemies and intriguers” turned the all-powerful prime minister, Ibn al-Khaṭīb, against him and raised suspicions regarding his loyalty; it can be conjectured that the task of these enemies must have been greatly facilitated by the apparent jealousy between the two most brilliant Arab intellectuals of the age. Once more, Ibn Khaldūn found it necessary to take his leave, and he returned to Africa. The following 10 years saw him change employers and employment with disconcerting rapidity and move from Bejaïa (Bougie) to Tilimsān (Tlemcen), Biskra, Fez, and once more to Granada, where he made an unsuccessful effort to save his old rival and friend, Ibn al-Khaṭīb, from being killed by order of its ruler.
During this period Ibn Khaldūn served as prime minister and in several other administrative capacities, led a punitive expedition, was robbed and stripped by nomads, and spent some time “studying and teaching.” This extreme mobility is partly explained by the instability of the times. The Almohad Empire, which had embraced the whole of North Africa and Muslim Spain, had broken down in the middle of the 13th century, and the convulsive process from which Morocco, Algeria, and Tunisia were subsequently to emerge was under way; wars, rebellions, and intrigues were endemic, and no man's life or employment was secure. But in Ibn Khaldūn's case two additional factors might be suspected—a certain restlessness and a capacity to make enemies, which may account for his constant complaints about the “intriguers” who turned his employers against him.