Uganda's constitutional court has struck down an anti-homosexuality law that punishes gay sex with long jail sentences, citing procedural irregularities in the way the legislation was enacted.

Under the Anti-Homosexuality Act, those convicted of "aggravated homosexuality" faced punishments of up to life in prison and seven years for "aiding and abetting homosexuality".

The law, which came into effect in February, drew widespread protest from western countries, some of which responded by cutting aid to Uganda.

Homosexuality is a taboo issue in much of Africa and illegal in 37 countries on the continent, but the punishments laid out in Uganda are among the harshest.

"I can confirm the anti-homosexuality law has been struck down," said Nicholas Opio, a lawyer for petitioners to the court.

"The judge said there were irregularities in the process of its enactment and also there was no quorum in parliament."

The petition had alleged that the law violated fundamental rights.

The constitutional court ruling can be challenged through an appeals process, lawyers said.

United Nations spokesman Stephane Dujarric says the UN is pleased with the ruling.

"The secretary-general welcomes the decision by the constitutional court of Uganda to annul the country's anti-homosexuality act as a victory for the rule of law," he said.

"He pays tribute to all those who contributed to this step forward, particularly the human rights defenders in Uganda who spoke out, at times incurring great personal risk."

Fear of violence, imprisonment and loss of jobs means few gay people in Africa are open about their sexuality.

Reuters