To add, IIRC, several of those larger, hostile forces surrounding the Jews were also mostly okay with homosexuality, or at the very least didn't forbid it. The relatively young Jews, in forming their collective identity, needed to set laws that not only kept order and serve a social purpose, but also to differentiate themselves from the other factions with whom they were competing with. Several books like Leviticus served to state these laws that would clarify how Judaism differed from the other nations/factions/religions of the area.
Contemporary scholarship focuses on this last point: the prohibition against homosexuality was a means of creating distinction between Israelite
(Jewish would be an anachronism)
society and neighbors. Much about Israelite was a rejection of what were perceived to be the excesses of Egyptian religion. Among those were the plethora of sexual rituals that required the individual to submit to cult clergy. Indeed, the language used to prohibit sodomy--and it is a prohibition against sodomy, not homosexuallit, and certainly not lesbianism--classes it with prohibitions of other cult acts, not merely casual behavior. Given that it focuses exclusively on the act of sodomy and the high requirement of proof in courts, private practice of homosexuality is nearly untouchable. It really could only apply to public acts, such as ritual sex, and reflects other laws that separate sexuality from the practice of religion.