It is not permissible to listen to music, whether in a gym or elsewhere, because of a great deal of evidence which states that it is haraam to listen to musical instruments. See the answer to question no. 5000.
The fact that there is music in the gym is an evil which must be denounced. It is not sufficient just to put on earphones to block the sound or distract oneself by listening to something permissible. Rather if denouncing it verbally does not work, there remains the option of denouncing it in one’s heart, which can only be achieved by going away from the evil and leaving the place where it is happening, so long as one is able to do that.
The Prophet (peace and blessings of Allaah be upon him) said: “Whoever among you sees an evil action, then let him change it with his hand [by taking action]; if he cannot, then with his tongue [by speaking out]; and if he cannot, then with his heart – and that is the weakest of faith.”
Narrated by Muslim (49).
The fact that it is obligatory to leave the place where evil is being committed is also indicated by the verse in which Allaah says (interpretation of the meaning):
“And it has already been revealed to you in the Book (this Qur’aan) that when you hear the Verses of Allaah being denied and mocked at, then sit not with them, until they engage in a talk other than that; (but if you stayed with them) certainly in that case you would be like them. Surely, Allaah will collect the hypocrites and disbelievers all together in Hell”
al-Qurtubi (may Allaah have mercy on him) said: The words of Allaah, “then sit not with them, until they engage in a talk other than that” mean, something other than kufr (disbelief). “(But if you stayed with them) certainly in that case you would be like them” – this indicates that it is obligatory to avoid people of sin if they are openly committing evil, because the one who does not avoid them is approving of their deeds, and approving of kufr is also kufr. Allaah says “(but if you stayed with them) certainly in that case you would be like them”, so everyone who sits in a gathering where sin is committed and does not denounce them will share the burden of sin with them.
They should be denounced if they speak of sin or commit sin; if one cannot denounce them then he should leave them so that he will not be one of those mentioned in this verse.
It was narrated that ‘Umar ibn ‘Abd al-‘Azeez caught some people who were drinking alcohol, and it was said to him that one of the people present was fasting. He had the punishment carried out on him too, and he recited this verse: “(but if you stayed with them) certainly in that case you would be like them”, i.e., approval of sin is also a sin. End quote.
Al-Jassaas said in Ahkaam al-Qur’aan (2/407): This verse indicates that it is obligatory to denounce evil to the one who does it, and part of denouncing it is showing one’s dislike of it if it is not possible to remove it, and not sitting with the one who does it, and leaving him until he stops it and does something else. End quote.
Shaykh Ibn Baaz (may Allaah have mercy on him) said: Denouncing in one's heart is obligatory for everyone, which means hating the evil and leaving the people if one is unable to change it with one's words or actions, because Allaah says (interpretation of the meaning):
“And when you (Muhammad صلى الله عليه وسلم) see those who engage in a false conversation about Our Verses (of the Qur’aan) by mocking at them, stay away from them till they turn to another topic. And if Shaytaan (Satan) causes you to forget, then after the remembrance sit not you in the company of those people who are the Zaalimoon (polytheists and wrongdoers)”
End quote from al-Durar al-Saniyyah fi’l-Ajwabah al-Najdiyyah (16/142).
To sum up, you must denounce this evil and advise those who are in charge of this gym. If they respond, then praise be to Allaah, and if they do not respond then look for another gym, and do not share in the sin.
And Allaah knows best.