My only credentials are being a 50 year old U.S. native speaker and writing technical documents a peripheral part of my work. But under those credentials, I have to agree with the "Ngram Viewer," making your understanding correct.
The second version is absolutely the most common and "correct" in my view. The first version would sound like a non-native speaker.
Without delving into research and only using my experience (because I'm only doing this "on the side," as well), I project the reason for this is that the article "a" adds just a little information to the statement. The intended communication seems to be that there is no such thing as "any possible" free lunch, and the article "a" helps to communicate that.
Sometimes it helps to view things through the eyes of an opposing phrase, by changing the target part of speech just to see what happens. As such, please consider: "There is no such thing as the free lunch." That is not an American phrase, but if it were, it would imply that there is at least a concept of a single, unique "free lunch" out there that exists in the world, like a totally unique artifact to be found by Indiana Jones. :) But "no such thing" means it actually does not exist (and Indy won't find it).
So when one realizes how silly that "the" version sounds, one can more easily see that clarifying "any possible" free lunch through the use of the article "a" appears to be helpful instead of no article at all (which is your first version not "correctly" used).
https://www.ef.com/wwen/english-resources/english-idioms/
https://www.phrases.org.uk/meanings/proverbs.html
https://idioms.thefreedictionary.com/
There will be others. Good luck.