If you are not a citizen of an EU state, you must apply for a visa if your period of study will be greater than 3 months long – that being the period of time you are entitled to stay in Spain under a regular tourism visa (such tourism visas being included in a passport from any country exempted from the requirement to present a Visa when entering the country).
For those wishing to study for between 3-6 months, or for longer than 6 months, a Visa (type D) is required.
Once the visa has been obtained – at the appropriate Spanish consulate in your country of residence - the student/researcher must furthermore obtain a TIE Residency Card for Students within 30 days of arriving in Spain.