TORONTO — Canada’s Senate on Tuesday passed the federal government’s historic bill legalizing the recreational use of marijuana, clearing the way for the country to become the first advanced industrialized nation to legalize the drug nationwide and fulfilling a major campaign promise of Prime Minister Justin Trudeau.

The Senate passed the bill by a vote of 52 to 29, with two abstentions, lifting a prohibition on the recreational use of marijuana that has been in place since 1923. The law will not take effect until it receives royal assent — a final ceremonial stage of the legislative process — and the government sets a date for legalization.

On Wednesday, Trudeau announced that marijuana would be legal on Oct. 17. He had hoped to make cannabis legal by July 1, but Canada’s provinces and territories said they needed eight to 12 weeks to make final preparations before they would be able to sell cannabis to consumers.

Trudeau heralded the vote in a tweet, saying: “It’s been too easy for our kids to get marijuana — and for criminals to reap the profits. Today, we change that.”

Justice Minister Jody Wilson-Raybould, who sponsored the legislation, tweeted that the Senate vote was a “historic milestone for progressive policy in Canada.”

The law makes Canada the second country after Uruguay to have a nationwide, legal market for marijuana.

It grants the federal government the power to license and regulate a restricted group of cannabis growers but gives Canada’s 10 provinces and three territories the discretion to decide how to sell and distribute the drug. Some, like Ontario, will sell it at a small number of state-run stores operated by the provincial alcohol monopoly. Others, like Newfoundland and Labrador, will sell it to consumers at certain grocery stores.

The legislation sets the minimum age for purchase at 18 and allows for the personal possession of up to 30 grams of dried cannabis, with rules on edibles to come later. Anyone found to be selling marijuana to a minor faces penalties of up to 14 years in prison.

Like tobacco companies, producers trying to market their product face strict advertising rules. Cannabis can be sold only in plain packaging that is a single, uniform color and free of flashy graphics or images.

Canadians spent more than $5.7 billion (approximately U.S. $4.2 billion) on marijuana in 2017, most of it for recreational use, according to a Statistics Canada report released in January. That makes the cannabis business larger than the tobacco industry and as large as the beer industry.

Bill Morneau, Canada’s finance minister, said he expects the government to collect nearly $300 million Canadian in taxes on legalized cannabis. The provincial and federal governments will split the excise tax revenue 75 percent to 25 percent for the first two years after legalization.

One of Trudeau’s first major policy pledges as leader of the Liberal Party was to legalize marijuana, a promise that become a central pillar of his party’s 2015 federal election campaign. When his government introduced a bill for that purpose in April 2017, its backers framed it as an effort to discourage consumption among youths and to crush the illegal market.

In a news conference after the legislation was introduced, Bill Blair, a former Toronto police chief who was an architect of the legislation, said, “Criminal prohibition has failed to protect our kids and our communities.”

And while public opinion polls show Canadians have widely supported legalization, introducing the legislation was the easy part for Trudeau’s government. The bill has been the subject of more than a year of fractious debate and legislative back-and-forth between the Senate and the House of Commons.

Disagreement between the chambers flared last week when the House rejected 13 amendments that the Senate added to the bill, including one dealing with whether provinces could ban home cultivation. The Senate eventually deferred to the government’s position of allowing Canadians to grow up to four marijuana plants at home.

Even after Tuesday’s vote, the government is left with a long to-do list. A second piece of legislation that would change laws on impaired driving and give police new powers to carry out roadside intoxication tests has not yet been passed. Questions remain, too, over whether to grant amnesty for past marijuana convictions and over how strictly police forces should crack down on the growing of cannabis at home.

The legalization of marijuana in Canada has implications for cross-border relations with the United States. Under the law, it will remain illegal for Canadians to take cannabis across the border into the United States, just as it will remain a criminal offense for Americans to bring cannabis into Canada.

Leonard Saunders, an American immigration lawyer in Washington state, testified before a Canadian Senate committee that Canadians could be denied entry to the United States even after cannabis is legalized because it is still considered illegal at the federal level in the United States.

“I see a wall on the northern border,” he said, adding that Canadians could face more scrutiny at the border if they admit to having smoked cannabis.

But Ralph Goodale, Canada’s minister of public safety, told the Senate that his message to Americans is that “this should not be an issue.”

“It becomes an issue if you make it one, but there’s no need to make it one, because the border rules have not changed,” he said.