It seems he mainly wrote it in order to explore Scandinavian myths of old, and to create a beautiful fantasy to be read. From the eNotes introduction:
Henrik Ibsen wrote Peer Gynt in 1867. He never intended that the work be performed on stage; instead, Ibsen envisioned his work as a poetic fantasy to be read. However, Peer Gynt quickly became recognized as a masterwork of Scandinavian literature, and in 1876, Ibsen adapted his work for the stage.