President Museveni signed the bill during a press conference at State House in Entebbe, near the capital, Kampala, on Monday.
"No study has shown you can be homosexual by nature, that's why I have agreed to sign the bill," Museveni said in a speech after the signing ceremony.
"If the West does not want to work with us because of homosexuals, then we have enough space to ourselves here," Museveni added.
The law, which toughens penalties for homosexual sex, sets out a 14-year jail sentence for first-time offenders.
The legislation also sets life imprisonment as the maximum penalty for “aggravated homosexuality.” This category of offenses is defined as repeated gay sex between consenting adults and acts involving minors, a disabled person, or where one partner is infected with HIV.
It also outlaws the promotion of homosexuality and requires people to denounce gays.
The Ugandan anti-gay bill easily passed through parliament in December after it was agreed to drop a death penalty clause.
The bill is popular with the conservative local constituency that is vehemently opposed to homosexuality, but international rights groups have condemned it as draconian.
Museveni is a key African ally of the United States and the European Union but he has been under fire recently by key Western donors over the anti-gay legislation.
Earlier this month, US President Barack Obama called the law a "step backward for all Ugandans" which would complicate bilateral ties with Kampala.
Many African countries, with the exception of South Africa, have laws that ban or suppress homosexuality. In January, a law took effect in Nigeria that outlaws same-sex relationships with up to 14 years in prison for gay couples living together.
hc/pfd (Reuters, AFP, AP)