Online Etymology Dictionary reports that loony bin is less than 100 years old:
Slang loony bin "insane asylum" is from 1919.
However, a Google Books search finds one instance from a decade earlier. From P.G. Wodehouse, Mike and Psmith (1909):
“Nothing that happens in this loony bin,” said Psmith, “has power to surprise me now. There was a time when I might have thought it a little unusual to have to leave the house through a canvas chute at one o'clock in the morning, but I suppose it's quite a regular thing here. Old school tradition, etc. ..."
Wodehouse also accounts for the second-earliest Google Books match, albeit in the variant form "looney-bin." From P.G. Wodehouse, Indiscretions of Archie (1921):
Archie proceeded on his way, still mystified. Then he reflected that artists as a class were all pretty weird and rummy and talked more or less consistently through their hats. You couldn't ever take an artist's opinion on a picture. Nine out of ten of them had views on Art which would have admitted them to any looney-bin, and no questions asked. He had met several of the species who absolutely raved over things which any reasonable chappie would decline to be found dead in a ditch with.
I haven't been able to identify the 1919 occurrence that Etymonline has in mind. However, Eric Partridge, A Dictionary of Slang and Unconventional English (1937) has this entry for the phrase:
loony-bin. A lunatic asylum : Cockneys' : from ca. 1890. Ex loony, [definition] 2 [namely, "a fool, a lunatic"].
In the term loony bin, bin seems to refer simply to a holding-chamber, box, or other container. Partridge may be right that the phrase "loony-bin" goes back to the late nineteenth century in England, but he provides no instances of it in print from that period or later. Wodehouse may be the person most responsible for its spread to general usage throughout the English-speaking world.