The Mahabharata mentions that Vyāsa had seen the events unfolding with his divine sight and recited the great poem for the first time after the Kuru descendants had reached their final destination after death.The second time it was recited by Vaishampāyana at the snake sacrifice of Janamejaya.
The Mahabharata Swargarohana Parva Chapter 5 mentions Vyasa seeing the events of unfolding with his divine sight:
It has been composed by the ascetic Krishna, O Brahmana, of truthful speech... He has composed this, beholding everything with a celestial eye that has been cleansed (strengthened) by varied lore
The Mahabharata Adi Parva Chapter 1 says:
The son of Satyavati having, by penance and meditation, analysed the eternal Veda, afterwards composed this holy history
Formerly, the spirited and virtuous Krishna-Dwaipayana, by the injunctions of Bhishma, the wise son of Ganga and of his own mother, became the father of three boys who were like the three fires by the two wives of Vichitra-virya; and having thus raised up Dhritarashtra, Pandu and Vidura, he returned to his recluse abode to prosecute his religious exercise.
तेषु जातेषु वृद्धेषु गतेषु परमां गतिम्।
अब्रवीद्भारतं लोके मानुषेऽस्मिन्महानृषिः॥५६॥
It was not till after these were born, grown up, and departed on the supreme journey, that the great Rishi Vyasa published the Bharata in this region of mankind
Note: अब्रवीत् means spoke the Mahabharata, though the translation says published and the incorrect translation may lead to an incorrect conclusion.
Vyasa requested Lord Ganesha after composing it in his imagination and wanted a way to teach his disciples, for which he propitiates Lord Ganesha. Thus we can say he recited it to Ganesha (spoke it first after the death of Dhritarashtra, etc as per the reference above) and after doing so, he teaches his disciples, out of whom Vaishampayana recites the one meant for the humans (with one lakh verses) to Janamejaya at the snake sacrifice (Ref. Same chapter, the events may seem a little out of place because of the fashion in which they are narrated)
It took Vyasa 3 years to complete the Mahabharata
The sage Krishna-Dwaipayana regularly rising for three years, composed this wonderful history called Mahabharata - Mbh 1.62