Skhirat, Morocco: Hopes that divided, war-ravaged Libya can pull itself together and fend off advancing Daesh extremists soared on Thursday, as the country's rival factions signed a U.N.-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 following 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 IS's affiliate in Libya and 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 U.N. 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," al-Makhzoum said.

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

The speakers of the two parliaments - Tripoli-based Nuri A.M. Abusahmain and Aguila Saleh Issa from the east - were not at the Morocco ceremony. The two, who are seen by analysts as hard-liners, held talks on Tuesday in Malta to forge a separate deal without U.N. involvement.

Afterward, they issued a statement saying the representatives who travelled to Morocco were not mandated to represent the parliaments in the talks.

Before the start of Thursday's ceremony, Al-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.

U.N. envoy Martin Kobler, who attended the Morocco ceremony, said that 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."

The foreign ministers of Turkey, Italy, Spain, Qatar, Tunisia, and Morocco also spoke at the ceremony in support of the deal.

Among the first to welcome the deal was French Foreign Minister Laurent Fabius, who praised the accord and promised to support efforts by a new unity government.

"The priority should now go toward creating a national unity government," he said in a statement. "That's the condition for tackling terrorism and trafficking that threaten the security of the region and Europe."

U.N. Secretary-General Ban Ki-moon called the ceremony a "historic signing ... a critical step in continuing Libya's post-revolution transition after months of turmoil and uncertainty." Ban said the U.N. will keep working to broaden support for the agreement and also cautioned that the "road ahead will be difficult."

In Washington, State Department spokesman John Kirby welcomed the deal, saying it "provides the framework for establishing a unified Libyan government of national accord."

Stopping the violence however hinges on getting the government up and working in Tripoli despite any opposition it may face there, said Mattia Toaldo, a policy fellow at the European council on foreign relations think tank.

"If they manage to solve the Tripoli issue, they have a relatively good chance, because there are number of local cease-fires already in place in Libya," he said.

"If the government doesn't manage to establish itself in Tripoli, then there could be a big battle for the control of it between militias loyal to the - let's call it the U.N. government - and militias loyal to the GNC. That could mean a lot of fighting and a lot more space for Daesh to expand."