When the bill was first introduced in 2009, it was widely condemned for including the death penalty, but that was removed from the revised version passed by parliament.
Instead it sets life imprisonment as the penalty for a homosexual act where one of the partners is infected with HIV, sex with minors and the disabled, as well as repeated sexual offences among consenting adults, a spokeswoman for Uganda’s parliament said.
The bill also prescribes a seven-year jail term for a person who “conducts a marriage ceremony” for same-sex couples.
The bill was passed unanimously.
President Yoweri Museveni must sign it into law within 30 days. Although in the past he spoke disparagingly of gays, in recent times Mr Museveni has softened his position, saying he is only opposed to gays who appear to “promote” themselves.
“In our society there were a few homosexuals,” Mr Museveni said in March. “There was no persecution, no killings and no marginalisation of these people but they were regarded as deviants. Sex among Africans is confidential. If I am to kiss my wife in public, I would lose an election in Uganda.”
The passage of the bill makes it “a truly terrifying day for human rights in Uganda,” said Frank Mugisha, a Ugandan gay activist, who called the legislation “the worst anti-gay law in the world.”
He urged Mr Museveni not to approve the new law.
“It will open a new era of fear and persecution,” he said. “If this law is signed by president Museveni, I’d be thrown in jail for life and in all likelihood killed.”