LIMA (Reuters) - Peru’s former President Ollanta Humala and his wife turned themselves in to authorities late on Thursday after a judge ordered them to spend up to 18 months in jail while prosecutors prepare charges against them for alleged money laundering.

Peru's former President Ollanta Humala leaves the Nationalist Party headquarters in Lima, Peru, July 13, 2017. REUTERS/Guadalupe Pardo

The ruling by Judge Richard Concepcion, which triggered jeers from the couple’s supporters during a public hearing that stretched over two days, marked the second time he has mandated jail time for an ex-president since a massive graft scandal in neighboring Brazil rippled into Peru.

Prosecutor German Juarez cited testimony from former executives of Brazilian builder Odebrecht - at the center of the graft scandal - to accuse Humala and his wife, Nadine Heredia, of taking $3 million that allegedly stems from corruption.Juarez also accused the couple of taking illegally-obtained cash from late Venezuelan President Hugo Chavez.

“Here’s a president who rose to the presidency and governed us with an electoral campaign built on illicit money. That’s serious because it morally wounds society,” Juarez said.

Humala, a mild-mannered former military officer who governed Peru from 2011 to 2016, turned himself in with Heredia, the co-founder of his nationalist party, immediately after the ruling. Both denied wrongdoing and called their pre-trial detention unfair.

“This confirms abuse of power, which we will face in defense of our rights and the rights of all,” Humala said on social network Twitter, as television showed images of the couple walking into a building of holding cells, escorted by police.

The ruling came a day after former Brazilian President Luiz Inacio Lula da Silva was sentenced to nearly 10 years in prison for graft and dealt a further blow to the left in Latin America. Unlike Humala, Lula will remain free on appeal.

Humala recast himself as a more moderate leftist like Lula to win the 2011 election, following an unsuccessful 2006 presidential bid as an ally of Chavez.

He ended his five-year term with low approval ratings last year, despite backing private investment and starting social programs for the poor.

Humala’s downfall jolted Peru’s political system, though he is not expected to run for office again. Rightwing groups celebrated his downfall, while leftists urged prosecutors to investigate Odebrecht’s links to conservative politicians.

Centrist ex-president Alejandro Toledo, believed to be in the United States, refused to turn himself in this year, after Concepcion ordered him jailed before a trial over allegations of having taken a $20-million bribe from Odebrecht.

Toledo and Humala rose to power on the support of those who once believed they would counter the graft and autocratic rule of the 1990-2000 government of then president Alberto Fujimori.

President Pedro Pablo Kuczynski has said he might pardon the rightwing Fujimori, who is now serving a 25-year sentence for human rights violations and corruption, in a quest for better ties with Fujimori’s supporters in Congress.