The two main rivals in conflict-ridden Libya have agreed on a nationwide ceasefire, except for in the fight against terrorist groups, and will work on holding early presidential and parliamentary elections, according to a joint declaration.

Prime Minister Fayez Serraj and Khalifa Haftar, the military commander based in the remote east of the vast country, made the announcement on Tuesday after talks hosted in a western suburb of Paris by French President Emmanuel Macron.

"We commit to a ceasefire and to refrain from any use of armed force for any purpose that does not strictly constitute counterterrorism," Serraj and Haftar said in a joint declaration.

READ MORE: ISIL fighters executed by Haftar's forces in Libya

Macron said that both Serraj and Haftar had shown "historic courage" in agreeing to the ceasefire.

"I think today the cause of peace has made great progress," he said at a news conference after the signing of the agreement.

"Libyan people deserve this peace, we owe it to them ... the Mediterranean needs this peace," he added.

Macron, in facilitating the reconciliation, urged a political solution to the crisis and proposed that an army had to be working under civilian authority control for the territorial integrity of Libya.

The French leader confirmed that elections could be held in Libya in spring 2018.

The joint declaration called for the demobilisation of fighters and to make all efforts to integrate them into the regular forces.

The creation of a national amnesty process, a transitional justice system and reparations were referenced in the document.

It also urged support for the rule of law in Libya and respect for human rights.

Al Jazeera's Paul Brennan, reporting from La Celle-St-Cloud, to the west of Paris, said that President Macron has acknowledged that signing up to the 10-point declaration involves risks.

"There are no guarantees of success," our correspondent said. "Serraj and Haftar still have to sell the deal to the various political factions that have not been directly involved in the talks here, which is going to be a tall order."

Tuesday's meeting was the first between the Libyan factions since exploratory talks hosted by the UAE in Abu Dhabi in early May.

Libya has been locked in a state of violence and turmoil since 2011, when a bloody popular uprising ended with the ousting and death of former President Muammar Gaddafi.