From antiquity, men had created; at a point they became able to distinguish the creation of ideas per se, and to value the skill of doing this. Later they learned to write and, eventually, to write down the ideas (as well as the number of ox, geese, ducks, wives and peculiarly-coloured pieces of rock or ore they had).
From this grew the library because what one man could write, another could copy, and the copy could be taken somewhere else without disturbing the information written on it, which meant that people's ideas could live on after their death. And since Knowledge is Power, one way to get power was to amass knowledge by gathering the copies together.
In antiquity, the 'famous' libraries were at Syracuse and at Alexandria; later they became a status symbol for aspirational kings, a power-house for acquisitive lords (both temporal and spiritual) because they recorded what land and wealth had been promised or given to whom, and a source of fuel in a hard winter.