The first computer bug really was a moth, but 'bug' is much older.
In about 1947, operators of Harvard's Mark II computer found a moth stuck in a relay and taped it into their log book: 'First actual case of bug being found.' It is a great story, often retold by Grace Hopper. But the joke only worked because engineers, including Thomas Edison, had been saying 'bugs' for decades.
The moth is real. While Grace Hopper was working on Harvard's Mark II and Mark III machines, operators tracing an error in the Mark II found an insect trapped in one of its electromechanical relays. They removed it and taped it into the log book beside a note calling it the first actual case of a bug being found. Hopper, a US Navy computer pioneer who later became a rear admiral, popularised the story.
Look closely at the wording, though. 'First actual case' only makes sense as a pun. The operators, including William Burke, seem to have been joking about a word they already used. Engineers had called faults 'bugs' since at least the 1870s. In an 1878 letter, Thomas Edison described how, after an invention's first burst of intuition, 'bugs', as he said such little faults were called, appear and take months of work to overcome. Soldiers in the Second World War spoke of bugs and glitches in their gear, and Isaac Asimov used the word for a misbehaving robot in a 1944 story.
The word has since outlived the machines. Software bugs are now a vast cost: a 2002 study for the US National Institute of Standards and Technology estimated they cost the American economy about 59 billion dollars a year. Some have been deadly, such as faults in the Therac-25 radiation therapy machine in the 1980s. One of its bugs only appeared when an operator typed very fast, so it never showed up in testing.
Finding them has always been part of the job. The computing pioneer Maurice Wilkes recalled realising in the late 1940s that a good part of the rest of his life would be spent finding errors in his own programs. Every programmer since has known the feeling.
Source: Wikipedia — Software bug · Text summarised from Wikipedia (CC BY-SA 4.0)