Ottawa police chief Eric Stubbs said the student, identified as Febrio De-Zoysa, has been charged with six counts of first-degree murder and one count of attempted murder.
Mass killings are rare in Canada.
Stubbs said the deceased are Sri Lankan nationals who recently came to Canada. They included a 35-year-old mother, a 7-year-old son, a 4-year-old daughter, a 2-year-old daughter and the 2 1/2-month-old baby girl as well as a 40-year-old acquaintance of the family.
The police chief said when the first officers arrived at the home the family's father was outside and screaming for someone to call 911. Police received two emergency calls at 10:52 pm Wednesday.
The father is in hospital with serious, but non-life-threatening, injuries.
"This was a senseless act of violence perpetrated on purely innocent people," Stubbs said.
Sri Lanka's high commission said that it is in touch with relatives in the country's capital, Colombo.
De-Zoysa made a brief appearance in court Thursday and mumbled his assent as the justice of the peace ordered him not to speak to the father who survived the attack or to four other witnesses who provided statements to the police.
His case was adjourned until March 13 to give him time to find a lawyer.
"Our first reactions are all ones of shock and horror at this terrible violence," Canadian Prime Minister Justin Trudeau said.
Ottawa Mayor Mark Sutcliffe called the news distressing for all the city's residents.
"It's hard to believe," he said. "It's devastating and heartbreaking."