Obtaining a student residence card
If you are a citizen of a European Union country or of the United Kingdom, Liechtenstein, Iceland, Norway and Switzerland, you are entitled to free movement and residence in Spain. However, if you wish to reside in Spain for a period of time exceeding three months must register at the Central Registry Office for Foreigners in order to be issued a Number Identification as a Foreigner (NIE).
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