You need to live in it for 2 out of the last five years. See IRS web reference for the long explanation.
To be eligible for an exclusion, your home must have been owned by you and used as your main home for a period of at least two years out of the five years prior to its sale or exchange. The required two years of ownership and use during the fiveâ€“year period ending on the date of sale do not have to be continuous. You can meet the ownership and the use tests during different two year periods. However, both tests must be met during the fiveâ€“year period ending on the date of the sale or exchange.