The International Criminal Court (ICC) has ruled that Libya's Gaddafi-era intelligence chief, Abdullah al-Senussi, can be tried in Libya.
The decision means that the ICC will no longer demand that Mr Senussi be sent to The Hague to stand trial.
The former military intelligence boss was indicted by the ICC in 2011 for war crimes allegedly committed during the uprising against Colonel Gaddafi.
Mr Senussi's lawyer, Ben Emmerson, described it as a "shocking decision".
Mr Emmerson said he would be appealing against the ruling.
He said the ICC had evidence "that the Libyan justice system is in a state of collapse and that it is incapable of conducting fair trials of any Gaddafi-era officials".
The lawyer for the Libyan government welcomed the ICC's decision.
Philippe Sands said the ruling "vindicates the efforts it [the Libyan government] has made to give effect to the principle of complementarity, which allows Libya to conduct the trial of Mr Senussi if it satisfies the court, as it has done, that it can conduct a fair trial".
The principle referred to means that the ICC does not carry out proceedings against a suspect if it deems they are receiving a fair trial in a domestic court.
A pre-trial chamber at the ICC decided that "Libya is willing and able genuinely to carry out" investigations into Mr Senussi, the court said in a statement.
It said the decision had "no bearing on the case against Saif al-Islam Gaddafi".
The late Libyan leader's son was also indicted by the ICC for war crimes, and is a co-defendant, along with 36 others, of Mr Senussi in the domestic case.
The ICC said Libya had failed to show it had collected more than "a few sparse items of evidence" against Mr Gaddafi and had yet to secure him legal representation.
A court in Tripoli is to decide on 24 October whether to indict Mr Gaddafi and Mr Senussi, among 20 figures of the former regime charged with the 2011 killings of protesters, Agence France-Presse reported.
Saif Gaddafi is being held by a militia in the western town of Zintan. Last month the militiamen refused to deliver him to a court in Tripoli to appear at a pre-trial hearing alongside Mr Senussi and his co-defendants.
He is facing trial in Zintan on a separate charge of trading information threatening Libya's national security. That case has been adjourned until 12 December.
The ICC said its decision not to try Mr Senussi – Col Gaddafi's brother-in-law and said to have been one of his closest aides – could be appealed against if it later decides the trial is unfair.
Mr Senussi has been implicated in an infamous Libyan massacre in 1996, in which more than 1,000 inmates were killed at the Abu Salim prison in Tripoli.
In 1999 Mr Senussi was tried in absentia in France and sentenced to life imprisonment for the shooting down of a UTA airliner over Niger a decade earlier.
He fled Libya after the downfall of Col Gaddafi but was extradited from Mauritania last year.
Amnesty International has called for the immediate transfer of Mr Senussi and Mr Gaddafi to the ICC.
'Law of the gun'
Amnesty said Libya still needed to improve security and rebuild the country's justice system to ensure fair impartial trials.
Ben Emmerson said Libya was "sliding into widescale lawlessness where the law of the gun rules and armed militias do as they please".
The former intelligence chief's lawyer added that the effect of Friday's decision would be "to condemn Mr Senussi to face mob justice without even access to a lawyer, and in which the inevitable outcome is the death penalty".
Libya has been beset by security problems since the 2011 uprising, during which Col Gaddafi was captured and killed while trying to hide from rebels.
Earlier this week, the country's prime minister, Ali Zeidan, was kidnapped for several hours by armed militiamen.