A former student pleaded guilty on Wednesday to killing 17 people in a shooting rampage at a high school in Parkland, Florida and apologized in court to relatives of the victims.

Nikolas Cruz, who was 19 at the time, took a legally purchased AR-15 assault rifle into Marjory Stoneman Douglas High School, his former school, on Valentine's Day in 2018 and killed 17 students and staff members.

Cruz pleaded guilty to 17 counts of first-degree murder and 17 counts of attempted murder for those who were wounded during the attack, which sparked a student-led movement for tighter gun control laws.

Cruz, who was dressed in a blue collared shirt and sweater vest and wearing large glasses and a face mask, responded "guilty" as Judge Elizabeth Scherer read off each of the charges in a packed Broward County courthouse.

Cruz will now go before a jury for the penalty phase of the trial. He faces a minimum of life in prison without parole but prosecutors have said they will seek the death penalty.

Elizabeth Smith (L) and Lindsey Riha, teachers at Marjory Stoneman Douglas High School, hug each other as teachers and staff returned to the school for the first time since the mass shooting on campus
Elizabeth Smith (L) and Lindsey Riha, teachers at Marjory Stoneman Douglas High School, hug each other as teachers and staff returned to the school for the first time since the mass shooting on campus GETTY IMAGES / JOE RAEDLE

Following his guilty pleas, Cruz, who is now 23, apologized to the relatives of his victims.

"I am very sorry for what I did and I have to live with it every day," he said. "It brings me nightmares."

"If I were to get a second chance I would do everything in my power to try to help others," he said.

Addressing relatives of the victims, Cruz said, "I believe it's your decision to decide where I go, whether I live or die, not the jury's."

Relatives of some of the victims were among the spectators in the courtroom and wiped away tears as a prosecutor recounted the attack in chilling detail.

Relatives of victims of the February 2018 shooting at a high school in Parkland, Florida, arrive for the plea hearing of school shooter Nikolas Cruz
Relatives of victims of the February 2018 shooting at a high school in Parkland, Florida, arrive for the plea hearing of school shooter Nikolas Cruz GETTY IMAGES / POOL

The shooting was the worst school massacre in the United States since the horror at Sandy Hook Elementary School in Newtown, Connecticut, in 2012, which left 26 dead.

The Florida shooting stunned the country and sparked new efforts, led by students from the school itself, for tougher gun control -- although the polarized US Congress has yet to enact meaningful gun reform.

A rally organized by the Parkland students, "March for Our Lives" drew hundreds of thousands to the nation's capital in March 2018.

Cruz bought the weapon legally, despite having been in local records as having a history of mental health problems.

Expelled from school for disciplinary reasons, Cruz was known to be fixated on firearms -- and had reportedly been identified as a potential threat to his classmates.

The FBI confirmed it was alerted several months before the attack to a message posted on YouTube, in which a user named Nikolas Cruz vowed: "I'm going to be a professional school shooter."

On the day of the attack Cruze arrived at the school in an Uber, began shooting, and left the scene nine minutes later, leaving behind a scene of carnage.

Footage recovered from his phone showed he had filmed his plans to attack his former school, saying his goal was to kill "at least 20 people."

Cruz told a detective after his arrest that he heard demons ordering him to "buy weapons, kill animals and destroy everything."