ST. PAUL, Minn. — Spurring onlookers’ cheers, the Minnesota Senate voted Monday to make gay marriage legal, putting the state on the brink of becoming the 12th to allow same-sex couples to marry. Governor Mark Dayton immediately announced he would sign the legislation Tuesday.

The Senate vote of 37-30 came four days after the House passed the bill on a 75-to-59 vote. When the tally was announced after more than four hours of debate, a huge cheer erupted in the chamber and gallery, where spectators stood and applauded.

Minnesota will become the first state in the Midwest to make gay marriage legal in a legislative vote. Iowa legalized same-sex marriage in 2009 through a court ruling. Under the legislation, gay couples will be able to get married starting Aug. 1.

Last week, Dayton, a Democrat, called the bill ‘‘one of those society-changing breakthrough moments.’’ His staff scheduled a signing ceremony for 5 p.m. Tuesday on the Capitol steps.

It is a rapid turnaround for gay marriage supporters, who just six months ago had to organize a massive effort to defeat a constitutional amendment that would have banned gay marriage. The groups who defeated the amendment quickly turned their attention to legalizing gay marriage, and their efforts were aided by Democrats capturing full control of state government in November.

Only one Republican, Branden Petersen of suburban Andover, voted ‘yes’ Monday. Three Democrats, all from rural Minnesota, voted against the bill.

Massachusetts became the first state to allow same-sex marriage in 2004 after a state Supreme Judicial Court ruling.

In the last week and a half, Rhode Island and Delaware became the 10th and 11th states to legalize gay marriage. In Illinois, a gay marriage bill has cleared the state Senate but awaits a House vote.

The Minnesota House vote last Thursday drew more than a thousand demonstrators representing both sides of the issue. But on Monday, the Capitol and grounds were dominated by gay marriage backers.

Supporters say they just want same-sex couples to have the same legal protections and societal validation that straight couples get with marriage.

Opponents say gay marriage undermines an important societal building block that benefits children and also exposes people opposed on moral grounds to charges of bigotry.

Supporters of the bill taped blue and orange hearts on the Capitol steps Monday, creating a path into the building for lawmakers with the signature colors of their movement. In the rotunda, they sang songs including ‘‘Over the Rainbow,’’ ‘'Going to the Chapel,’’ and ‘‘The Star-Spangled Banner.’’

St. Paul Mayor Chris Coleman ordered the Wabasha Street Bridge near downtown festooned in rainbow-striped gay pride flags to mark the occasion and temporarily renamed it the ‘‘Freedom to Marry Bridge.’’ He also proclaimed it ‘‘Freedom to Marry Week’’ in the capital city.

At the start of the Senate debate, Republican opponents raised concerns that the bill does not go far enough to protect people and organizations with faith-based objections from refusing business related to gay weddings.

‘‘Are we going to have the treads of history roll over people of religious faith?’’ asked Senator Warren Limmer, a Maple Grove Republican.

But a majority of senators rejected a Republican amendment to increase the bill’s religious protections, as gay marriage supporters argued it would have gutted civil rights protections for gay people.

Micah Thaun Tran, of Golden Valley, has been with his partner for 13 years and said they are planning a fall wedding in Grand Marais along Lake Superior.
He was also present for the House vote Thursday and said he could not stay away as the final vote was taken.

‘‘Today I just want to be a spectator of history,’’ Tran said. ‘‘It is just so validating.’’