Uruguay's lower house of Congress has voted to create a government body to control the cultivation and sale of marijuana and allow residents to grow it at home or as part of smoking clubs.
The use of marijuana is already legal in the South American nation, but sale and cultivation is not.
After hours of spirited debate, 50 lawmakers voted for the bill and 46 against it.
The Senate is expected to vote on the measure backed by President Jose Mujica later this year.
Mr Mujica, a former leftist guerrilla fighter, says the bill would control the marijuana trade under strict guidelines, help undermine drug-smuggling gangs and fight petty crime.
To avoid making the country a drug tourism destination, only Uruguayans would be allowed to use marijuana.
Critics say the measure risks luring more Uruguayans to harder drugs and could rile fellow Latin American countries battling drug-related violence such as Colombia and Mexico.
The legislation would establish a National Cannabis Institute to control the drug's production and distribution, impose sanctions on rule-breakers and design educational policies to warn about the risks of marijuana use.
Households would be permitted to grow up to six plants, or as much as 480g of marijuana per year under the measure.
It also would set regulations for smoking clubs with up to 15 members, 90 plants and annual production of up to 7.2kg.