Private libraries appeared during the late Roman republic. Having large quantities of books signified wealth. What few illiterate owners there were back then, scarcely them. They displayed the scrolls in bookcases of citrus wood inlaid with ivory.
Before the printing press, books were kept in small boxes, which owners carried with them. As manuscript volumes accumulated, they were stored in cupboards. Later the doors were discarded, which began to look like the bookshelves we know today.
As time went on printing would be invented and it would become much more common for the average person to be able to read. The printing press allowed the title to be printed on the spine. This allowed for books to be organized on shelves in the fashion we are used to now.
The oldest existing bookcases are at Oxford University, built in the sixteenth century. These are the earliest examples of shelved galleries over flat walls.