If you wish to sell your rented flat, there are a number of precautions you should take and a number of possible solutions.
Warning: Don't offer the flat to the current tenant
For legal reasons, you should not offer to sell the apartment to the tenant, because if he does not want to buy it and you then terminate the lease, he can contest the termination and claim that you threatened him to buy the property or to leave. You should avoid giving notice to buy.