The use of trains to carry crude is surging as drillers in parts of North America produce more oil than area pipelines can accommodate.

Much of the recent oil train growth in the US is due to record shipments from Canada, where pipeline expansion projects, including Keystone XL and Trans Mountain, have stalled amid environmental opposition and legal delays. Crude-by-rail shipments also have ticked up from North Dakota’s Bakken region and the Permian Basin of West Texas and New Mexico, according to energy-monitoring firm Genscape.

The crude-by-rail comeback is expected to last through late this year in the Permian, and longer in North Dakota and Canada, as companies struggle to lay new pipe as quickly as drillers are getting oil out of the ground.

Shipping oil by train is more expensive than sending it through a pipeline, so producers often avoid making long-term commitments to rail companies. It costs about $20 a barrel to send oil by rail from Canada to the US Gulf Coast, compared with about $12.50 by pipeline, according to energy investment bank Tudor Pickering Holt & Co.

But pipeline projects typically lag behind growth in oil and gas production, and the gap has lengthened in many parts of the country in recent years as local activism has made it increasingly difficult to complete projects.

Bottlenecks have grown particularly severe in Canada, where heavy crude was selling locally for more than $50 a barrel below US benchmark prices last fall, reflecting producers’ inability to get it to market due to pipeline problems. US oil prices have since fallen more than 20 percent.

The congestion in Canada spurred companies including Houston-based ConocoPhillips and Calgary-based Cenovus Energy to ink rail deals.

“The intention is to bridge us over to the next major pipeline expansion, so a few years,” ConocoPhillips finance chief Don Wallette, Jr. said last fall.

Cenovus’s three-year agreements will allow it to transport about 100,000 barrels of oil daily to the US Gulf Coast, where refiners mix it with lighter crudes to produce fuel.

In October, about half of the oil the US imported by rail from Canada went to the Gulf Coast, according to The Wall Street Journal, helping to offset a 30 percent decline in crude purchases from Venezuela over the past two years. Roughly a quarter went to the Midwest, while smaller amounts went to the East and West coasts.

Derailments, notably one in Lac-Mégantic, Quebec, that killed 47 people in 2013, have raised concerns about the safety of transporting oil by trains on a large scale. That prompted federal regulators to impose tougher safety requirements for railcars, though opposition remains in some communities.

The heightened demand for oil train transportation has benefited railroads, including Union Pacific, whose petroleum shipments rose 30 percent last year as the company handled more crude oil. But Chief Executive Lance Fritz said the railroad isn’t investing heavily to support crude-by-rail shipping because the demand could evaporate once major pipeline projects come online.

“We’re careful to make these commitments because it’s a short-lived phenomenon,” Fritz told The Wall Street Journal. “It’s just not going to be around for long-term returns.”

Since shipping oil by rail is generally more expensive, pipelines remain a more attractive option when available, analysts say.

Oil companies often use trains on an ad hoc basis, and rail provides geographic and financial alternatives for producers wary of committing to new pipes. Pipeline companies typically won’t proceed with a project unless drillers sign multiyear contracts guaranteeing payment regardless of whether they have oil to ship.