Bayern Munich have agreed on a two-year loan deal for Real Madrid midfielder James Rodriguez, subject to a medical.

In a statement, Bayern said the deal, which runs until June 2019, included the option for the club to permanently buy the Colombia international.

The agreement represents a major coup for Bayern, with reports suggesting that Chelsea and Manchester United were also in the race for Rodriguez's signature.

The 26-year-old was widely expected to leave Real Madrid after starting just 13 La Liga games last season.

"We're delighted we've been able to complete this transfer," Bayern chairman Karl-Heinz Rummenigge said.

"Signing James Rodriguez was our coach Carlo Ancelotti's biggest wish, following their successful spell working together in Madrid.

"James is a very versatile player. He's a goalscorer himself, he sets up a lot of goals and on top of that he's great from set-pieces.

"There's no question that this transfer further increases the quality in our team."

Rodriguez is expected to be included in the Bayern squad that flies out to the Far East for a pre-season tour on 16 July.

The Colombian won the Golden Boot at the 2014 World Cup and is contracted to Real until 2020. He was signed for an initial fee of about €80m (£72m) from Monaco in 2014.

Rodriguez posted a farewell message to Madrid fans on Instagram on 11 July, thanking them for their support.

"Dear Madridistas, I am grateful for all the good moments we have shared in three wonderful years," he wrote.

"I'm happy to have been able to wear this shirt, which I always wore with a lot of pride, and I can only say thank you for all the love, I will take with me the best experiences. Thanks for everything!"

Rodriguez is the fifth new arrival at Bayern this summer after Corentin Tolisso, Niklas Sule, Serge Gnabry and Sebastian Rudy.