SKHIRAT, Morocco — Hopes that divided, war-ravaged Libya can pull itself together and fend off advancing Islamic State extremists soared on Thursday, as the country's rival factions signed a UN-brokered deal to form a unity government that is meant to bring about peace.

But the lawmakers from Libya's rival parliaments who hugged and celebrated at the signing ceremony in Morocco still face the enormous task of convincing the deal's many opponents back home — including rival political factions and heavily armed militias — that compliance is worthwhile.

Libya slid into chaos after the 2011 toppling and killing of dictator Moammar Gadhafi. Since then, its divisions only increased, and now it has two governments and parliaments — the internationally recognized one in the country's east, and an Islamist-backed one in the capital, Tripoli.


The agreement aims to create a new national government in Tripoli, end the divide between the rival parliaments, governments, and military coalitions, and give the international community a partner for its battle against the Islamic State's affiliate in Libya and against human traffickers.

The agreement also envisages a presidential council tasked to choose the Cabinet, which includes a prime minister, five deputies, and three state ministers. Fayez Serraj, a member of the eastern parliament from Tripoli, will head the council.

''On paper, this is fantastic news,'' said analyst Claudia Gazzini of International Crisis Group. ''In practice, the uncertain level of support for the agreement in Libya, the fact that the leadership of both existing parliaments oppose it and are busily devising their own peace plan, and the fact that the new government will have little control over key parts of the country have left many skeptical.''

The document was signed by Emhemed Shoaib, the deputy speaker of the internationally recognized Libyan parliament, and Salah al-Makhzoum, the second deputy of the Islamist-backed parliament based in Tripoli, among others. But it has detractors on both sides who seek a separate deal without UN involvement.


''We know well that the document of political accord in its current form is not the perfect thing that everyone wants, but at the same time, this political accord is a stage on the path to rescue Libya from collapsing and to ensure its unity,'' Makhzoum said.

Shoaib said the deal is meant to say ''goodbye to weapons'' that Libya is awash in.

Before the start of Thursday's ceremony, Makhzoum and Faraj Abu-Hashem, the spokesman for the east-based parliament, told The Associated Press that 88 lawmakers from the two parliaments were present at the signing. The eastern parliament has 156 known members, while the rival parliament in Tripoli has 135.

UN envoy Martin Kobler, who attended the Morocco ceremony, said it was ''just the beginning of a long journey for Libya.''

''Four challenges in particular will immediately test the abilities of the new government,'' he said. ''First, to face immediately the dire humanitarian situation in the country. Second, an inclusive national security dialogue. Third, the fight against Daesh (the Islamic State group) and other terrorist groups, and fourth, a particular attention to Benghazi and other areas.''