The Senate approved legislation Thursday mandating construction of the controversial Keystone XL pipeline, setting the stage for a veto showdown with President Obama.

In a 62-to-36 vote, 53 Republicans and nine Democrats approved a bill seeking to force completion of the 840-mile pipeline, a measure Obama has vowed to veto while federal environmental reviews continue.

After years of legislative jostling, the Keystone legislation had taken on a political life of its own, gaining far more significance on the right and left than the actual output from the proposed pipeline through Montana, South Dakota and Nebraska.

Several years ago, when a gallon of gas cost nearly $4 and the unemployment rate was near 10 percent, Republicans began clamoring for Obama to approve the pipeline so the additional 830,000 daily barrels of oil flowing through the 36-inch-diameter pipeline would help lower gasoline prices and create thousands of jobs in the heartland.

Environmental activists saw the proposal as a harmful precedent that would lead to greater oil exploration and diminished efforts toward alternative energy. Their Democratic allies in Congress have urged Obama to reject the pipeline, arguing that it would carry dirty oil from western Canada through the Mountain West and Prairie states, risking environmental damage while creating few long-term jobs.

By last fall, the Keystone project became a national rallying cry in Senate races across the nation as Republicans from as far away as Alaska and New Hampshire made the project a cornerstone of their campaigns against Democratic incumbents. Once the Republicans picked up nine additional seats and the majority in the November midterm elections, the legislation’s fate became clear: It could win congressional approval but not overcome a presidential veto.

Despite the support of nine Democrats, Republicans remain several votes shy of the two-thirds majority they would need to override Obama’s expected veto. It would mark the first major piece of legislation that Obama has vetoed since taking office more than six years ago, having had a Democratic majority in the Senate that prevented anything objectionable from reaching his desk. On two occasions, Obama vetoed less significant legislation, with one related to a Keystone provision attached to a different bill.

Regardless of the likely stalemate, Senate Majority Leader Mitch McConnell (R-Ky.) declared the nearly month-long debate a victory that would lead to the pipeline getting built. He also touted his new approach of allowing freewheeling debate in the Senate.

“The debate over these American jobs has shown that with bipartisan cooperation, it’s possible to get Washington functioning again,” he said Thursday as the Senate prepared to finish voting on more than 40 amendments to the bill.

The bill differs slightly from the version approved by the House in early January, with a pair of ­energy-efficiency programs added. Senate Republicans are hopeful that House Speaker John A. Boehner (R-Ohio) will simply move to pass their legislation and send it to the president’s desk. It is possible that instead the leaders will convene a conference committee to iron out the differences and have the final legislation reapproved by both chambers.

Democrats called the debate a political show for the oil industry. “It will be vetoed, and the veto will be sustained,” Sen. Brian Schatz (D-Hawaii) said after the vote.

Obama has embraced the views of liberal opponents to Keystone, but officially the administration is awaiting an environmental review by the State Department.

“This Keystone project is undergoing review at the State Department,” Eric Schultz, a White House spokesman, told reporters Wednesday. “That is a process that long predates this administration. So we are opposed to any legislative maneuver that would circumvent that process.”

Thursday’s flurry of votes capped several weeks of debate that produced dozens of proposals from both sides. Most related to energy production or climate change, but some dealt with campaign finance laws. Another sought to delist prairie chickens from the protections of the Endangered Species Act.

One amendment, offered by Schatz, was a simple nonbinding resolution declaring that “human activity significantly contributes to climate change.”

It was rejected, but five Republicans voted with Democrats on the matter, including Sen. Lindsey O. Graham (S.C.), who on Thursday declared his intention to form a presidential exploratory committee for 2016.

“The Republican Party would be better served by saying there is ample science that man-made emissions, CO2 in nature, are causing the planet to heat up, and we should go to a lower-carbon economy in a smart way,” Graham said Thursday. “I think Republicans would be better served by attacking the solution they [Democrats] offer rather than denying that there is a problem.”

Such a position might not fare well with Republican primary voters. Other yes votes included a pair of Republicans, Sens. Kelly Ayotte (N.H.) and Mark Kirk (Ill.), who face difficult reelection campaigns next year and will probably tout those votes as examples of their legislative independence.

“What we’ve seen over the last several weeks is the Senate I remember, the Senate,” said Sen. Richard J. Durbin (Ill.), the No. 2 Democratic leader who has been filling in for Reid, praising the “active debate and amendments.”

Elected in 1996, Durbin is part of the minority of senators who have served during more than one presidential administration. “For some members,” he said, “it is a new experience.”