Without naming them, Museveni accused "arrogant and careless Western groups" of trying to recruit Ugandan children into homosexuality, prompting local pressure for the law.
The new law calls for first-time offenders to be sentenced to 14 years in jail. It also sets life imprisonment as the maximum penalty for "aggravated homosexuality," defined as repeated gay sex between consenting adults as well as same-sex acts involving a minor, a disabled person or where one partner is infected with HIV.
Government officials applauded after he signed the bill, which in its original draft called for the death penalty for some homosexual acts. That penalty was removed from the legislation following an international outcry. Rights groups repeatedly urged Museveni not to sign the bill, saying it is unnecessary in a country where homosexuality is already illegal under a colonial-era law that criminalized sex acts "against the order of nature."
Some European countries such as Sweden have threatened to cut aid to Uganda if the measure was enacted and U.S. President Barack Obama warned that signing the bill would "complicate" this East African country's relationship with Washington.
"By signing this bill, Museveni has not only let down gay Ugandans; he has also failed the very constituencies he claims to be protecting, including children," said Maria Burnett, a Uganda researcher for Human Rights Watch. "Laws to protect children from abuse by perpetrators of either the same or the opposite sex are on the books, but are poorly enforced in Uganda; policing consensual same-sex activity between adults and criminalizing talking about sexual rights will only distract from important police work and take away resources from the hard work of protecting children from abuse."
Burnett added that the bill is "a dramatic blow to freedom of expression ... criminalizing the expression of divergent views doesn't bode well for anyone in Uganda."
But the bill is widely popular in Uganda, where it has been championed by Christian clerics and many politicians.
Homosexuality is criminalized in many African countries. Nigeria last month passed an anti-gay law.
In signing the bill, Museveni said he had previously thought homosexuality was merely "abnormal" sexual behavior that some people were born with — the reason he once opposed harsh penalties against gays. Now he said he is convinced that it is a choice made by individuals who may try to influence others.
Africans are "flabbergasted" by homosexual behavior which they see as a "fundamental attack on their way of life," he said.
"Many of our homosexuals are mercenaries, heterosexuals who become homosexuals because of money ... These are prostitutes for money," he said.
The anti-gay measure was introduced in 2009 by a lawmaker with the ruling party who said the law was necessary to deter Western homosexuals from "recruiting" Ugandan children.
That legislator, David Bahati, said Monday that the bill's enactment is "a triumph of our sovereignty, a victory for the people of Uganda, the children of Uganda."
Ugandan gays have disputed Bahati's account, saying he and other political leaders were influenced by conservative U.S. evangelicals who wanted to spread their anti-gay agenda in Africa.
Pepe Julian Onziema, a prominent Uganda gay activist, said he was disappointed that Museveni signed the bill without taking time to talk to the people targeted by the law: Ugandan homosexuals.