Either way can work. It really depends on your personal goals. You say that you probably won't retire where you are now. Do you know where you do want to retire? If not, buying a property in a particular place will lock in a decision you may not feel ready to make.
Do you want to own or rent in retirement? Owning has advantages. There's no landlord to tell you what you can and cannot do with your property. You don't have to make a rent payment each month although you do have to pay property taxes each year. A paid-off home is a kind of savings. It saves rent expenditures later. It's an inferior form of savings, as you can't spend it on other options. Which is why many won't view it as savings at all.
Renting is an option though. You need to save enough money to support your rent stream. Remember to allow for the possibility that your rent will increase in the future. Note that this is in addition to the money that you need for other expenditures.
One option that can help you is a Real Estate Investment Trust (REIT). This is advantageous here because you want to put your rent savings in something that will increase in parallel with rents. You generally won't be able to invest 401k funds in a REIT, but you can invest IRA funds in a REIT.
You had suggested buying a place that you'd rent out until retirement. Another option is put the money in a REIT and use the accumulated value to purchase a home at retirement. Note that to do that, you wouldn't put the money in a retirement account with limited distributions. You could just put the money in a regular investment account, or there may be retirement options that support a one-time distribution at retirement. In any case, this would be an alternative, more flexible way to save for housing in retirement.
If you find your dream home in the perfect retirement community, buying and renting out a place can work. But it's not the only approach. Renting in retirement or saving for a retirement purchase can also work. Regardless, you are correct to start thinking about it now. Decades gives you time to adjust if your strategy looks to be falling short of your goals (whatever those may be).
If you do purchase, don't expect to make money on the rent. It is often true now that rent is only 60-65% of the money needed to service the mortgage. Either don't rely on the rental income or do thorough research on what comparable houses are charging in rent before purchasing.
Real estate prices collapsed in the 2006-2008 period. They have since rebounded. Note that this may mean they are overpriced again. If you're thinking purchase, consider waiting until the next recession to make a move. If these prices are the new normal, you haven't lost much (you will probably get a better mortgage price with more money down). If not, then you're ahead.
Renting as a landlord can be complicated. You have to maintain insurance and reserves to handle extreme situations. For example, if the tenants burn the house down. Or if they trash the place. You'll need someone to collect rent, find new tenants, clean between tenants, and keep up the maintenance on the property. Expect to sometimes have to buy new appliances or to repair a roof or toilet.
Try to avoid getting stuck with a mortgage in retirement. You can get mortgages of different lengths. So don't buy a thirty-year mortgage if you're retiring in twenty years. Get a twenty or better yet, a fifteen. Then you have some room to adjust before actually retiring. Shorter mortgages have higher monthly payments, so include that in your planning. If you're not taking the mortgage now, you can still save for the house purchase. The more you pay up front, the less your loan will cost.