It depends in your fitness goals. Most cases, you do not really need to. The main purpose of a gym is to provide a facility to exercise. There are other places where exercise can be performed including in the comfort of your own place. You can even just go for a 15-minute walk per day and have a healthy lifestyle.
People who go to the gym go to the gym because they want to build bigger muscles, they want to reach a certain athletic performance, meet friends and so on. But as long as you have a clear fitness goal, going to the gym is not mandatory