Getting from Spain to Portugal overland is a piece of cake and you have various options.
There is an overnight train called the Lusitania Tren-Hotel which departs from Chamartin station in Madrid and takes ten and a half hours to get to Lisbon.
If you are heading on to somewhere other than Lisbon then you can change at Entroncamento in Portugal for trains to either Coimbra or Porto in the north.
Prices start at €60 based on a basic one way ticket in second-class. There are other options such as single cabins, double cabins, and posh cabins also available and so you can enjoy the luxury of a good night of sleep if your budget allows.
From Southern Spain
If you are coming from Granada, Cordoba or Sevilla then you can easily take a bus to the border town of Ayamonte or Vila Real de Santo Antonio before taking either a bus or train along the coast of the Algarve.
From the North East of Spain
If you are coming from the North East then there is an overnight train to Lisbon that comes from France (the Sud-Express from Hendaye / Irun) and passes through both San Sebastian and Salamanca before entering Portugal at Vilar Formoso in Beira Alta and Guarda – Portugal’s highest town.
You can change trains at Coimbra in order to head north to Porto.
The journey takes 13 hours and is as comfortable as your budget allows Tickets start at around €70 one way and if you want a bunk in a cheap six-berth cabin you will pay around €87 one way.
From the North West of Spain
From Vigo in Galicia there are trains directly to Porto which take just over three hours and cost around thirteen euros in one direction. This route follows the River Minho down the coas to Vila Praia de Ancora, Vina do Castelo and Barcelos.