Consumed by anxiety and depression, Mr. Bissonnette told police he contemplated suicide at 16. He developed an alcohol problem. He wanted to become a pilot but got a job at a call center instead.

After Michael Zehaf-Bibeau, a Muslim, attacked the Canadian Parliament in October 2014, Mr. Bissonnette bought his first gun. It was one of several he would purchase legally, omitting his psychological problems from application forms, according to Marc-André Lamontagne, a psychologist who interviewed him in prison.

Six weeks before the mosque attack, Mr. Bissonnette told Mr. Lamontagne he went to a Quebec City shopping center to kill people and himself, but couldn’t go through with it.

“He had fantasies to do something big so that people would not laugh at him after his death, so that people would remember him,” Mr. Lamontagne said.

At the Quebec mosque, its windows still pocked with bullet holes, the Muslim community is still struggling to come to terms with the attack and their fear and grief.

The mosque’s president, Mohamed Labidi, said anti-Muslim incidents intensified before and after the attack. Before the killing, a severed pig’s head was left on the mosque’s doorstep. When he announced plans to build a Muslim cemetery on city land six months after the attack, his car was set on fire.

He blamed far-right groups and anti-immigrant “trash radio” hosts for creating a “toxic climate.”

Amir Belkacemi, the son of Khaled Belkacemi, said his father had come to Canada from Algeria in the 1990s to escape terrorism, only to become ensnared by violence here. Still, he said he had no intention of leaving the place he calls home.

“Quebec didn’t create the monster Alexandre Bissonnette,” he said. “But the Islamophobia here gave him his motive.”