SAN FRANCISCO -- Former Peruvian President Alejandro Toledo can be released on bail while he fights extradition to his native country to face corruption charges, a U.S. judge ruled, citing the ex-president's declining mental health.

U.S. District Judge Vince Chhabria ordered Toledo released but said the former president can't leave the lockup before Oct. 22 so federal prosecutors have time to appeal or find a different "detention arrangement."

Toledo, 73, has been held in solitary confinement at the Santa Rita Jail about 40 miles (60 kilometres) east of San Francisco since his arrest at his home in the city of Menlo Park on July 16. He is confined to a small cell and allowed out for an hour every two days.

The judge said in a written order issued Thursday that Toledo faces a lengthy extradition case that could last for months, if not years, and cited a report by a staff psychiatrist at the jail that says the ex-president's is taking medication for anxiety and depression.

"The combination of the anticipated length of the proceedings and the conditions of Toledo's confinement creates a special circumstance that qualifies Toledo for release," the judge wrote.

Magistrate Judge Thomas S. Hixson in San Francisco previously denied bail for Toledo after prosecutors argued he was a flight risk and noted that officials found a suitcase with $40,000 in cash during his arrest.

Toledo has been aware of the attempt to extradite him since February 2017 and had not tried to flee California, where they said he lived as a permanent legal resident and where he has ties going back to the 1970s, when he was a student at Stanford. Longtime friends, including Stanford professors, have agreed to put up $1 million for bail, his attorneys have said in court.

Chhabria said Toledo's flight risk would be diminished because he doesn't have a current passport, would remain under "home lockdown" and wear a GPS monitoring device. And fleeing would harm his friends, the judge pointed out.

"In addition to the difficulty of fleeing, Toledo has longstanding ties to Northern California, and two of his closest friends would lose their home should he abscond during the extradition proceedings," Chhabria wrote.

Toledo, who has denied wrongdoing, was Peru's president from 2001 to 2006.

He was a visiting scholar at Stanford University as recently as 2017, though the school has said it was an unpaid position. He was working on a book.

The ex-president is accused in Peru of taking $20 million in bribes from Odebrecht, a Brazilian construction giant. The Odebrecht scandal also has tainted the careers of other former presidents in Peru who are under investigation.

In April, former leader Alan Garcia killed himself as officers waited to arrest him in a graft probe.