This article is more than
2 year oldSex outside marriage will carry a jail term of up to a year under the new laws, which take effect in three years.
The raft of changes come after a rise in religious conservatism in the Muslim-majority country.
Critics see the laws as a "disaster" for human rights, and a potential blow to tourism and investment.
Several groups of mainly young people protested against the legislation outside parliament in Jakarta this week. It's expected the new laws will be challenged in court.
They apply equally to locals and to foreigners living in Indonesia, or visiting holiday destinations such as Bali. Under the laws unmarried couples caught having sex can be jailed for up to a year.
They are also banned from living together - an act for which people could be jailed for up to six months. Adultery will also be an offence for which people can be jailed.
Supporters of the legislation say changes were made to accommodate concerns of critics - for prosecutions to start a complaint must be filed by the children, parents or spouse of the accused couple.
Ajeng, a 28-year-old Muslim woman living in the West Java city of Depok, said she was now at risk for living with her partner for the past five years.
"With the new law, both of us can go to jail if one of the family decides to make a police report," she told the BBC.
"What if there's one family member who has a problem with me and decides to send me to jail?
"I think living together or having sex outside of marriage is not a crime. In my religion, it's considered a sin. But I don't think the criminal code should be based on a certain religion."
She said she had joined the nationwide protests in 2019 when the law had first been broached. She took the sign: "For the right to cuddle, I took to the streets."
However on Tuesday, parliament unanimously approved the new code of over 600 articles.
Rights groups say the new provisions disproportionately affect women, LGBT people and ethnic minorities.
Many businesses had also been opposed to the legislation, saying it discouraged visitors and investment. But lawmakers have celebrated overhauling laws dating back to Dutch colonial rule.
"It is time for us to make a historical decision on the penal code amendment and to leave the colonial criminal code we inherited behind," law minister Yasonna Laoly told parliament.
The new legislation contains scores of new clauses criminalising immorality and blasphemy and restricting political and religious expression.
Human Rights Watch's Asia Director Elaine Pearson told the BBC it was a "huge setback for a country that has tried to portray itself as a modern Muslim democracy".
The group's Jakarta-based researcher, Andreas Harsano, said there were millions of couples in Indonesia without marriage certificates "especially among Indigenous peoples or Muslims in rural areas" who had married in specific religious ceremonies.
"These people will be theoretically breaking the law as living together could be punished up to six months in prison," he told the BBC.
He added that research from Gulf states, where there are similar laws governing sex and relationships, showed women were punished and targeted by such morality laws more than men.
There are now also six blasphemy laws in the code, including apostasy - renouncing a religion. For the first time since its independence, Indonesia will make it illegal to persuade someone to be a non-believer.
New defamation articles also make it illegal for people to insult the president or criticise state ideology.
However legislators said they had added defences for free speech and protests made in the "public interest".
Indonesia is not a secular state. Atheism is unacceptable - technically you need to follow one of six prescribed religions. So it is a multi-religious state with an official ideology, Pancasila, which prioritises no faith over any other. That was Indonesia's post-independence leader Sukarno's idea, to discourage large parts of the archipelago where Muslims are not a majority from breaking away.
But since the fall of his successor Suharto - who ruthlessly suppressed political Islamic groups - there has been growing mobilisation around Islamic values, the sense that Islam is threatened by outside influences and more conservatism in many areas of the island of Java, where more than half of Indonesians live. Political parties have responded to this and demanded tougher laws to police morals.
Current leader Joko Widodo is from the syncretic Javanese tradition that adheres to a more flexible form of Islam, but his main preoccupation is his legacy of economic development rather than tolerance and liberal values. He has shown, for example in the jailing of former Jakarta governor Ahok on blasphemy charges, that he's willing to give hardline Muslims some of what they want.
By the time the new code comes into effect, Jokowi will have left office at the end of his second term.
Since Indonesia's democratic transition in 1998, strict religion-based laws on sex and relationships have been introduced in some parts of the country of 267 million people.
The province of Aceh already enforces strict Islamic law and has punished people for gambling, drinking alcohol and meeting members of the opposite sex.
Many Islamic civil groups in Indonesia have been pushing for more influence in shaping public policy in recent years.
A previous draft of the code was set to be passed in 2019 but sparked nationwide protests with tens of thousands taking part in demonstrations.
Many, including students, took to the streets and there were clashes with police in Jakarta.
Ajeng said many Indonesians who were not affected by the law had also protested in 2019 because "people don't want their taxes to be used to send people to jail just for sex".
"People are angry that their liberty is being taken. Indonesia has plenty of problems like poverty, climate change and corruption, but instead of solving a problem they've created a bill that only adds to the problem."
Newer articles