In 1911, over a swathe of small farms south of Tripoli, an Italian aviator named Giulio Gavotti leaned out of his biplane and threw a small bomb onto Turkish soldiers below. It was the first recorded use of a powered aircraft as a weapon of war. Today, Libyan militiamen defending the capital face a deadlier threat from the sky: drones firing precision-guided bombs. The drones have reportedly been supplied by the United Arab Emirates, a serial meddler in post-revolution Libya, to militias led by a renegade general named Khalifa Haftar.

Haftar, an army officer under Gaddafi and later a CIA asset who lived in northern Virginia for twenty years, has been accruing international backing and expanding his territory at the head of the self-styled Libyan National Army (LNA). Arguing that the country is not ready for democracy, Haftar promises order through authoritarian rule. He is also a dedicated anti-Islamist. There are three options for Islamists in Libya, he told me when I met him in 2014: dead, in prison or out of the country. On 4 April this year he launched an assault on the internationally recognised Government of National Accord (GNA) in Tripoli, announcing that he was cleaning the city of corrupt militias and Islamists. Such pledges are a thin cover for a power grab; the United Nations envoy for Libya has called it a ‘coup’. In any case, Haftar’s LNA includes a number of tribal and Salafist militias, who are mired in corruption themselves.

On 15 April Haftar received a personal phone call from President Trump, who praised his ‘vision’ for Libya. American policy had previously been to back the GNA. But Haftar has long enjoyed military assistance from two of Trump’s favourite despotic regimes – Egypt and the United Arab Emirates – as well as support from France and Russia.

His international backers had counted on a swift and even bloodless entry into the capital, but that has not happened. The Tripolitanian militias that Haftar had hoped would defect to his side have put up fierce resistance. In the drawn-out fighting over the past three months, more than a thousand people have been killed and thousands of families have been displaced. Haftar’s opponents have halted his invasion and are confident they can push him out of western Libya.

The disparate militias on the front line are only nominally loyal to the weak central government, though it’s paying some of them well to fight. Their power dwindled before the war, but now they have newfound influence and a sense of entitlement. If and when Haftar is defeated, a new contest for power could erupt among the victors.

One evening last month, I joined a group of anti-Haftar militiamen on the outskirts of Tripoli. On the roof of a ruined villa, lanky young men scanned the front with a night vision scope. The air was heavy with the smell of sweat and rotten fruit. The LNA were three hundred metres away, on the other side of an olive grove hemmed by junipers. To our left, there were flashes of tracer-fire, the growl of heavy machine-guns and the crash of mortars. Hunkered behind a breezeblock wall, the commander on the roof was listening intently to a walkie-talkie he’d stolen from Haftar’s forces.

‘Zero Two, Zero Four, there are bodies in the Tiger!’

‘Zero Four, get them out!’

‘I can’t, I can’t, there’s too much gunfire!’

The Tiger was an armoured personnel carrier, supplied by the Emirates, that had just been destroyed by GNA fighters. We could see the glow of its burning wreckage just over a hedgerow. The LNA thought it had been hit by an anti-tank missile, but GNA militiamen had been ordered to pull back because ‘we’re hitting it from the air.’

The degree of precision suggests a strike by a drone, rather than one of the GNA’s ageing jets. In the last month, the Tripoli forces have received armed drones from Turkey, which, along with Qatar, is opposed to Egyptian and Emirati support for Haftar. Turkish and Qatari influence in Libya had been in retreat for several years, but now the GNA is turning to whoever can help. Turkey has also provided armoured personnel carriers, though the GNA fighters would prefer smaller, nimbler vehicles. The drones, however, are having a real effect.

After the Tiger was hit, the LNA forces tried to regroup. We came down from the roof because the direction of enemy fire had shifted. A bullet slammed into the wall next to us – the LNA gunner, it seemed, had been drawn by the light of a cell phone.

An affable engineer and father of four in his late forties, the GNA commander is called Muhammad al-Darrat, but he goes by the nickname ‘Glaw’. The last time I saw him was in summer 2016, during the seven-month war against Islamic State in the central city of Sirte. Haftar says he is fighting terrorists in Tripoli but Glaw isn’t the only GNA-aligned militiaman who fought against Islamic State; many showed me shrapnel and bullet scars from the battle. Some had lost brothers or cousins. Back then, they enjoyed air cover and intelligence support from American military personnel based in western Libya. But shortly after Haftar’s attack on the capital, these Americans left the country – and then Trump called Haftar. ‘That bothered us,’ Glaw said.

The defence of Tripoli, he told me, is much tougher than the war against the Islamic State. ‘We are up now against a military, not a terrorist organisation,’ he said. ‘We didn’t expect it would take this long.’

We heard the sound of an LNA drone overhead. The LNA attacked our position with heavy gunfire. One of the fighters asked Glaw if they could respond. The commander shook his head. It was a trap: the LNA were trying to lure his men into moving their hidden gun trucks into the open, where they could be struck from the air.

On 26 June, after the Turkish drones joined the action, GNA forces dealt a major blow to Haftar with a surprise attack on his forward headquarters in the mountain town of Gharyan, south of Tripoli. They found a trove of high-end military weapons, including US-made Javelin anti-tank missiles, which were originally sold to France. Questions persist about how they wound up in Gharyan, though French intelligence officers have secretly helped Haftar in eastern Libya and there are reports they are doing so again in the attack on Tripoli.

On the battlefield, the militias fighting Haftar are surprisingly cohesive and well co-ordinated. But just months ago, some were fighting one another – and suspicions remain. I met young Tuareg men from Libya’s far south fighting in a Tripoli-based militia with a large Salafist component called the Special Deterrence Force. There are fighters from Benghazi, displaced by Haftar in his previous war, with a desire for vengeance. They cover their faces because they have family in the east, vulnerable to retribution. The ethnic Amazigh (commonly known as Berbers) in Libya’s western mountains worry about Haftar’s exclusivist Arab vision. ‘The first thing any dictator does is go after minorities,’ one of their officers told me.

Some of the militiamen say they are fighting from a sense of civic duty, to safeguard ‘the revolution’ or prevent a ‘militarisation of the state’. Some are fighting to advance their own interests. Others behave like avaricious mafias, who’ve carved Tripoli up into fiefdoms. One of the most powerful is the Nawasi Brigade. Named for a horse-riding school in an eastern neighbourhood, Nawasi became close to the GNA leadership, protecting its personnel and facilities. The UN has accused it of extorting money from Libya’s sovereign wealth fund. But it is also one of the most dependeable militias going up against Haftar in Tripoli. No doubt it will seek greater influence after the war. The GNA’s reform-minded interior minister, Fathi Bashaga, told me he won’t let that happen. ‘No forgiveness just because you fought Haftar,’ he said.

But the militias that cause the most concern may be those from Bashaga’s hometown, the port city of Misrata, Libya’s economic and industrial powerhouse, a three-hour drive east of the capital. They defended the city from Gaddafi during a brutal, months-long siege in 2011. They also provided the bulk of the fighters for the battle against Islamic State. Now some of them are back at war against Haftar and paying a high price: Misratan deaths account for about half of all GNA losses. Other militia commanders worry that Misrata will ‘collect the bill’ for these losses, in the form of appointments to government ministries and access to financial assets in the capital.

Meanwhile, Tripoli is reeling from the war. Militias on both sides have taken over civilian buildings and used them as ammunition depots or sniper perches. Medical workers have been killed in the clashes, and food and water supplies are under threat. More than 100,000 people have been forced to flee their homes.

The seaside suburb of Tajura is a twenty-minute drive from the front line. Roughly ten thousand displaced people live there in makeshift shelters, in schools, hotels and factory dormitories. Tripoli’s water main doesn’t reach the area and a storage tank is contaminated. Tensions run high in tight quarters. The locals are not always welcoming; municipal officials said the owners of beachside cabanas are kicking out some of the displaced families.

On 3 July, an LNA airstrike hit a migrant detention centre in Tajura, killing more than fifty people. Most of the blame for the massacre falls on Haftar’s forces for bombing a civilian facility for which they had the co-ordinates, but the GNA bears some responsibility for placing an arms depot next to the detention centre, and so does the EU for its policy of returning migrants to a conflict zone.

All over the capital, there are frequent electrical outages, and long lines at banks and petrol stations. An already decrepit health sector is collapsing, strained by casualties. Rubbish collection, already inadequate, has worsened because the trucks can’t get to dumps to the south. In the upscale neighbourhood of Hay al-Andalus, city officials said there had been an increase in robberies and carjackings because the militias who had been acting as police have deployed to the front.

One hot afternoon, I took a tour of the walled Old City with a group of young Libyans working to preserve its treasures through photographic documentation and education. Calling themselves Trabulusna (Our Tripoli), they are led by a woman named Hiba Shalabi who was spurred into action by the decay she observed: unregulated construction and endemic neglect. We walked through sunlit alleys strung with wire, past a carpet weaver, a former Jewish coffee house and the British consul’s former residence. Then on to a tiled mosque, a bathhouse, a Sufi school, and a church where Filipinos and Nigerians congregate.

Under a single electric bulb, apparently immune to the blackouts, an elderly artisan was fashioning silver jewellery in a small shop. I asked him about the war. ‘What war?’ he smiled.