Gilgamesh sits at an unusual point: he is a legendary hero who may also be a real king, and the two kinds of evidence are separate.
The Sumerian King List, a document of the early second millennium BCE that records dynasties with reign lengths running into tens of thousands of years for the earliest, names Gilgamesh as a ruler of Uruk in its first dynasty and gives him a hundred and twenty-six years. The list is not a chronicle; it is a political construction asserting that kingship passed from one city to another, and its numbers are unusable. But it names him among rulers whose existence is otherwise attested, and other early sources treat him as a former king. An inscription of a later ruler of Lagash refers to him. If he was real he belongs to roughly the twenty-seventh century BCE, and nothing written by or for him survives.
The poem is a separate and better-documented thing. There are independent Sumerian poems about Bilgames from the late third and early second millennium. The Akkadian epic is a deliberate literary composition assembled from that material, existing in an Old Babylonian version of about 1800 BCE and in the standard version of roughly the thirteenth to eleventh centuries, ascribed in antiquity to a scribe named Sin-leqi-unninni, one of the earliest named authors in world literature.
The bulk of the standard text comes from the seventh-century library of Ashurbanipal at Nineveh, excavated in the 1850s. It is incomplete. New fragments are still being identified, including substantial additions published in the last two decades, and the poem is not a fixed object.