OTTAWA — The Canadian government on Tuesday said it would buy an oil pipeline to the country’s west coast, ensuring that its widely protested expansion will go forward.

The Trans Mountain pipeline, which carries oil from Alberta to a port in a suburb of Vancouver, British Columbia, has become a flashpoint in a wider debate in Canada over the environmental impact of tapping Alberta’s oil sands.

Critics view the sands as a particularly dirty energy source. Similar objections drove efforts to block the Keystone XL pipeline from Alberta to the United States.

Environmentalists and some indigenous groups have strongly opposed the expansion of the pipeline, citing environmental concerns. The province of British Columbia is trying to block it in the courts.