In the beginning, there was hope. A racial barrier shattered. An anxious nation eager to turn the page on foreign war and economic hardship. And the audacious plans of a new president.

President Obama stood on the West Front of the Capitol on Jan. 20, 2009, and dismissed as small-minded those who would question his grand ambitions.

“They have forgotten what this country has already done,” Mr. Obama said, “what free men and women can achieve when imagination is joined to common purpose, and necessity to courage.”

Days before, Mr. Obama — the African-American son of a Kenya-born father and a Kansas-born mother — had stood in the Oval Office, in a White House built in part by slaves, flanked by four white men who were the only others on the planet who understood the burden of the job he was about to take on.

With the former presidents George Bush, George W. Bush, Bill Clinton and Jimmy Carter. January 2009.

Mr. Obama began with bold strokes. He signed executive orders to close what remained of the secret Central Intelligence Agency prisons. He ordered the closing of Guantánamo Bay.

He pushed through a $787 billion economic stimulus bill, and began the work to provide health coverage to 46 million Americans.

To Republicans hoping to temper his agenda, Mr. Obama had a curt message. “Elections have consequences,” he told Eric Cantor of Virginia, the House Republican leader.

With the new dog, Bo, and his daughters, Malia and Sasha, on the South Lawn. April 2009.

His presidency could be transformational, Mr. Obama believed. He quizzed historians on how past commanders in chief had harnessed Congress to achieve great things.

“He had a mind-set of getting big things done the way Lyndon Johnson was able to in the 1960s, and to be part of the pantheon of great progressive presidents,” said Douglas Brinkley, a presidential historian who has spoken regularly with Mr. Obama. “He truly believed that he could unify the country.”

He found instead that it was as divided as ever.

“It feels bad,” he said in 2010, licking wounds from a midterm electoral loss he called a “shellacking.”

After Republicans won a majority of seats in the House. November 2010.

The agent of change had become a symbol of the status quo he had vowed to upend. Mr. Obama had his health care bill, but the triumph had come at a heavy cost.

“This is something that I think every president needs to go through,” he told reporters. “In the rush of activity, sometimes we lose track of, you know, the ways that we connected with voters that got us here in the first place.”

Tea Party-fueled Republicans, energized by their virulent opposition to what they derisively branded “Obamacare,” vowed not to let Mr. Obama’s agenda advance.

And so his aspirations shifted with the political tide.

“I guess in my second term, I’ll be doing a lot of foreign policy,” he ruefully told a group of historians at a 2011 dinner at the White House.

At the Group of 20 summit meeting with President Vladimir V. Putin of Russia. June 2012.

Receiving the remains of Americans killed in Benghazi, Libya. September 2012.

David Kennedy of Stanford University was there. “He just felt checked and blocked and that he couldn’t get any further,” he said. “He went from a place of being just full of enthusiasm and hope and expectation and hope and aspiration to a real sense of disappointment, of being stymied.”

It never got easier. Time and again, Mr. Obama had to comfort a nation stunned by repeated shooting rampages across the country. Spending time with families who lost young children in the gun massacre at Sandy Hook Elementary School in Newtown, Conn., in December 2012, was the “hardest day of my presidency,” he said afterward. “And I’ve had some hard days.”

Mr. Obama’s voice crackled with anger in 2013 after the Senate defeated measures to expand gun restrictions. Three years later, while announcing a set of modest executive actions to curb gun violence, he shed tears in the East Room as he remembered the Newtown children.

Announcing executive actions on gun control. January 2016.

“Every time I think about those kids,” he said as he wept openly, “it gets me mad.”

No matter how hard he tried to avoid defining his presidency through the prism of race, it was always a powerful subtext, and sometimes a potent flash point.

His victory was heralded as the dawn of a post-racial America, but the first African-American president never really saw it that way.

“At the inauguration, I think that there was justifiable pride on the part of the country that we had taken a step to move us beyond some of the searing legacies of racial discrimination in this country,” Mr. Obama said several weeks after being sworn in. “But that lasted about a day.”

Observing the 50th anniversary of the Voting Rights Act, in Selma, Ala. March 2015.

There is more work to be done, he always said.

“We just need to open our eyes and our ears and our hearts to know that this nation’s racial history still casts its long shadow upon us,” he said when he walked across the Edmund Pettus Bridge in Selma, Ala., to commemorate the 50th anniversary of the Voting Rights Act — and of “Bloody Sunday,” when black civil rights activists were brutally beaten by state troopers.

The deaths of Trayvon Martin, Eric Garner, Michael Brown and nine black parishioners in South Carolina forced Mr. Obama to speak out, and yet try to balance both sides.

“Maybe we now realize the way racial bias can infect us even when we don’t realize it,” Mr. Obama said at the funeral for Rev. Clementa C. Pinckney, a victim of the mass shooting at Emanuel African Methodist Episcopal Church in Charleston, S.C. “So that we’re guarding against not just racial slurs, but we’re also guarding against the subtle impulse to call Johnny back for a job interview, but not Jamal.”

With Representative John Lewis, Democrat of Georgia. March 2015. At a funeral for the pastor killed in the Charleston church massacre. June 2015.

Standing in Nelson Mandela’s jail cell on Robben Island in South Africa. June 2013.

But when he opened the White House to Black Lives Matter activists, who told him their voices were not being heard, he admonished them: “You are sitting in the Oval Office, talking to the president of the United States.”

With King Salman of Saudi Arabia. January 2015.

His foreign policy was one of contradictions. Nine days after announcing that he would send 30,000 more American troops to Afghanistan, Mr. Obama accepted the Nobel Peace Prize in Oslo. Even he did not believe that he had earned it yet.

Six years later, Mr. Obama had to concede that the war in Afghanistan would not end before he left office.

With Prime Minister Justin Trudeau of Canada and President Enrique Peña Nieto of Mexico in Ottawa. June 2016.

With President Raúl Castro of Cuba in Havana. March 2016. With Pope Francis at the White House. September 2015.

But he basked in international adulation for other breakthroughs: a broad climate change agreement, the most significant in history, forged in Paris; a nuclear deal with Iran; a surprise opening with Cuba.

“I didn’t make any big mistakes,” the Nobel Prize winner told historians in 2014 of his foreign policy legacy. That was before the Islamic State began a brutal offensive in Iraq, a ruthless new adversary on the rise.

After a Supreme Court decision upholding parts of his signature health care law. June 2015.

In the end, the victories were hard fought.

“Yes, we can,” was his inspirational campaign mantra in 2008, but in 2016, traveling the country to promote his rival-turned-ally Hillary Clinton, he often resorted to, “C’mon, man!” He watched incredulously as voters contemplated replacing him with a man who owed his political rise to a false conspiracy theory that Mr. Obama was born in Kenya.

He smiled tightly for the cameras after the election, shaking hands with the president-elect in the Oval Office only days after telling Americans that Donald J. Trump could never be trusted with the nuclear codes.

Campaigning for Hillary Clinton in North Las Vegas. October 2016.

With President-elect Donald J. Trump in the Oval Office. November 2016.

“You don’t need hope when things are going well,” he said to heartbroken young aides. “You need it when things are not going well.”

Ever rational and restrained, he insisted on an orderly passing of the baton. It was jarring to aides who wanted the fiery Barack Obama of the 2008 campaign.

But he, and the country, had changed. Hope had been tempered by volatile politics, the art of the possible and the slowness of societies to evolve.

“America is not a fragile thing,” Mr. Obama said in a farewell address in Chicago. “But the gains of our long journey to freedom are not assured.”