All of the dead were students of the College of Agriculture in the town of Gujba in Yobe state, the area governor said in a statement.
The early-morning attack was carried out by “Boko Haram terrorists who went into the school and opened fire on students” while they were sleeping, the military spokesman in Yobe, Lazarus Eli, told AFP.
Salamanu Ibrahim, a 23-year-old student at the college, said dozens of gunmen took part in the killing, bursting into dorm rooms and firing indiscriminately in the dark.
“The attackers went berserk,” he told AFP in Yobe’s capital Damaturu, some 30 kilometers (18 miles) away, where he fled along with hundreds of other students.
“They were fully armed with sophisticated rifles, and improvised explosives,” and razed several college buildings after leaving the dorms, he said.
The office of Yobe Governor Ibrahim Gaidam put the death toll at 40, with four others injured.
In a previously scheduled television interview, President Goodluck Jonathan said he instructed Nigeria’s security chiefs “to look at different ways of handling” the insurgency hours after learning of the latest bloodshed.
He also voiced exasperation at the targeting of students in the interview broadcast on several networks, saying: “Why did they kill them? … You can ask and ask.”
The European Union’s foreign policy chief Catherine Ashton called the slaughter “horrific.”
Yobe has seen a spate of attacks on schools and universities, all blamed on Boko Haram, an extremist group that has killed hundreds in its four-year insurgency.
The name Boko Haram, loosely translated, means “Western education is forbidden,” and the group has become notorious for slaughtering students.
In July in the town of Mamudo in Yobe, Islamists threw explosives and sprayed gunfire into dormitories in the middle of the night, killing 41 students.
Suspected Boko Haram gunmen shot dead seven secondary school students and two teachers in Damaturu in June.
Military offensive criticized
The military has described the recent school attacks as a sign of desperation by the Islamists, claiming that they lack the capacity to strike anything but soft targets.
Defense officials have said that an offensive launched against Boko Haram in mid-May has decimated the group and scattered its fighters across remote parts of the northeast, the insurgents’ traditional stronghold.
While many of the recent attacks have occurred in more remote areas, often targeting defenseless civilians, the unchecked killing has cast doubt on the success of the military campaign.
“Although there is increase in troop movement and… more military hardware in the northeast, people were yet to see the kind of action on the ground that effectively nips criminal and terrorist activities in the bud,” the Yobe governor’s statement said.
Scores have been killed this month, including in the northeastern town of Benisheik, where at least 142 people were slaughtered by Boko Haram fighters who came disguised as soldiers, set up checkpoints and fired on motorists and bystanders.
Some of the recent violence has targeted vigilante groups that have formed to help the military.
The northeast remains under a state of emergency imposed on May 14.
The phone network has been switched off in much of the northeast since the emergency measures were declared, a move the military said would help prevent the Islamists from coordinating attacks.
Boko Haram has said it is fighting to create an Islamic state in Nigeria’s mainly Muslim north, but the group is believed to be made up of different factions with varying aims.
The group has attacked churches, mosques, newspaper offices, the security forces, politicians and a UN building, among other targets.
According to an estimate made earlier this year, the conflict has cost more than 3,600 lives, including killings by the security forces. The current figure is likely much higher.
Nigeria is Africa’s most populous country and top oil producer, roughly divided between the mainly Muslim north and a predominately Christian south.