Ahmad Alhaw, 27, had admitted to the murder and assaults in the river port city of Hamburg.
In delivering the sentence, the judge noted the “particular severity” of the crime, lowering Alhaw's chances of obtaining early release on parole.
Prosecutors had said the attack stemmed from a “radical Islamist” motive, charging that the man had hoped “he would die as a martyr”.
His aim was to kill as many German Christians as possible to avenge the suffering of Muslims worldwide, they said.
There was no indication he belonged to the so-called Islamic State group or any other extremist organisation.
The defendant entered a supermarket on July 28th last year, snatched a 20-centimetre (eight-inch) knife from the shelves, unwrapped it and fatally stabbed a 50-year-old man.
As panic broke out, he slashed and stabbed at others inside and then outside the shop, shouting “Allahu Akbar” (God is greatest), and also wounding a 50-year-old woman and four men.
“I saw him run toward me, covered in blood. He was radiating bloodlust,” a 20-year-old supermarket worker who locked himself in a backroom recounted in court.
Outside the supermarket, seven mostly migrant bystanders confronted the screaming attacker, hurling rocks and chairs at him and then subduing him until police arrived.
The men – among them a 30-year-old man of Turkish descent who was wounded in the struggle – have since been celebrated and honoured for their courage.
One of them, Afghan asylum seeker Toufiq Arab, told Bild daily: “I'm not a hero, I only did my duty.”
The Islamist attack was Germany's first since December 2016 when Tunisian Anis Amri ploughed a stolen truck through a Berlin Christmas market in an attack that claimed 12 lives.
The knife rampage led to charges that security services had been too complacent in keeping an eye on Alhaw despite multiple warnings that he was radicalising.
It also fuelled growing xenophobia against Muslim migrants after the mass influx of more than one million asylum seekers since mid-2015.
Witnesses told the court they had seen the failed asylum seeker self-radicalise in the lead-up to the attack.
Like Amri, Alhaw was due to have been deported after his asylum application was rejected, but the process was held up by a wait for identity documents.
Having been rejected for asylum in Norway, Sweden and Spain, he had arrived in Germany in 2015 and was moved to a Hamburg migrant accommodation centre, which after the attack was raided by heavily armed police.
A psychiatrist said Alhaw had told him he was initially drawn to the West because of its open lifestyle.
At first he studied the German language, socialised and at times drank
alcohol and smoked marijuana, said witnesses.
But Alhaw, who had watched IS propaganda videos online, grew deeply religious and increasingly distrustful of state authorities.
By 2016 he thought that he suddenly understood the Koran completely, rejected all previous temptations, and started looking at all other people as either true believers or non-believers, the psychiatrist said.
Alhaw said the knife attack had not been his decision but God's will.
But he surprised the court in a February 19 hearing when he appeared with his beard shaved off and for the first time showed remorse.
“Unfortunately I can't turn back time,” he told the victims and the relatives of the man he killed. “All I can do is to ask you for forgiveness.”