Federal authorities on Wednesday charged the 29-year-old Uzbek immigrant they say plowed a rented truck into people walking and cycling on a New York City bike path — killing eight — with providing support to a terrorist organization, alleging that he was inspired by the Islamic State to inflict mayhem and chose Halloween because he figured there would be more people on the street.

In court papers, federal authorities said that Sayfullo Saipov told the FBI that he began planning his attack a year ago, though he settled on using a truck as a weapon only two months prior.

Saipov said he was proud of what he had done — even requesting to display the Islamic State flag in his hospital room — and that he was inspired particularly after he watched a video of Islamic State leader Abu Bakr al-Baghdadi, court papers say. In that video, Baghdadi questioned what Muslims in the United States were doing to respond to the killing of other members of their faith in Iraq, Saipov told the FBI.

In addition to the terrorism charge, Saipov faces one count of violence and destruction of a motor vehicle — a charge that, because prosecutors allege someone died as a result, could carry with it the death penalty.

Just hours after the charges were filed, President Trump weighed in with a tweet calling for Saipov to get the death penalty, and he repeated that sentiment on Thursday morning. Trump and his White House had suggested they could move Saipov into the military justice system, rather than the federal court system, before charges were filed, and Trump on Thursday backed away from that idea.

“There is also something appropriate about keeping him in the home of the horrible crime he committed,” Trump tweeted Thursday morning. “Should move fast. DEATH PENALTY!”

Trump’s comments could complicate the case, in which federal prosecutors have not yet decided whether to seek such a punishment, because defense attorneys could argue that such remarks from the president may prevent a jury from giving the suspect a fair trial.

Police and FBI officials decried the incident as the worst terrorist attack in New York since Sept. 11, 2001, and they immediately launched an international investigation to uncover how and why it happened. They urged New Yorkers to carry on as normal, even as they said they would beef up security at this weekend’s New York City Marathon and explore other steps to prevent a similar incident.

“What happened yesterday was not okay,” New York City Police Commissioner James P. O’Neill said. “It will never be something that any of us just accept as inevitable.”

Saipov, who drove for Uber and once had his own trucking company, apparently was radicalized after he came to the United States, and a note written in Arabic that he left at the scene extolled the Islamic State, authorities said.

Translated to English, the note said, in part: “Islamic Supplication. It will endure,” court papers say.

“The gist of the note was that the Islamic State would endure forever,” said John Miller, the deputy commissioner of intelligence and counterterrorism at the New York City Police Department.

David Patton, Saipov’s attorney, said: “In a case like this involving so much tragedy, it’s more important than ever to let the judicial process play out. How we as a society treat Mr. Saipov will say more about us than it will about him.”

It was not immediately clear whether investigators had found any evidence that others had knowledge of, or assisted with, the plot. The FBI said briefly Wednesday night that agents were looking for another man, identified as 32-year-old Mukhammadzoir Kadirov, or Muhammad Kadirov, in connection with the investigation. But they gave no indication why they were doing so or if he was suspected of having a role, and just minutes later, they said they were no longer seeking him.

“We believed he had information related to yesterday, but we are not looking for that individual any longer,” said FBI Assistant Director in Charge William F. Sweeney Jr., adding, “We have found him, and I’ll leave it at that.”

A person who was in touch with both Saipov’s and Kadirov’s families on Wednesday said that Kadirov is in New Jersey, has retained an attorney and is cooperating with law enforcement officials, but that he was not under arrest as of Wednesday evening.

The person, who spoke on the condition of anonymity because of the sensitivity of the subject, said Kadirov is Saipov’s cousin. The person, who spoke to Kadirov over the phone, said Kadirov seemed “utterly shocked and horrified” by what Saipov had done.

Investigators found on Saipov’s phones 90 videos and 3,800 images, many of which seemed to be Islamic State propaganda, videos of the group’s fighters killing prisoners or bomb-making instructions. He told agents that he wanted to kill as many people as he could and considered putting Islamic State flags at the front and back of his truck — but ultimately decided it would draw too much attention, court papers say.

National security officials have warned that as the Islamic State is being routed in Syria and Iraq, the group’s supporters might try to carry out crude attacks in the places where they live. The group has urged its supporters to use vehicles as weapons, and the tactic has been employed in France, Britain, Germany, Sweden, Spain and Canada.

The group, though, did not immediately claim responsibility for the incident in New York, according to the SITE Intelligence Group, which monitors extremist activity.

Officials on both sides of the political aisle agreed that Saipov’s rampage was an act of terrorism, but there was almost immediate disagreement on how to respond.

Trump decried the way Saipov entered the United States — via a State Department program that grants visas to people from countries with historically low rates of immigration to the United States — and said he would ask Congress to end it. On Twitter, Trump asserted that he would increase already “extreme” vetting of those wanting to come to the country, and he took a swipe at Senate Minority Leader Charles E. Schumer (D-N.Y.).

New York Gov. Andrew M. Cuomo (D) fired back that Trump’s tweet was “not helpful” and said he was “bothered by an attempt by anyone to politicize this situation.”

The president also said he was considering sending Saipov to the U.S. military prison at Guantanamo Bay, Cuba, and White House press secretary Sarah Huckabee Sanders said later that “I believe we would consider this person to be an enemy combatant.”

Sanders, though, did not seem to affix any legal meaning to the term, and of sending Saipov to Guantanamo Bay, she said Trump was trying to indicate that he “would support that, but he wasn’t necessarily advocating for it.”

Even the suggestion of labeling Saipov an enemy combatant or putting him in Guantanamo Bay — which probably would spark years of legal wrangling — drew immediate condemnation from civil liberties groups. The court system at the military prison is still struggling to bring to trial those accused of playing roles in the 9/11 attacks.

“Military detention would be unconstitutional, contrary to congressional statutes and unnecessary,” said Hina Shamsi, director of the ACLU National Security Project.

On Tuesday afternoon, Saipov is alleged to have ultimately crashed into a school bus and emerged from his truck armed with a paintball gun and pellet gun. A passer-by flagged down police officers responding to an unrelated call at a school in the area, and one of them shot and wounded Saipov, police said.

Court papers say Saipov shouted “Allahu akbar,’’ meaning “God is great’’ in Arabic, after he got out of the truck. Investigators found a stun gun on the floor of his truck and three knives in a black bag he was carrying, court papers say.

Among those Saipov is accused of killing were a group of childhood friends from Argentina, now in their late 40s, who had been planning a trip to New York for years; a young mother; and two men in their 20s and 30s from New York and New Jersey.

Police said Saipov had never been the “subject” of an FBI or New York police investigation, though they stopped short of saying he was unknown to law enforcement. Miller said “he will have some connectivity to individuals who were the subjects of investigation, though he himself was not.” Law enforcement officials said the FBI has been investigating a friend of Saipov’s, but that probe had not previously found derogatory information against Saipov.

The FBI has, in the past, scrutinized people who have gone on to commit attacks. Perhaps most notably, agents investigated Omar Mateen, who shot and killed 49 people last year in an Islamic State-inspired attack in an Orlando nightclub, for 10 months in 2013, even putting him under surveillance and recording his calls before ultimately closing the case.

A perpetrator of the Boston Marathon bombing in 2013 also drew scrutiny before that attack.

Saipov arrived in the United States in March 2010 and lived in Ohio, Florida and New Jersey, according to authorities. He started a trucking company, was married, had children and at one point, worked for Uber, where he passed a background check. His wife has told the FBI that she did not know he was planning violence, a person familiar with the case said.

Those who knew Saipov said they were surprised by what had happened — though some acknowledged that he had antagonistic tendencies.

Mirrakhmat Muminov, 38, a truck driver in a suburb of Akron, Ohio, said that Saipov lived in his community and worked part-time for his trucking company for a few months. But he had to fire him because he was often late for deliveries and was aggressive with customers.

“He was always aggressive and nervous,” Muminov said in an interview. “We warned him in our community: ‘Don’t be nervous. You have to stay calm.’ ”

Devlin Barrett writes about national security and law enforcement for The Post. He has previously worked at the Wall Street Journal, The Associated Press, and the New York Post, where he started as a copy boy.