Joaquín Guzmán Loera, the Mexican drug lord known as El Chapo, in federal custody on Long Island in 2017. Guzmán was sentenced on July 17 to life in prison.

NEW YORK — Joaquín Guzmán Loera, the Mexican drug lord known as El Chapo, was sentenced Wednesday to life in prison, ending one of modern history’s most brutal and notorious criminal careers.

The life sentence, mandated by law as a result of the severity of Guzmán’s crimes, was handed down in the U.S. District Court in Brooklyn, where the kingpin was convicted last winter of drug, murder and money laundering charges after a sprawling three-month trial.

As some of the federal agents who had chased him for years looked on from the gallery, Judge Brian M. Cogan issued the life term and Guzmán, 62, was hauled away to prepare himself — pending an appeal — for spending the rest of his life behind bars.

Cogan said the “overwhelming evil” of Guzmán’s crimes were apparent. Besides giving him a life sentence plus 30 years, he ordered the drug lord to pay $12.6 billion in restitution.

Speaking for several minutes before his sentencing, Guzmán said he had not received a fair trial and complained about his imprisonment in a federal jail in Manhattan, calling it “psychological, emotional and mental torture 24 hours a day.”

“Since the government of the United States is going to send me to a prison where my name will never be heard again, I take advantage of this opportunity to say there was no justice here,” he said.

Guzmán almost certainly will be sent to the country’s most forbidding federal prison, the U.S. Penitentiary Administrative Maximum Facility, or ADX, in Florence, Colorado.

Guzman’s career atop one of Mexico’s most powerful cartels came to a close only after Mexico agreed to extradite him to the United States in January 2017.

His ability to escape from prison and evade capture for years underscored the deep corruption of Mexican authorities by his cartel, which employed bribery and intimidation to control not just local police departments but also the highest-ranking officials in the national government.

“It’s justice not only for the Mexican government, but for all of Guzmán’s victims in Mexico,” said Raymond P. Donovan, the agent in charge of the New York office of the Drug Enforcement Administration, which was instrumental in capturing the kingpin twice.

The trial took place under intense media scrutiny and tight security that involved bomb-sniffing dogs, police snipers and federal marshals with radiation sensors.

Prosecutors leveled some of the most serious charges possible against him, presenting evidence that he sent hundreds of tons of drugs to the United States from Mexico and caused the deaths of dozens of people to protect himself and his smuggling routes.

The case revealed in exacting detail the inner workings of the Sinaloa drug cartel — such as how it employed information technology consultants and how it packaged its cocaine in rubber “condoms.”

But given the defendant’s fame and notoriety, the trial was also a boisterous legal circus, complete with a horde of international reporters, a steady trickle of curious “narco-tourists” and a cameo appearance by an actor who plays the drug lord on a Netflix show.

The verdict Feb. 12 came after more than a week of deliberations by the jury. Ultimately, Guzmán was found guilty on all 10 counts of the indictment.

As the verdict was read, he sat listening to a translator, looking stunned. When the reading of the verdict was complete, Guzmán leaned back to glance at his wife, Emma Coronel Aispuro, who flashed him a thumbs up with tears in her eyes.

On that day, Richard P. Donoghue, the U.S. attorney for the Eastern District of New York, called the guilty verdict a victory for law enforcement.

“There are those who say the war on drugs is not worth fighting,” Donoghue said. “Those people are wrong.”

Even Guzmán’s lawyers admitted defending the kingpin was a daunting task.

“I’ve never faced a case with so many cooperating witnesses and so much evidence,” said Jeffrey Lichtman, one of Guzmán’s lawyers. “We did all we could as defense lawyers.”

Two days after Guzmán’s conviction in February, two of the kingpin’s sons, Joaquín and Ovidio Guzmán López, were indicted by federal prosecutors in Washington.

Several days after that, a juror, who spoke anonymously, told a Vice Media reporter that several of the panelists had disobeyed repeated orders by the judge not to follow media coverage of the trial.

The Vice article prompted Guzmán’s lawyers to submit a motion requesting a new trial. For a few weeks, it seemed possible that the jurors might be hauled back into court for a hearing to determine if they had in fact committed misconduct.

But in early July, Cogan denied the new trial motion and moved things forward toward the sentencing.

U.S. authorities began their hunt for the kingpin as far back as the early 1990s, when he was indicted on separate federal charges in Tucson, Arizona, and San Diego.

The two indictments were filed just before and somewhat after he was arrested while on the run in Guatemala and then returned to Mexico, where he was tried and imprisoned for the 1993 murder of Juan Jesús Posadas Ocampo, a beloved Roman Catholic cardinal.

In 2001, however, Guzmán broke out of prison — by many accounts, in the bottom of a laundry cart — and spent the next 13 years playing cat-and-mouse with the law.

He evaded both arrest and the five subsequent indictments filed against him in the United States, largely by shuttling between a series of hideouts in the Sierra Madre mountains in the Mexican state of Sinaloa.

In February 2012, Mexican and U.S. authorities came within inches of nabbing him in an ocean-view mansion in Cabo San Lucas, Mexico.

But it was not until Donovan and a vast coalition of law enforcement and military officers on both sides of the border mounted an enormous wiretap operation that cracked the kingpin’s communications network that he was caught. He was found in a beachfront condominium in Mazatlán in February 2014.

Within a year and a half, however, he had escaped again — this time, through a sophisticated tunnel that opened into the shower of his prison cell. A coalition similar to the one that caught him in 2014 redoubled its efforts and captured the kingpin for a second time, after a violent gunfight, in Los Mochis, Mexico, in early 2016.

When Guzmán finally stood trial in New York in November, his conviction was all but assured given the mountains of evidence collected against him over the years.

Some of that evidence came from incriminating intercepts from the various wiretaps over which agents had for months been listening in on the kingpin and his underlings. But just as damaging were the 14 witnesses from inside his Sinaloa drug cartel who testified against him.