While exercise is important for overall health and muscle gain, diet is the primary way to gain weight. You need to consume more calories than you burn to gain weight.