A federal judge on Friday ruled Utah’s same-sex marriage ban unconstitutional in a state dominated by the Mormon Church, one of the country’s staunchest opponents of gay marriage.

“The State’s current laws deny its gay and lesbian citizens their fundamental right to marry and, in so doing, demean the dignity of these same-sex couples for no rational reason,” Judge Robert J. Shelby, of the U.S. District Court for the District of Utah, wrote in his opinion. “Accordingly, the court finds that these laws are unconstitutional.”

Utah’s voters approved the ban on gay marriage in 2004.

The decision, if allowed to stand, would make Utah the 18th state where same-sex marriages are legal and the second to allow them this week. Another southwestern state, New Mexico, became the 17th on Thursday when its Supreme Court ruled a ban there was unconstitutional as well.

In his ruling, Shelby wrote that Utah failed to prove that heterosexual marriages would be affected by same-sex ones. “In the absence of such evidence,” he wrote, “the State’s unsupported fears and speculations are insufficient to justify the State’s refusal to dignify the family relationships of its gay and lesbian citizens.”

The Salt Lake County Clerk began issuing marriage licenses almost immediately after the ruling.

“It couldn’t be more exciting,” said Jim Dabakis, the head of Utah’s Democratic Party, who married his partner of 27 years, Stephen Justesen, on Friday afternoon, with Salt Lake City Mayor Ralph Becker officiating. Dozens of couples lined up to get married at the county clerk’s office after the ruling, Dabakis said. “There’s nothing but pure euphoria.”

But there was a sense of urgency, he added. “You never know when they might pull the rug out from under you.”

That urgency was well founded. A spokesman for the Utah attorney general’s office, Ryan Bruckman, said that the state planned to appeal the ruling and ask for a stay to stop the issuance of licenses, with lawyers rushing to file the relevant documents Friday.

“We’re trying to get it filed as soon as possible,” he said. But logistical issues could hold up the filing for a couple of days, he added. In a statement, the office said the ruling that same-sex marriage is a fundamental right “has never been established in any previous case in the 10th Circuit.” If the stay is granted, issuance of same-sex marriage licenses would stop, at least temporarily, and the fight would be brought to the U.S. Court of Appeals for the 10th Circuit.

Proponents of same-sex marriages said they were prepared for a stay, but remained optimistic. “We believe this momentous decision will ultimately stand as the law in Utah,” Peggy A. Tomsic of Magleby & Greenwood, the firm representing the plaintiffs in the case, said in a statement issued after the judge’s ruling that also noted the expected appeal.

This has been a banner year for the movement, with 10 states progressing toward same-sex marriage in some way, according to data maintained by the Human Rights Campaign, an advocate for the lesbian, gay, bisexual and transgender community. Bans have been overturned or marriages have begun — or have been approved to begin — in California, Delaware, Hawaii, Illinois, Maryland, Minnesota, New Jersey, New Mexico, Rhode Island and, now, Utah.

Nationally, public opinion on the issue has flipped in the past decade. In 2003, 55 percent opposed gay marriage while 37 percent supported it, according to a Washington Post-ABC News poll. Today, the sides have virtually switched, with 58 percent supporting it and 36 percent opposing, according to a Washington Post-ABC News poll in March.

But public opinion on the issue in Utah has been historically slower to turn. Only 28 percent supported legalizing gay marriage in a February 2012 poll conducted by the Center for the Study of Elections and Democracy, while a similarly timed CBS/New York Times poll found 38 percent support nationally. The Utah poll found that 43 percent preferred civil unions, while 24 percent said the same nationally.

Utah’s large Mormon population is particularly opposed to homosexuality, as well. Nationally, nearly two in three Mormons in 2011 said society should discourage homosexuality, while only about one in three people overall said so, according to a Pew poll. Nearly three-fifths of Utah’s residents are Mormon, according to a 2007 Pew Religion & Public Life survey.

The Church of Jesus Christ of Latter-day Saints campaigned for the passage of Proposition 8, a 2008 California ballot initiative limiting the definition of marriage as between a man and a woman. It was even fined for failing to report some of its non-monetary contributions in the final weeks of the campaign. While the initiative passed, it was ultimately overturned.

Dabakis said the ruling is more about equal civil rights and should not be seen as a threat to the sanctity of Mormon teachings.

“I’m just comfortable that we need to continue to explain to people that this is for civil marriage,” he said. “This has nothing to do with religious marriage. This is no threat to religion. Religion is a different thing.”

Vince Bzdek, Peyton M. Craighill and Scott Clement contributed to this report.