A federal immigration judge ruled Tuesday that President Obama’s uncle can remain in the United States, sparing him deportation to his native Kenya in a case that riveted attention on the elderly man who had lived under the radar in this country for 50 years. For most of that time, he was here illegally.

Boston immigration Judge Leonard I. Shapiro said Onyango “Omar” Obama can now get a green card, and in five years, apply for US citizenship, unless the Department of Homeland Security appeals the case within 30 days.

“Congratulations,” Shapiro said after he announced his decision. “Welcome to America.”

The case stirred controversy because the president’s uncle was arrested in 2011 for drunken driving in Framingham and was found to have an outstanding deportation order from 1992. Many accused the government of giving him special treatment at the same time President Obama is deporting record numbers of illegal immigrants.

But Shapiro said Onyango Obama was eligible for a green card under federal immigration law because he arrived in America before 1972 and had displayed “good moral character.” The judge said the 69-year-old paid taxes, volunteered in the community, and was a “kind and decent” person.

During his testimony, Obama begged the judge for the chance to stay, saying he did not know Kenya anymore. He left at age 19 to come to the United States.

“America is a land of opportunities, a land of chances,” Obama told the judge.

Obama also invoked his nephew’s name during the hearing, which lasted more than an hour. Asked if he had relatives in the United States, he said he had two nieces; a sister, Zeituni Onyango, who appeared in court Tuesday; and his brother’s son, Barack Obama.

“I do have a nephew,” Obama said slowly. “He’s the president of the United States.”

The White House said last year that Obama had never met his famous nephew, but the uncle testified Tuesday that the president had stayed with him at his Cambridge apartment for three weeks when he came to attend Harvard Law School in the 1980s.

“It’s a good thing to let your nephew stay with you,” he said after the hearing, adding that in his family, “your brother’s kids are your kids as well.”

The White House did not respond to numerous requests for comment.

The hearing offered new details of Onyango Obama’s life in Cambridge and then, Framingham, where he manages Conti Liquors for $22,000 a year.

For one, Obama said his name is Obama Okech Onyango, not Onyango Obama, as Framingham police reported.

Under questioning from his lawyer, Margaret Wong, Obama testified that he came to the United States in 1963 to attend a prestigious boys’ school in Cambridge. He said the first year of his tuition was paid by a friend of his brother, Barack Obama Sr., the president’s father. But Obama could not afford the following years, so he left.

He said he graduated from high school in Cambridge and earned a bachelor’s degree in philosophy from Boston University, contradicting reports he never finished high school.

But it is unclear how he paid for college. Boston University would not confirm that he attended the school.

Obama said he renewed his student visa, but in the 1970s the visa expired and he was here illegally. He was ordered deported several times. He did not explain why he never left.

He expressed remorse for his 2011 drunken driving arrest, calling it a mistake
to drink three beers and then go out to buy milk. The case was dismissed after he admitted to sufficient facts in Framingham court, served a year’s probation, and incurred other penalties. After his arrest, he allegedly told an officer, “I think I will call the White House.”

Obama said that his record was otherwise clean and that he typically worked six days a week at the store. He said he rarely drinks and volunteers in his community, such as raising money to aid immigrants from Kenya.

He testified that he had consulted a psychologist and that he sometimes suffers from depression, anxiety, and lack of sleep. He said he also has high blood pressure and was worried that he would be unable to get his medication in Kenya.

While spry and elegant in a taupe suit, he said he was unsure how he would survive in Kenya.

“I don’t have any more friends there,” he said. “At my age, it becomes a little bit difficult.”

Homeland Security prosecutor Jerry DeMaio pointed out that Obama knew he was supposed to leave the country when his student visa expired, but did not.

“So, you’ve been out of status for about 43 years,” DeMaio said.

“I would say so, yes,” Obama replied.

DeMaio also said an immigration agent in 1984 said in a report that Obama said he had a green card, when he did not. Obama did not recall saying that.

Obama said he also could not remember if he told the Framingham police in 2011 that he wanted to call the White House.

Alfred Ouma, a naturalized US citizen who owns the Framingham home where Obama lives, testified that returning to Kenya would devastate Obama. He said that their homeland had changed dramatically and that many Kenyans would find it odd that Obama has never married or had children.

“People will ask him all kinds of questions,” he said.

Ouma, a 57-year-old electrical engineer, said Obama is a mentor to him and scores of other Kenyan immigrants. Obama also cooks dinners for family and friends.

“He’s a gentleman,” Ouma testified.

Zeituni Onyango, Obama’s sister, was also prepared to testify but Shapiro said he did not need her testimony. Shapiro
granted Zeituni Onyango asylum in 2010, after a federal official had illegally exposed her status to the media days before the president’s 2008 election.

Since then, Onyango has granted television interviews and written a book, but her brother has been media-shy. Before the hearing Tuesday, he said only that he hoped for “victory, of course.”

BostonGlobe.com complimentary digital access has been provided to you, without a subscription, for free starting today and ending in 14 days. After the free trial period, your free BostonGlobe.com digital access will stop immediately unless you sign up for BostonGlobe.com digital subscription. Current print and digital subscribers are not eligible for the free trial.

Thanks & Welcome to Globe.com

You now have unlimited access for the next two weeks.

BostonGlobe.com complimentary digital access has been provided to you, without a subscription, for free starting today and ending in 14 days. After the free trial period, your free BostonGlobe.com digital access will stop immediately unless you sign up for BostonGlobe.com digital subscription. Current print and digital subscribers are not eligible for the free trial.