Tunis: The suicide bomber in Tunisia who blew himself up in a bus packed with presidential guards on Tuesday had been arrested by police before on suspicion of militant ties but was released for lack of evidence, a security official said.

Tunisia is struggling to counter Islamist militancy since becoming a beacon of democratic change in the region after its 2011 uprising that ousted autocrat Zain Al Abidine Bin Ali.

Hussam Abdelli, a street vendor from an impoverished neighbourhood of Tunis, detonated his explosives as presidential guards boarded a bus on Tuesday afternoon on one of the capital’s main boulevards, killing 12 people.

The potential missed opportunity to stop a suicide bomber - despite authorities finding militant literature in his house and neighbours noticing changes in his behaviour over the last few years - shows how security forces are struggling to prevent attacks.

Abdelli’s bus bombing follows massacres targeting a Sousse resort hotel and the Bardo national museum in Tunis earlier this year.

Like other homegrown attackers before him and the Tunisians who left to fight for militant groups in Iraq and Syria, Abdelli appears to have followed a familiar path from young man who showed few signs of his new violent ideology.

All three of this year’s major attacks in Tunisia have been claimed by Daesh, the militant group controlling large parts of Iraq and Syria. All three were carried out by Tunisians who appear to have been radicalised at home or trained in militant camps in Libya.

“This terrorist was arrested by the police and then freed by the justice system for lack of evidence,” Interior Ministry security chief Rafiq Shelli told local radio.

Neighbours said Abdelli was arrested in August. Officials declined to give precise dates for his arrest and release.

Tuesday’s attack was the first suicide bombing in the capital and forced the government to implement a curfew, declare a state of emergency and promise harsh measures to protect against militants returning from war zones.

Authorities said on Friday they had arrested 40 people with suspected links to militant groups, and had issued 92 house arrest orders for people suspected of returning from Syria, Iraq and Libya.

More than 3,000 Tunisians are now fighting for Daesh or other militant groups in Iraq, Syria and neighbouring Libya.

Some have threatened to return to stage attacks in Tunisia.

It was not clear whether Abdelli had left the country to fight overseas or train before he carried out his attack. Police had arrested him after finding militant literature in his possession, officials said.

Neighbours in his poor district near Tunis’ Ettadhammen area said until a few years ago he enjoyed football enough to earn himself the nickname “Pele” after the Brazilian star.

“We used to play football together,” his cousin Atef said.

“He changed about 3 years ago, and he started to avoid us. He became isolated in the neighbourhood. He would not talk to girls any more and spent most of the time in the mosque or at one of the Quranic schools.” Mohammad, a neighbour, also remembers the change in the last few years. He said Abdelli spent a lot of time selling small cakes outside the mosque from a small cart.

“He was arrested after they found propaganda in his house calling for jihad in Iraq and Syria, but they freed him quickly,” said Mohammad.

One of the victims of Abdelli’s bombing lived in the same poor neighbourhood. Omar Katayi was one of the guards on board the same bus where Abdelli detonated his explosives.

One local woman said she had seen Abdelli just last week in jeans and with his beard shaved off, perhaps, she said, not to attract attention with his usual more conservative appearance.

“We are all in shock,” said neighbour Fatma Ben Saleh. “We all know his mother and father. They were just a normal family.”