Obtaining a student residence card
If you are a citizen from outside the EU, you should apply for a student visa at the Spanish consulate in your home country or your country of legal residence. Applicants should submit the documents that show that they have been admitted to a programme, and apply for a student visa (type D).
Please find more information here