Zinedine Zidane, who led the club to three successive Champions League titles, stepped down in May, saying the club needs "a different voice".
Lopetegui previously managed Porto for two years, before taking over at the Spanish national team in July 2016.
The 51-year-old will take up the position after the World Cup, which begins on 14 June.
A former goalkeeper, Lopetegui made one senior appearance for Real Madrid but played 107 matches for Logrones.
He began coaching in 2003 as assistant manager to Spain's Under-17 team.
He managed Spain at Under-19, Under-20 and Under-21 level before moving to Porto.