In this article, I am referring to your own home, not to an investment home.
Buy the house where you leave is a great investment almost all of the time…. tax-free…. I mean if you buy houses to rent out you have to pay taxes on the rental income while if you buy your own house you virtually pay rent to yourself but that money is tax-free…. Also, the loan payment has 3 basic components: interest, principal, and escrow. The interest is tax-deductible ( check with your accountant ), the principal is money that you effectively pay to yourself, it goes to you and when you sell the house you will get it all back while the escrow is just an expense.
Buying is almost always better in the long run but not so in the short run. All the expenses due to the purchase/sell of the property have to be amortized in the time you will own the property and these expenses have to be added to the holding expenses. Also Real estate grows on average 3.5% in value – It is not always steady sometimes there are real estate crises and prices go down but on the average they grow, so , if you hold long enough you will enjoy the capital appreciation
Why did I say almost always better ? Well if you buy a house after a big surge in value ( like in the pre-bubble years of 2006 2007 ) you will have to wait a long long time before you recuperate your investment.
Chances are that if you stay less then 3, 4 or 5 years renting is the cheapest option in any case.