CHARLOTTE — Two people were killed and four others were injured after a shooter entered a campus building Tuesday at the University of North Carolina at Charlotte and opened fire.

It was the last day of classes for the school year, one that many students had expected to finish off with a concert but that ended with gunfire and barricades.

“Unbelievable grief,” Jeffrey A. Baker, chief of the University of North Carolina at Charlotte Police & Public Safety agency, said at a news conference Tuesday night, “that this — came to our campus.”

Police identified the suspect Tuesday night as Trystan Andrew Terrell, 22, and said he was in custody. The Charlotte-Mecklenburg Police Department said Wednesday he was charged with two counts of murder, four counts of attempted murder, four counts of assault with a deadly weapon with intent to kill, possession of a firearm on educational property and discharging a firearm on education property.

Terrell could not immediately be reached for comment Tuesday evening.

The university’s chancellor, Philip L. Dubois, called it “the saddest day in UNC Charlotte’s history.”

It was a situation campus police had trained for, just as officers do at colleges across the country at a time when such shootings feel all too frequent. Baker said his officers responded swiftly and were able to stop the suspect. “Our officers’ actions definitely saved lives,” he said.

Baker said officers were able to get quickly to the building where the shooting happened because they already were converging for a Waka Flocka Flame concert on campus.

Two people were found dead at the scene, according to a Mecklenburg Emergency Medical Services Agency post on Twitter. Three people were in critical condition, Baker said, while a fourth person did not have life-threatening injuries.

One suspect was in custody, and there was no reason to believe other people were involved in the shooting, the Charlotte-Mecklenburg Police Department announced Tuesday evening.

Shots were fired at 5:40 p.m., university officials said.

University officials warned the campus Tuesday evening of shots being fired and urged people to “Run, Hide, Fight.” Campus buildings were locked down.

Amando Chavez, 18, said he was in a film class with about 30 other students when another student entered the classroom and told the professor a shooter was on campus.

The class was in Cameron Hall, a few buildings from where the shooting unfolded, so the students decided to make their way to the student union. They walked single file, down a hill together, as police cars sped in the opposite direction, said Chavez, a Charlotte native.

At least a few hundred students crowded into the student union, Chavez said. Some students cried, he said, but for the most part, it felt “like a normal high school lockdown.” He texted his parents every five minutes, letting them know he was safe.

Police released him and other students about 8 p.m., he said.

“I’m kind of shocked,” he said. “It’s been so peaceful and quiet, you wouldn’t expect this here.”

As of 9:30 p.m., Austin Ferguson, 21, said his residence hall remained on lockdown. He and eight other students barricaded themselves inside a snack room for an hour, using several chairs, two tables and a recycling bin to block the door before a couple of resident advisers told them it was safe to come out.

“When it actually happens to you, it’s something you can’t even fathom,” Ferguson said. “You’ve become a statistic and so has your school.”

Ferguson said he had left a political science class two buildings away from the Kennedy building, just before the shooting happened, and headed to the student union. He said he saw people “hysterical,” crying and out of breath outside the building.

University officials said shots had been reported near the Kennedy building, one of the first structures to be built on campus and now home to administrative offices.

As the campus remained on lockdown Tuesday evening, North Carolina Gov. Roy Cooper (D) expressed his concern and thanked law enforcement officers. “We know for many people here, this will be the worst day of their lives,” Cooper said.

He said Tuesday was a day to reunite families and comfort people, but that in the future, authorities would take a hard look at what happened and how such tragedies could be prevented.

He said students and parents should not have to deal with this kind of fear. “This violence has to stop,” the governor said.

Late Tuesday, Dubois said that he had been traveling for an NCAA meeting when he saw the alert about the shooting on the school’s website. The school’s lockdown protocols worked as designed, he said. It’s something for which drills are conducted, but it’s a shock when it happens, he said.

Charlotte Mayor Vi Lyles (D) expressed condolences on social media Tuesday evening.

A family reunification center was set up, at the parking lot of a grocery store, for students and others to reconnect with relatives.

Parents, family and friends gathered at the Harris Teeter lot. Baker told them, “As a university police chief, you say, ‘God, I hope this never happens here.’ I’m very, very sorry and heartbroken that anything like this happened on our campus.”

Corey Lindenberg, a sophomore from Greensboro, N.C., was waiting for a friend who was in the building where the shooting took place. He had texted with his friend but said he wanted to be there when he was allowed off campus.

Lindenberg said he participated in an active-shooter drill last year in which an officer came in and offered guidance about what to do in the event of an attack — including barricading the door and fighting back by throwing objects at the shooter.

“You hear about it all the time on the news and stuff, but the last thing you think is it’s going to happen at your own school,” Lindenberg said. “I always feel safe on campus. I just never would have thought this would have happened. When it does, it’s a shock.”

After 9 p.m., university officials were still advising people in campus buildings to remain sheltered in place. Officials said they believed no ongoing threat existed but cautioned people that the search was methodical: “The campus remains on lockdown while police clear each building. This could take several more hours.”

Leaders of the UNC system extended their support and condolences.

“We are devastated to learn of the act of violence that occurred earlier on the campus of UNC Charlotte, and our thoughts and prayers go out to the victims and their families," UNC Board of Governors Chairman Harry Smith and UNC System interim president Bill Roper said in a joint statement Tuesday night. “The University of North Carolina System stands in solidarity with UNC Charlotte and stands ready with assistance for the students, faculty, and staff affected.”

Shortly before 11 p.m., university officials announced that people could leave campus.

Some were planning to gather in solidarity Wednesday.

After 10 p.m., university officials announced that exams were canceled through Sunday, even as they urged people to continue to shelter in place, and raise their hands and answer questions if approached by police. And they added a final plea: “Students, if you have not yet contacted your family to let them know you are safe, please do so.”

Svrluga and Truong reported from Washington. Mark Berman and Julie Tate contributed to this report.

This is a developing story. It will be updated.