A gymnasium, also known as a gym, is a covered location for athletics. The word is derived from the ancient Greek term "gymnasium". They are commonly found in athletic and fitness centres, and as activity and learning spaces in educational institutions.
Going to gym can cause mixing with women with out proper Hijab and can cause you hearing sinful music. If there are no women at all and no sinful music,b then it is allowed to go to the gym. Headphones cover your ears up to some extent but still you will be able to hear. You must be sure that you are away from listening sinful music.
If in a certain time, there are no women at all in the gym, Muslim man can go to the gym during that time only.
Any work or act or business which causes or leads or results in sinful act is not permissible. Making both genders mix with out proper Hijab is not allowed in Islam. Usually gyms in western countries don't care for proper Hijab, that it why Muslim should not indulge himself in such business which can lead to sinful acts, otherwise he will be responsible and his income from such sinful business will not be lawful.
There are many shar'i concerns regarding a mixed gym, for both males and females.
The dressing of men and women are not within the shar'i boundaries, and any male or female will without choice see someone of the opposite gender with bare minimum clothing, or in provocative positions that for us could be considered immodest in public. This could lead to lustful gaze, and other things that entail haram.
If you are able to avoid all of this, and avoid doing things that would be against a Muslim woman's public modesty, then you can go. This means you would need to go at a time where there are no men at all.
Alternatively, you can go to a women only gym, or purchase gym equipment and use at your own discretion at home.
And Allah knows best.