SANTIAGO, Chile — Uruguay’s Senate approved legislation on Tuesday that will allow the country to legalize the cultivation and sale of marijuana on a nationwide scale. Uruguay’s leftist president, José Mujica, a supporter of the measure, has signaled that he will enact the legislation in coming days.

Under the legislation, approved by a vote of 16 to 13, Uruguay would create a state-run Institute for the Regulation and Control of Cannabis to oversee the planting, harvesting and sale of marijuana. The drug would be sold at pharmacies, with buyers signing up in a state registry, a process enabling them to purchase up to 40 grams a month at $1 a gram.

The action on the bill followed years of debate in Uruguay, which has been grappling with an increase in drug-related violence. Opponents contended the measure would open the way for greater drug use in Uruguay, while supporters claimed it would remove the marijuana trade from the domain of illegal traffickers, allowing the authorities to regulate its consumption.

“We are convinced that we can apply our own policy to drugs in compliance with international norms,” said Roberto Conde, a senator in Uruguay’s governing Broad Front coalition.