Tony Blair says British troops will stay in Afghanistan until their job is done and has praised its president for the "remarkable progress" made there.
He told reporters he believed Afghan people deserved to live in a democratic country without oppression.
"I want to recommit ourselves to say that we will be with you in this endeavour," the UK prime minister said, after talks with the Afghan president.
Earlier he had visited British troops in the south of the country.
Addressing President Hamid Karzai in a joint press conference in the capital Kabul, Mr Blair said: "The progress that Afghanistan has made under your leadership over these past few years has indeed been remarkable."
There had been economic growth over several years and a lot of reconstruction and development was under way, he said.
He said the UK would help the country to meet challenges of security, reconstruction and development.
"We believe that Afghanistan, rather than being abused as a haven for terrorists and for the Taleban to oppress people, that Afghanistan and its people deserve the chance to increase their prosperity and to live in a proper democratic state.
"The roots of the Taleban, al-Qaeda, this type of global terrorism around the world, are deep and where they gained a foothold in a country like Afghanistan, it is going to take time to banish that for good," he said.
But he added there was sufficient sign of progress that it should give some hope for the future.
He insisted the international community had no choice but to continue to fight against the insurgency.
"What is the alternative? We came to Afghanistan because the sickness and evil that was in Afghanistan came to us," he said, referring to the 9/11 attacks in the US.
President Karzai said progress had been made with 4.5 million refugees returning to Afghanistan in less than four years.
"While the Taleban were here they were running away from Afghanistan," he said.
"Now they have come back, and more are returning as we talk, from our neighbourhood and from the rest of the world."
Mr Blair last visited troops in Afghanistan in 2002
He said the country's economy and the health service had also improved but acknowledged the opium harvest had increased in some parts of the country.
He said it was naive to think Afghanistan could be wiped clean of drugs in two years.
It was important to end the trade because it threatened future security and economic prospects and had made addiction a major concern in the country, he added.
While in Kabul, Mr Blair also met Lt Gen David Richards, the British officer commanding the Nato force.
Gen Richards said despite fierce fighting, international forces were gaining ground against the Taleban, and he believed the battle was "winnable".
Earlier, Mr Blair met 800 British servicemen and women at Camp Bastion in Helmand province to thank them for the job they were doing.
"Here in this extraordinary piece of desert is where the future of world security in the early 21st Century is going to be played out," he said.
The visit was intended to boost morale but Liberal Democrats accused Mr Blair of failing to address problems of "overstretch and equipment shortages".
Lib Dem defence spokesman Nick Harvey said bringing peace would be impossible without dealing with the "fundamental operational difficulties".
The troops are among a 6,000-strong British force fighting the Taleban. So far this year, 36 have been killed.
Mr Blair has not visited British troops in the country since soon after the US-led invasion in 2002.
On Sunday, Mr Blair was in Pakistan where he and President Pervez Musharraf agreed to strengthen their ties to fight terrorism.