From Shelter:
You must be renting on a commercial basis to get housing related benefits.
This usually means you have a legally binding tenancy agreement.
Your family member doesn't have to make a profit from renting to you. It can still be a commercial letting even if you're charged below the usual market rent. But your relative should intend to take on the rights and responsibilities of a landlord.
Benefit decision makers will consider whether your relative would enforce the agreement. For example, by serving notice to end the tenancy if you don't pay rent.
They may also check that the tenancy hasn't been set up just so you can claim benefits.
Under universal credit rules, there's no restriction on renting from a former partner as long as it's a genuine commercial agreement (as above.)
Under housing benefit rules, you can't claim in either of the following situations:
You used to live in the home as a couple
You have a child together who is under 16 and lives with you