Coca-Cola is getting a $5 billion shot of caffeine.

The company has agreed to buy coffee chain Costa Coffee from UK drinks and hotels group Whitbread for £3.9 billion ($5.1 billion).

Costa has nearly 4,000 stores across 32 countries. It will give Coca-Cola (KO) "a strong coffee platform across parts of Europe, Asia Pacific, the Middle East and Africa, with the opportunity for additional expansion," Coca-Cola said in a statement.

Besides its thousands of retail stores, Costa Coffee also has smaller on-the-go outlets called Costa Express, a roastery and for-home coffee products.

Whitbread, which acquired Costa in 1995, said it will use proceeds from the sale of the coffee business to expand its other big brand, Premier Inn hotels.

The deal still needs to get approval from shareholders and regulators, and is expected to be completed in the first half of 2019, the companies said.

Buying Costa puts Coke in direct competition with Starbucks (SBUX) and several other global coffee brands.

Earlier this year, Nestle (NSRGF) and Starbucks agreed a $7.2 billion global coffee alliance, which will see Nestle market, sell and distribute Starbucks products outside its stores worldwide. JAB, the Luxembourg-based holding company that owns brands like Krispy Kreme and Peet's Coffee, bought UK sandwich and coffee chain Pret A Manger in May.

Another of JAB's coffee brands, Keurig, merged its business with one of Coca-Cola's domestic competitors — Dr Pepper Snapple — in January.

"Hot beverages is one of the few segments of the total beverage landscape where Coca-Cola does not have a global brand," Coca-Cola CEO James Quincy said Friday. "Costa gives us access to this market with a strong coffee platform."

Competition is heating up in other areas, too. Coca-Cola's chief rival Pepsi (PEP) announced earlier this month that it would buy Israeli DIY seltzer company SodaStream for $3.2 billion.