A judge ordered the arrest of Peru's former President Ollanta Humala and his wife on Thursday night as they face money laundering and conspiracy accusations tied to a construction scandal involving Brazilian company Odebrecht.

The preventive detention, which would keep Humala and his wife Nadine Heredia in jail for up to 18 months while they are investigated, came after prosecutors argued the couple could flee Peru to evade justice or could interfere with the nearly three-year investigation.

Shouts of protest could be heard in the courtroom as Judge Richard Concepcion issued his ruling.

Humala and Heredia deny wrongdoing but were on their way to turn themselves into authorities, their lawyers said after the ruling. "They're not fleeing. They never intended to flee," said defence lawyer Wilfredo Pedraza.

READ MORE: Connecting Iquitos - Building a road through the Amazon

The allegations against Humala stem from testimony by the former head of construction giant Odebrecht, who said he illegally contributed $3m to Humala's 2011 presidential campaign. The couple is also accused of taking undeclared funds from the late Venezuelan leader Hugo Chavez during a previous, unsuccessful presidential bid.

Humala, who finished his term in 2016, never declared the contributions and prosecutors argue he and his wife conspired to hide them for personal gain.

Humala, 55, has insisted he has no intention of fleeing the country and has said he looks forward to defending himself against what he calls baseless charges.

"We're staying here, we've even handed over our passports," he said earlier on Thursday during a brief meeting with reporters who had gathered for three days outside his home. "In every moment, we've shown our roots and good will. But the prosecutor sees everything we do in the opposite light. I think he's been poisoned."

The same judge previously ordered the arrest of another former Peruvian president, Alejandro Toledo, for related charges. Toledo is in the United States fighting attempts by Peruvian authorities to have him deported to answer the charges.

Authorities across Latin America have been moving to charge officials accused of taking some $800m in bribes from Odebrecht. The company acknowledged the bribes when it signed a plea agreement in December with the US Department of Justice.

The bribes include some $29m paid in Peru for projects built during the administrations of Toledo, Humala and former President Alan Garcia.

The same scandal has ensnared former Brazilian President Luiz Inacio Lula da Silva, who was convicted on Wednesday and sentenced to almost 10 years in jail for taking gifts from another Brazilian builder who along with Odebrecht paid bribes to politicians in exchange for government contracts.

He denies the accusation and will remain free while he appeals what he says are politically motivated charges.