Spain has become the fourth country in the European Union to legalise euthanasia and assisted suicide.
Lawmakers passed legislation on Thursday to permit euthanasia and assisted suicide for people who "suffer a serious or incurable disease or a serious or chronic condition" such that it cases them "intolerable suffering".
The law stipulates that the person must be "capable and conscious" in order to make the request.
"Today is an important day: we are heading towards the recognition of human rights. We are heading towards a more humane and fair society," Health Minister Carolina Darias said in parliament.
"Today we are a more human, fairer and freer country," Prime Minister Pedro Sanchez tweeted on Thursday.
"Thank you to all those who fought tirelessly for the recognition of the right to die with dignity in Spain."
The Bill was strongly opposed by the Spanish Catholic Church, which likened euthanasia to "a form of homocide".
Far-right party, Vox, which voted against the legislation, said it would launch an appeal in the Constitutional Court.
Until now, helping someone to end their life has been punishable by up to 10 years in prison, but studies have consistently shown a majority of Spaniards to be in favour of reforming the law.
The legislation passed by 202 votes to 141, with two abstentions, and is due to come into effect in three months. A similar law is due to come into force in New Zealand in November.
The Spanish law extends only to adult legal residents in the country, unlike Belgium which has the most relaxed euthanasia laws in the world, permitting terminally ill children in "unbearable suffering" to be helped to end their lives.