It is precisely because there is risk involved in giving a loan that there is any profit to be made. If the legal system actively enforced repayment through debtors' prison and the like, there would be no basis for interest.
Well, there would still be a basis for some interest, because the lender could make more money on their capital by using it for another purpose, such as buying T-bills or some other security with essentially zero risk. Intuitively I'd think that part of the interest charged on a loan is literally just to "rent the money", preventing it from being used for another purpose; the rest is to account for the possibility that the loan might not be repaid.