David Cameron defended the British role in toppling Muammar Gaddafi during a surprise one-day visit to the Libyan capital, Tripoli, on Thursday, arguing that it was a "complete fiction" to think his departure made the west more vulnerable.

The prime minister flew in from Algiers, despite recent threats to the British embassy and consulates, in a personal statement of support for the Arab spring and the new Libyan government, which is struggling to assert its authority against militias and lack of resources.

He said that the history of brutal dictatorships in north Africa and the Middle East was that they stored up problems for the future and in the long run "we are safer if we have a secure, stable democracy. That is why to me the Arab spring is still part of the solution and not part of the problem".

Asked if he was becoming similar to Tony Blair, Cameron said: "The similarity is that any sensible modern British politician has to recognise that Britain has interests in the world and should look outwards and work with others for a more stable, secure and democratic world. Where I think we need to learn the lessons of the past failures is that helping other counties, intervening in other countries, is not simply about military intervention."

On his arrival at the airport, Cameron travelled in a heavily armed 16-vehicle convoy to visit a sprawling police training college outside Tripoli.

Before meeting the prime minister, Ali Zaidan, who is trying to construct a government of national unity and disband the militias that dominate the country, Cameron promised to do more to help the country. Greeted by a band replete with bagpipes, he received strong applause and shouts of "God is great" when he pledged: "In building a new Libya you will have no greater friend than the United Kingdom. We will stand with you every step of the way."

Urging the recruits – arranged in front of him in a sunlit courtyard – to stick to their job, he said: "There is no real freedom, no real democracy, no real chance of prosperity without proper security. There is no real freedom without honour and honesty," adding: "The most important pledge you make is to uphold the law and fight corruption."

Amid tight security, he then walked through Martyrs' Square, where the revolution against Gaddafi began nearly two years ago. He met some of the young revolutionaries who led the uprising, as well as bemused locals.

Cameron was greeted by jostling crowds and Libyans taking photos of him as he discussed the fate of the revolution just 100 metres from Gaddafi's old palace. He was in the square for about 15 minutes in a relatively relaxed walkabout.

The prime minister met Mervat Mhani, who photographed some of the abuses during the revolution and who is working for the ministry of missing persons and martyrs. He also met Abdulrahman Alajeli, who has Libyan-UK dual nationality and who co-founded of the Libyan Youth Forum.

Cameron visited Tripoli and Benghazi with the then French president, Nicolas Sarkozy, in September 2011 in the wake of Gaddafi's fall, receiving a hero's welcome. The Nato-sanctioned no-fly zone operated by France and UK cleared the way for the uprising. This second visit has been planned for months and is designed to show that the prime minister is willing to tackle the consequences of the revolution he helped spawn.

In a sign of deterioration in the country, the Foreign Office has warned in recent days of threats to the British embassy in Tripoli and advised Britons to leave Benghazi because of a threat of attack. The same warning was issued to German and Dutch citizens.

There is a growing fear that Libya is becoming an incubator of turmoil, with weapons flooding the streets and jihadi militants ready to disrupt civil order. The central government has little authority beyond Tripoli. One militia, Ansar al-Sharia, is believed to have been behind the 11 September attack on the US consulate in the city that killed four Americans including the ambassador. Security and government officials have been victims of a wave of assassinations.

Critics of the Anglo-French intervention, including the Russian president, Vladimir Putin, claim that those who backed the removal of Gaddafi had not thought sufficiently about the aftermath.

British officials acknowledge that the Libyan government badly needs help to shore up its authority as an administration, including a functioning civil police and integrated army. Many militias operating in cities are only willing to join the Libyan National Army as a unit, and the government has done little to disband them.

There are well over 1,000 armed groups in the country. There is also concern that the Libyan revolution is destabilising the wider region. After Gaddafi fell, many Tuareg fighters loyal to the former dictator fled the country, returning to Mali, and have been central to the initial collapse of the Mali government in the north.

Cameron admits that the removal of dictators can reveal hidden fractures in society, but says the only answer is a mixture of openness and effective government. In his talks with the government, he will promise to stand by the Libyans and to improve governance, including a package on policing and defence aid.

• This article was amended on 3 February 2013. David Cameron was originally described addressing the police recruits "arraigned" in front of him.