Hi
Different reasons may explain this
1/ If you power on your Garmin GPS, then start the ride with few satellites synchronized, then the altitude precision is bad. It may happen if you power on the Garmin Edge inside a building, or close to walls outside or in a forest.
2/ There is a correction feature on garmin connect website. When you transmit your data to your garmin connect account, you can apply this correction (see picture, sorry for french language) which should allow to fix altitude gain, as this is base don topology data.
So try powering on the garmin in a free area, where lot of satellites are visible (5 are necessary for a correct altitude measure). Then use the garmin connect feature to compare w/ your friends, it should be better.