The heat and dust of the 27th Brigade parade ground on Tripoli's southern outskirts is a world away from the mist and cold of Cambridgeshire. Under an unforgiving sun, young Libyan army recruits in buzz-cuts march up and down to barked commands, dark sweat stains spreading across crisp new green uniforms. But next month they will be swapping this heat for the cold and damp of eastern England's Bassingbourn barracks, from where they will return as the speartip of Nato's new military intervention in Libya.

Two years after its bombing campaign spurred the rebels to victory in the Arab spring, Nato is coming back, this time to create an army to fill Libya's security vacuum. Alarm is spreading in western capitals about the country's growing chaos and militia violence, and evidence that al-Qaida is setting up bases in what is rapidly becoming a failed state. Having destroyed most of Libya's army in the 2011 bombing campaign, Britain and the United States are leading an operation to build a new one.

Since the revolution, militias formed to fight the regime of Muammar Gaddafi have morphed into private armies, led by charismatic warlords, their fighting and gangsterism bringing the country to its knees. Outside the capital, much of this desert country resembles a Mad Max film set, its highways crisscrossed by militiamen in exotic battle wagons featuring anti-aircraft guns and rocket tubes. Militia blockades have halted oil production, starving the government of cash. In the cities there is stagnation, violence, power blackouts and petrol shortages; this in the country that is home to Africa's biggest oil reserves.

Diplomats fear that the chaos will see Libya become another Afghanistan or Somalia, a lawless land where al-Qaida can find a home, this time on the shores of the Mediterranean.

The killing of an American high- school teacher in the eastern city of Benghazi last week is a reminder of the violence afflicting the country. The city has seen two weeks of battles between the army and Ansar al-Sharia, an Islamist militia blamed by some for the killing of US ambassador Chris Stevens in the city last year. Benghazi's small army garrison has declared it is too weak to hold the city, appealing to the government for help it cannot give.

Hence the training. Bassingbourn was a second world war base for the US air force, best known as the home of the Memphis Belle bomber, immortalised in a Hollywood film of the same name. Libya has paid £2.5m to have it taken out of mothballs and readied for the first batch of recruits, part of a force of up to 15,000 that Britain, Italy, Turkey and the US will train over the next two years.

On the Tripoli training ground, optimism is in the air as officers review their marching recruits. "This is a big chance for Libya," says General Mohammed Buzeiud, the very image of Field Marshal Montgomery with his pinched face, sharp eyes, lean frame and UK-style battledress. "We have seen a change in these men already in the last four weeks. They will get better with British training."

Details of the programme were hammered out last summer at the G8 summit in Lough Erne, with Nato taking units from newly formed brigades, such as the 27th, for 10-week stints of intensive infantry training. "They will get basic infantry skills, vehicle checkpoints, training on working within a command structure," says one western diplomat.

Along with the training has come a new military engagement. US drones are in the sky, flying from bases in Niger and Italy. A Nato mission, with an undisclosed mandate, has been deployed. The European Union is training coastguard and border patrol units, while the UN announced last week that it will send 235 troops to guard its Tripoli headquarters.

This "intervention-lite" comes with risks. The most powerful militias are backed and financed by sections of Libya's General National Congress, elected last year. The relatively secular parties back the western Zintan militia, while the Muslim Brotherhood and an equally fragmented coalition of Islamists back forces from Misrata. Each militia is seen as a bulwark against the other. Neither fully trusts the new army. The Muslim Brotherhood, whose Islamist coalition holds a narrow majority in congress, is wary of both Nato training and the fact that the new army is staffed by Gaddafi-era officers.

"These new brigades are entering the capital, everybody is wondering where they came from," says Muslim Brotherhood official Abdelatif Karmoos, a Canadian-educated Tripoli university professor who spent eight years in Gaddafi's political prisons. "Some people say they are trained and organised to take over."

Buzeiud insists his officers were always loyal to the country, not Gaddafi, pointing out that the former dictator distrusted his regular forces, pouring resources into special brigades run by his sons. But the general knows his future depends not on drill learned at Bassingbourn but on the population's support: "The army can provide the security, but the army needs the help of the people. It is no good creating an army if you don't have the people behind them."

For the moment, he has that backing, at least in the capital, following the worst outbreak of violence seen since the end of the revolution. Last month a militia from Misrata, based in abandoned Gaddafi-era villas in the Ghargour district of the capital, fired anti-aircraft guns and automatic weapons into crowds of protesters. When the smoke cleared, 47 were dead. For many Tripolitanians, Ghargour was the last straw. A furious backlash saw what may be a sea change in Libyan politics, as militias were expelled and replaced by army units that arrived to cheering crowds and horns. The backlash has been named the Croissant Revolution, after the derogatory nickname, the Croissants, which outsiders had for the supposedly "soft "population of the capital. Now croissants are displayed at anti-militia protests, checkpoints, and in packets hung from the guns of tanks guarding Tripoli's entrances.

"People here just want peace, " says Aymen Hussain, manager of the downtown Safari hotel. "Since the militias have left and the army has come, crime is down. The militias won't be back, the citizens won't let them."

Across the road from the 27th Brigade, commanders at another base, the 17th Thunderbolt Special Forces Brigade, are sanguine about British training. Older hands remember that the west has a history of changing sides. In the 1980s the US bombed Gaddafi and there was Lockerbie and the killing of Yvonne Fletcher. Then Tony Blair turned Gaddafi from a foe to an ally, before the west changed again, bombing his forces in the 2011 revolution. The most famous recruit, trained in three bases in England in the 1960s, was Gaddafi himself. All of which leaves some wondering how long western support will last.

But the officers are diplomatic. "In the past the relationship with the UK was bad, but now it is good," says Colonel Mohammed Ibrahim, head of Libyan training command. "We hope this training can build a new alliance."

For Libya's fractured government, beset by political divisions and fast running out of money as the militia oil blockade tightens, western help is essential.

"Libya's stability is important to the west, they have a real interest in ensuring Libya does not turn into another Somalia," says justice minister Salah Marghani, a former lawyer praised by human rights groups for defending political prisoners during the Gaddafi era. Militias have twice stormed his ministry, demanding judges be replaced by "revolutionaries" and he apologises when the door to his office sticks. "It has been broken open so many times, it doesn't work properly."

With the government divided, the economy in a tailspin and battles continuing between security forces and militants in the east, he says western help is the only way forward. "We need this help, the clock is ticking."