I rent the flat I live in. Something I hear a lot is that "Buying a house is a good investment". I hear this quite often from slightly older folks I know who it's worked out well for. I've been interested in the FIRE (Financial Independence, Retire Early) movement for a while - in general there seems to be a leaning within the FIRE community that actually renting is better.
Intuitively, I think most people think buying a house is better, because you're not 'throwing away' money on rent. Instead, that money instead goes towards a house! The counter argument is that: if you rent you can accumulate wealth by investing your deposit in, say, the S&P 500. The S&P 500 has returned 7% inflation adjusted on average, and this compounds overtime.
This came to a head when my wife said she thinks we should buy a house. So which is better?
I wrote a program to simulate both scenarios in the UK. Here's how it works:
Disclaimer: These numbers are made up, but represent a high-income person (or household) living in London. Checkout the code if you'd like to modify the scenarios to match your case.
The base scenario inputs for a 15 year simulation of renting:
2%(UK average for last 10 years )
£150,000, goes up by inflation each year
£4000 per month, adjusted by inflation
£2000 per month, adjusted by inflation
10%(S&P 500 45 year average )
You leave all your cash in the 'S&P 500'  and (hopefully) get a return.
Each year you earn money, spend it on rent and other outgoings. You then reinvest all left over cash straight back into stocks keeping as much of that in your ISA as you can to be tax efficient.
You do this for ~15 years.
For buying it's similar to renting but with some additional inputs:
1% increasing to 3% over a few years, 3% after that
You immediately buy a house (so you have less cash). The cash which is left over continues to be invested in the S&P 500.
Each year you earn money, spend it on your mortgage and other outgoings. You then reinvest all left over cash straight back into stocks keeping as much of that in your ISA as you can to be tax efficient.
You continue to live in this house and pay the mortgage for ~15 years.
Here's some graphs showing various scenarios which are slight variations of the base scenario above.
Because each year you earn money and save some of it, all the graphs are up and to the right.
Buying a house tends to do a little better than renting - this is probably because of leverage and house prices doing very well in recent history.
It's pretty noticeable that overall buying vs renting is quite close. Because of the way this is modelled if you simulate 40 years the two lines become the same line because over time most of your money ends up in the S&P 500 anyway.
Let's run the same kind of scenarios over 20 years for the UK going back to 1975.
This uses historical interest rates , house values , mortgage rates  and S&P 500 returns .
And for London:
For most years buying a house worked out better. It's also worth noting the mortgage rates in the 70's and 80's are much higher than what is consider 'normal' today.
When you buy a house instead of renting you're taking a bet about mortgage rates, house prices and the stock market for the next 15-30 years.
Using a mortgage lets you use leverage to magnify the benefits/downsides of house value gains.
One shortcomings of this analysis is that after 30 years you would fully own your house in the buy scenario - which drops your costs significantly after that point. So to match that performance, renting needs to outperform buying by the end value of the house.
The most important thing in the long term is probably your savings rate.
This analysis has a lot of assumptions / constraints (possibly mistakes!), but it personally helped me to understand what Renting or Buying might look like in the long run. There are obviously some large lifestyle differences between the two options beyond just wealth.
Thanks to Adam Lacchin, Zoe Balkwell and Mike Ford for their feedback.
 LifeStrategy® 80% Equity Fund - Accumulation for me!
 UK Inflation data: https://www.macrotrends.net/countries/GBR/united-kingdom/inflation-rate-cpi
 Mortgage rates: https://www.bankofengland.co.uk/boeapps/database/FromShowColumns.asp?Travel=NIxAZxI3x&FromCategoryList=Yes&NewMeaningId=RFRM2Y,FR2Y90,FR2Y75&CategId=6&HighlightCatValueDisplay=Fixed%20rate%20mortgage,%202%20year and https://www.bankofengland.co.uk/boeapps/database/Bank-Rate.asp
 S&P 500 Returns: https://www.macrotrends.net/2526/sp-500-historical-annual-returns