ISIS was forced to cede the Libyan town of Derna, but in the past few months they have taken areas in Libya’s “Oil Crescent,” and have begun attacks on the outer defenses of the city of Misrata. PHOTOGRAPH BY STRINGER / REUTERS VIA LANDOV

The wars in Syria and Iraq continue to dominate international headlines, but a growing stream of news offers reminders that Libya is in a state of meltdown. In Cairo on Sunday, John Kerry said he had discussed the need for “increased border cooperation” along Egypt’s long, mostly unguarded Libyan border. Tunisia has begun building a security wall along its own hundred-and-four-mile-long border with Libya.

For months, ISIS has been trumpeting its abduction and execution of African Christians in Libya. In February, a slick, ghoulish video showed twenty-one Egyptian hostages in orange jumpsuits being led along a beach by black-masked executioners, who forced them to kneel and then cut off their heads. In April, another video appeared, showing the execution of twenty-nine Ethiopians in Libya. Gunmen who trained with ISIS in Libya were involved in the murder of twenty foreign tourists, at a Tunis museum in March, and thirty-eight more tourists, most of them British, at a seaside resort in Tunisia in June. These attacks focused attention on the fact that Libya, a vast, oil-rich, underpopulated country with a long southern-Mediterranean coastline, has become part of the self-proclaimed ISIS caliphate.

In a parallel phenomenon, armed trafficking gangs in Libya are driving most of Africa’s illegal immigration across the Mediterranean to Europe. As many as a hundred and seventy thousand are thought to have made the crossing last year, with thousands dying en route. Unprecedented numbers are continuing to cross this year, taking advantage of the chaos in Libya.

Only five years ago, Libya was a source of stability in the region, as Muammar Qaddafi entered his fifth decade in office. In February, 2011, after a short, heady uprising inspired by the ouster of Hosni Mubarak, in Egypt, Libyan rebels based in Benghazi ousted Qaddafi’s security forces and effectively liberated the eastern half of the country. In March, as Qaddafi’s military forces appeared poised to retake Benghazi, NATO intervened with air strikes and Tomahawk missiles. The U.N. authorized the intervention after British Prime Minister David Cameron and French President Nicolas Sarkozy lobbied in favor of it on humanitarian grounds; after showing some reticence, President Obama agreed to commit U.S. military power.

On October 20th, 2011, NATO air strikes destroyed a road convoy carrying Qaddafi in his home town of Sirte. He was captured by the rebels, who tortured and killed him. Most Libyans assumed that their moment of emancipation had come, but, as they grappled with their country’s lack of institutions and a plethora of armed militias, they faltered and ultimately failed to find the path to democracy that Cameron and Sarkozy had promised. Wary of appearing to be occupiers, the NATO powers that had helped liberate the country stood by as it fell apart.

Last week, David Cameron’s special envoy for Libya, Jonathan Powell, acknowledged that the West bore some responsibility for Libya’s collapse. “We made a mistake leaving Libya so soon after Qaddafi’s downfall,” he told me. “We thought they didn’t need any Paul Bremers”—a reference to the American official who controversially ran Iraq after the U.S. military invasion—“but in fact they did need help.”

Last November, battle-hardened Libyan Islamists, who had returned home from fighting in Iraq and Syria, along with Islamists from other countries, seized the eastern city of Derna and claimed it for ISIS. Emulating their comrades in Raqqa and Mosul, they stoned, shot, beheaded, and crucified people deemed guilty of espionage or “un-Islamic” behavior. Last month, a rival militia loyal to Al Qaeda waged and won a battle for control of the city. The victors are said to have marched the captured ISIS commander through the streets naked before executing him. ISIS lost Derna, but in the past few months they have taken Qaddafi’s home town of Sirte and surrounding areas in Libya’s “Oil Crescent,” and have begun attacks on the outer defenses of the city of Misrata.

ISIS is advancing amid a low-level civil war, pitting militias and tribes against one another in alliances that defy easy description. Late last month, the country’s displaced government—which is based in the east, while a militia-backed government holds sway in Tripoli—carried out air strikes against two ships off the eastern coast, sinking one that allegedly carried jihadists and weapons. Benghazi is a war zone, where militias loyal to both governments, as well as jihadists, battle daily. Large swatches of the city have been destroyed, and hundreds of civilians have died in the fighting. There is also a regional-proxy-war element to consider. The eastern government is backed by Egypt’s military regime, Saudi Arabia, and the United Arab Emirates; the Tripoli coalition is connected to the Muslim Brotherhood and backed by Turkey and Qatar.

For most of the past year, the U.N., NATO, and the E.U. have pinned their hopes for a stabilized Libya on U.N. Special Representative Bernardino León. Two weeks ago, after months of shuttle diplomacy, León secured a tentative peace agreement that calls for a ceasefire, a power-sharing agreement between Libya’s rival governments, a timetable for new elections, and the deployment of international peacekeeping troops. The agreement was signed by the eastern government and by city councils and civil-society leaders, but not, crucially, by the hard-liners within the Islamist militia coalition that holds power in Tripoli, nor by Khalifa Heftar, a former army general who leads the anti-Islamist forces that are fighting in the east. (I wrote about Heftar for the magazine, in February.) Without Tripoli or Heftar’s involvement, there is no peace deal, but León, who characterizes himself as a “militant optimist,” told me, on July 26th, that he saw a “critical mass” developing that could swing Libyans toward reconciliation. “None of the leaders can bring all the actors forward, but if enough come, they will bring others. I think in this very complicated chess game, that we have a possibility to go forward,” he said. “Of course, it may all fall apart, because the hard-liners are trying hard to undermine it.”

Frederic Wehrey, a Libya analyst at the Carnegie Endowment for International Peace, was more skeptical about the prospects for peace. “The record on peace deals in civil wars happening before both sides are exhausted are not good, are they? I’m not optimistic,” he told me.

On July 20th, in a speech outlining his intention to do more to combat Islamist extremism, British Prime Minister David Cameron said that British warplanes would conduct air strikes in Syria, Iraq, and Libya, if it were determined that any of them harbor “a specific threat to British people.” Unless the crisis in Libya begins to be resolved soon, the calls for an international response may grow louder and stronger. Egypt carried out air strikes against ISIS positions in Libya after the mass execution of its citizens, in February. In keeping with its policy to retaliate against terrorists anywhere in the world, the U.S. has struck against high-value targets in Libya twice in the last five weeks. On June 15th, after a raid carried out by F-15 fighter jets, the U.S. claimed it had killed Mokhtar Belmokhtar, an elusive Algerian terrorist who masterminded the takeover of an Algerian gas plant in 2013, which resulted in the murders of at least thirty-eight foreign hostages. On July 3rd, a U.S. drone air strike killed Abu Iyadh, a veteran Tunisian terrorist who had gone underground in Libya, in 2013.