SAN FRANCISCO — Uber plans to buy Careem, its largest rival in the Middle East, the American ride-hailing company announced on Tuesday, giving it a strong foothold in the region ahead of its expected public offering.

The deal, valued at $3.1 billion, will need regulatory approval and may not be completed until next year.

Uber’s public offering, expected next month, could reach a valuation of up to $120 billion, rivaling the 2012 listing of Alibaba, the Chinese e-commerce giant. But Uber has remained unprofitable, burning cash on subsidies for its riders and drivers in an effort to undercut its competition. In the final quarter of 2018, the company lost $865 million on revenue of $3 billion.

By taking over its main competition in the Middle East, Uber could cut its losses there. The deal with Careem will also give it access to some countries, like Iraq, Palestine and Morocco, where it does not operate.