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 jihadist ties but was released for lack of evidence, a security official said.

Tunisia, one of the Arab world's most secular nations, is struggling to counter Islamist militancy since becoming a beacon of democratic change in the region after its 2011 uprising that ousted autocrat Zine El-Abidine Ben Ali.

Houssem Abdelli, a street vendor from an impoverished neighborhood of Tunis, detonated his explosives as presidential guards boarded a bus 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 jihadist literature in his house and neighbors noticing changes in his behavior over the last few years – shows how security forces are struggling to prevent attacks.

FILE - Tunisian President Beji Caid Essebsi decorates members from the presidential guards who were killed in a bomb blast on a bus in central Tunis the previous day during a ceremony at Carthage Palace, Tunis, Nov. 25, 2015.

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 a young man who showed few signs of his new violent ideology.

Three attacks, three Tunisians

All three of this year's major attacks in Tunisia have been claimed by the Islamic State, the militant group controlling large parts of Iraq and Syria. All three were carried out by Tunisians who appear to have been radicalized at home or trained in jihadist 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 Rafik Chelli told local radio.

Neighbors 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 jihadists returning from war zones.

Authorities said 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.

FILE - A man places a bouquet of flowers on a police van, near the bus that exploded Tuesday in Tunis, Nov. 25, 2015.

More than 3,000 Tunisians are now fighting for the Islamic State or other militant groups in Iraq, Syria and neighboring Libya.

Some have threatened to return to stage attacks in Tunisia.

Neighbors see change in Abdelli

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 jihadist literature in his possession, officials said.

Neighbors in his poor district near Tunis' Ettadhammen area said that, until a few years ago, he enjoyed football (soccer) 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 neighborhood. He would not talk to girls anymore and spent most of the time in the mosque or at one of the koranic schools."

Mohamed, a neighbor, 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," Mohamed said.

One of the victims of Abdelli's bombing lived in the same poor neighborhood. 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," neighbor Fatma Ben Saleh said. "We all know his mother and father. They were just a normal family."