Organised collections of recorded knowledge, held for use. Libraries are older than most of the writing systems that survive, their defining problem has always been finding rather than storing, and what they are for has changed more than once.

The library of Ashurbanipal at Nineveh, assembled in the seventh century BCE, is the oldest systematically organised surviving collection, holding tens of thousands of clay tablets across literature, divination, medicine and administration.
It survives because it burned. The fire that destroyed the palace baked the clay tablets hard, preserving them, which is why more Assyrian literature survives than from societies that wrote on materials that decay.
The Epic of Gilgamesh reached the modern world through this collection.
Earlier archives existed at Ebla, Mari and elsewhere, and the distinction between an archive of records and a library of works is not sharp in the earliest cases.

The Library of Alexandria, founded in the third century BCE, aimed at collecting all known writing and supported a resident community of scholars, which made it a research institution as much as a collection.
Its destruction was not a single event. Damage occurred at several points across centuries, including a fire during Caesar's intervention in 48 BCE, and the more consequential loss was the gradual dispersal of its scholars and the ending of its funding. The idea that one act of arson destroyed classical knowledge is a simplification that obscures a slower and more ordinary failure: an institution stopped being paid for.
A collection that cannot be searched is a heap. Cataloguing is therefore the central technology of libraries, and it is older than any of the systems now in use.
Callimachus produced the Pinakes at Alexandria, a catalogue of the collection organised by subject and author, which is the first known attempt at systematic bibliography.
Classification schemes assign a position in a subject order. Dewey Decimal, published in 1876, and the Library of Congress Classification are the dominant modern examples, and both encode the assumptions of their period: Dewey's original allocation of religion gave the great majority of its space to Christianity.
Subject headings and controlled vocabulary solve a different problem: the same idea described in different words. A user searching for one term should find works using another, which requires someone to decide the two are the same thing.
Card catalogues were the physical implementation for roughly a century, and their replacement by databases changed searching from browsing a fixed order to querying an arbitrary one.
Full text search changed it again, and did not remove the need for classification, because searching words finds documents containing them rather than documents about a subject.

Monastic and cathedral libraries preserved and copied texts in medieval Europe, and their copying is why classical works survive at all in the West.
Islamic libraries from the eighth century onward, including the collections at Baghdad, Córdoba and Cairo, were larger than their European contemporaries and were centres of translation as well as of preservation.
University libraries followed the universities, and books were frequently chained to desks, which records how expensive a single volume was.

Public libraries, free at the point of use and open to anyone, are a nineteenth century development. The Public Libraries Act of 1850 in Britain allowed local authorities to fund them from rates, and Andrew Carnegie funded over two thousand five hundred library buildings worldwide, generally requiring the local authority to commit to maintaining them.
The principle was contested at the time on the grounds that reading material for the working class was a private matter or an extravagance, and versions of that argument recur whenever library funding is reduced.
National and legal deposit libraries receive a copy of everything published in their jurisdiction, which makes them the collection of record.
Circulation of physical books has declined in many public library systems while visits have not fallen correspondingly, which indicates the building is being used for something other than borrowing.
That something is documented and varied: internet access for people without it at home, warmth and shelter, a place to work, children's activities, and assistance with government services that have moved online and assume digital access nobody provided.
Whether this is mission creep or the mission is genuinely disputed among librarians. The case for it is that a library has always been about access rather than about objects; the case against is that it substitutes for services that should be funded separately.
Digital lending is legally unsettled. Publishers licence rather than sell electronic books to libraries, generally with limits on loans and duration, so a library does not own its digital collection in the way it owns its physical one, and litigation over the extent of library lending rights is ongoing.
Preservation of digital material is harder than of print. Formats become unreadable, links break, and material that exists only on a platform vanishes when the platform does, which is a preservation problem without an established solution.
Libraries are the institution through which most surviving knowledge survived, and the specific mechanism was copying and cataloguing by people who were paid to do it, sustained across generations.
The Alexandria lesson is the durable one. Collections are lost less often by catastrophe than by the quiet ending of the arrangement that maintained them, which applies with some force to digital material that exists only while someone continues to pay for the servers.