We did this and got lucky (did not plan on moving so soon). Bought in 2017 for 310k, sold in 2019 for 352k. We had 20% down so avoided PMI. However that doesn't mean we profited 42k. Realtor fees were 17.6k (3% to seller, 2% to buyer I believe). We actually listed the house on our own on Zillow to avoid realtor fees but no one was interested. Got a realtor and boom 30+ people at open house. We probably put close to 20k in repairs/maintenance during those 2 years. So at best we broke even vs paying rent.
We tried to get it ready to rent out, but it wasn't a good rental area. Even hired a property manager to look for tenants. We had to pay for asbestos removal, releading, new paint, etc. And it was all for nothing.
Another thing new homeowners don't think about it decorations, furniture, home insurance. Those things add up. A house is general larger than an apartment that you rent. So you fill those empty rooms with stuff like a guest bed, lamps, curtains, dressers, wall decorations etc. The bathrooms need new shower curtains, towels etc. All that stuff adds up and has no resale value. You have to own yard tools like a lawnmower and snowblower. These things also cost money to maintain.
When we rented, we got free heat and hot water! When we owned, our heating bill would be up to $300 in winter :( and since it was a larger space, electricity was of course higher. We didn't have to do any yardwork when we rented.
Overall it was stressful and if we knew we wouldn't stay in the area, we would have kept renting. We were just glad to get rid of it.