David Cameron has told Libyans that "the British people want to stand with you" as he visited the country on the second stage of his African trip.
The prime minister was greeted by the public in Martyrs' Square in the capital Tripoli, having spoken to recruits at a police training college.
He has also met Prime Minister Ali Zidan and President Mohamed Magarief.
At a press conference, he announced that police investigating the 1988 Lockerbie bombing are to visit Libya.
Officers from the Dumfries and Galloway force had been granted permission to pursue their investigations in the country, he said.
The BBC's political correspondent Tim Reid said discussions had been taking place about the issue since the overthrow of Colonel Gaddafi in 2011 and while the Libyan authorities had always been supportive, it is only now that it has been approved.
Abdelbaset al-Megrahi, the only person ever convicted of the bombing, died last year after having been released from a Scottish jail in 2009.
Downing Street had requested a news blackout ahead of the prime minister's arrival from Algeria for security reasons.
'Good to be back'
Mr Cameron, who is being accompanied by his national security adviser and the head of MI6 on the trip, told police recruits at the training centre, which is receiving support from the British government, that it was "very good to be back".
In September 2011, Mr Cameron travelled to Libya with the then French President Nicolas Sarkozy to celebrate the liberation of this country from Colonel Gaddafi.
"I will never forget the scenes I saw in Tripoli and Benghazi," he said.
"The British people want to stand with you and help you deliver the greater security that Libya needs.
"So we have offered training and support from our police and our military. We look forward to working together in the years ahead."
Earlier this week, the Foreign Office warned of a "potential threat" to the British embassy in Tripoli.
This came less than a week after UK citizens were urged to leave the second city, Benghazi, because of a "specific and imminent threat to westerners".
The security situation has deteriorated since the PM's last visit. As he toured Martyrs' Square, a police helicopter hovered overhead and security forces were close at hand.
Acknowledging Libya faces major security challenges, Mr Cameron told the BBC's political editor Nick Robinson the country was on a "long, painful, hard road to a genuine, secure and stable democracy".
The UK, he added, would assist Libya to take the necessary steps to improve its security, such as help with disbanding militia, training the army and supporting a singe police force.
He also defended the French intervention in neighbouring Mali and rejected suggestions that foreign involvement in Muslim countries was the best recruiting sergeant for al-Qaeda.
On the contrary, he said terrorist groups would be encouraged if the international community ignored "ungoverned spaces and chaotic countries" and allowed them to fester - adding that this "was the lesson from Afghanistan and Somalia".
The UK's former Ambassador to Libya, Sir Richard Dalton, told the BBC that Libya faced many challenges, including agreeing a constitution, setting up new ministries, integrating the militias and diversifying an economy that has been heavily reliant on oil exports.
"But Libya has got a lot going for it still because its people are determined to protect the new freedoms that they have," he added.
Visiting Algeria on Wednesday in the first leg of his trip to Africa, the prime minister said the international community should use "everything at its disposal" to fight terrorism.
The recent hostage crisis at the In Amenas gas plant, in which some 37 foreigners died, was "a reminder that what happens in other countries affects us at home", he said.
He was the first British prime minister to visit Algeria since it became independent in 1962.