I don’t mean going to train for combat in the military barracks or training for a martial art; I mean who were the first civilizations that had people deliberately going to “gymnasiums” to better their physique or to improve their strength?
Well that depends on what you mean by "people" and "hit the gym". The benefits of training have been known for pretty much all of recorded history. It's pretty self evident, pratice something a lot and you'll get good at it. This, along with a basic human fascination for the extraordinary, lead to feats of strength being popularized in almost all cultures around the world. The chinese army had standardized strength tests since at least the Tang dynasty (700 CE), some greeks certainly worked out. You'll find the story of Milo of Croton repeated ad nauseum in the FAQ section about strength training along with a bunch of answers to questions like "how did knights/hoplites/legionnaires/etc. etc. work out?".
However, let's first sort out what you mean by people. Because some people have worked out for a long time. Generally the leisure classes and generally for the purpose of some specific competition. A well known example is the olympics. However, just because a small number of leisure class greek citizens worked out when they were young doesn't mean that the average greek farmer or craftsman would put down his hoe or hammer and hit the gymnasium after a long day of work for aesthetic purposes. So, if by "people" you mean average joes with no aspirations of competing on the highest levels, we're going to have to fast forward quite a bit.
The second part we need to deal with is what you mean by "hit the gym". Professional soldiers generally worked out. Mithraic/Zoroastrian worshippers had "houses of strength that combined training and religions rituals, athletes worked out to preform better in competitions but these men (mostly) were not the general population. Training was also functional rather than aesthetical. Wrestlers did seek to improve their strength but not to look good on the beach but to be better wrestlers. Professional soldiers trained too but again, to get better at their chosen profession. So if by "hit the gym" you mean "start working out for the aesthetic or health purposes", we're going to have to move skip ahead again.
To when? Well that depends. What I interpret "hit the gym" to mean is generally to work out for the betterment of your health or aesthetics in some form. Most people would probably take "hit the gym" to refer to strength training although it could just as well be a treadmill or a yoga class really. However, I'm going to interpret your question to be something along the lines of "when did strength workout for health or aesthetic purposes gain mainstream popularity in the 'west' (because I know jack shit about the history of strength training in any other region)".
The answer is, it started some time in the 1930's and the man who popularized it was Jack Lalanne (or Francois Henri Lalanne which is his real name). Lalanne was in turn inspired by Paul Bragg, an early american health food advocate. However, it was Lalanne who created and popularized the gym we know it today, opening the first health club in 1936 in Oakland, CA. Now, it's not like people didn't know that exercise was good for you before that but the concept of muscle building was often thought to lead to adverse health effects. Lalanne knew this wasn't true. He also understood the relationship between diet, exercise and overall health,
Lalanne did public demonstrations, taking in volunteers of all shapes and sizes and proving that his system of diet and weight lifting really worked. On a basic level Lalannes system is pretty much unchanged today. Don't eat excessive amounts of sugar, eat more veggies, lift weights and you will be stronger and slimmer than if you don't do these things. It's not exactly revolutionary today but in the 30's and 40's in was much less well understood. Doctors at the time even cautioned their patients to avoide Lalannes clubs saying that "LaLanne was an exercise 'nut,' whose programs would make them 'muscle-bound' and cause severe medical problems."
Lalanne also designed the first of several machines we today consider staples of any gym. The smith machine, the cable cross, the leg extension machine and the weight selectors you find on almost any machine that doesn't rely on barbell plates. He also encouraged women to lift even though this was met with a great deal of skepticism (and still is, for some reason the notion that lifting heavy will magically make women look masculine hasn't died yet).
Lalanne was also a very charismatic person. He got his first television show in 1953, The Jack Lalanne Show, it would run for 34 years. Lalanne was known for using everyday object to teach people the basics of working out in their homes, and for promoting his own health club chain which would grow to over 200 establishments. He also published numerous books, videos, even records. He preformed numerous feat of strengths, often related to swimming (like swimming from Alcatraz to the shore handcuffed) and doing 1000 chin-ups to promote his show. The man understood marketing.
So if Lalanne can be said to have popularized the gym and working out for aesthetic and health purposes, who popularized bodybuilding? You guessed. Arnold. Now, Arnold was by no means the first bodybuilder but he was certainly the most famous. The moving Pumping Iron (1977) remains a cult work in the fitness community and his continued career in acting brought with it a wave of hypermasculine ideals. This, couple with the Olympics switching from overhead press to flat bench press in 1972 and the rising popularity of powerlifting, helped create the "big three" (flat bench press, deadlift and squats) as the stereotypical workout. It's always a little hard to pin culture shifts to a specific event or person. Powerlifting was already gaining popularity before Arnold came along, bench press existed long before the switch in the olympics and hypermasculine ideals existed long long before Pumping Iron came out but once a culture starts shifting, it tends to be several mutually reinforcing trends that work in tandem.
So, when did strength workout for health or aesthetic purposes gain mainstream popularity? The 30 to the 70's and the trend has not slowed down yet.
I left out a lot here, like the cold war competition in powerlifting between the US and the USSR, the rise of anabolic steroids, health scares, fitness crazes, the spandex duo Jane Fonda and Richard Simmons etc etc.
"Dying is easy. Living is a pain in the butt. It's like an athletic event. You've got to train for it. You've got to eat right. You've got to exercise. Your health account, your bank account, they're the same thing. The more you put in, the more you can take out. Exercise is king and nutrition is queen: together, you have a kingdom." - Jack Lalanne