how many years do you have to live in a house to avoid capital gains tax?
To claim the whole exclusion, you must have owned and lived in your home as your principal residence an aggregate of at least two of the five years before the sale (this is called the ownership and use test). You can claim the exclusion once every two years.