The history of rail transport in Spain begins in the 19th century. In 1848, a railway line between Barcelona and Mataró was inaugurated, although a line in Cuba (then a Spanish overseas province) connecting Havana and Bejucal had already opened in 1837. In 1852 the first narrow gauge line was built, in 1863 a line reached the Portuguese border. By 1864 the Madrid-Irun line had been opened, and the French border reached.
In 1911 the first line to be electrified was the Gergal-Santa Fe line.
In 1941 RENFE was created.
The last steam locomotive was withdrawn in 1975, in 1986 the maximum speed on the railways was raised to 160 km/h (99 mph), and in 1992 the Madrid–Seville high-speed rail line opened, beginning the process of building a nationwide high speed network.