The Start
In 1861, homosexuality was made a criminal offence that you could be jailed for under the Victorian law. Section 377 is introduced, criminalizing sexual activities that are “against nature” including homosexual acts. The section actually states: “Whoever voluntarily has carnal intercourse against the order of nature with any man, woman, or animal shall be punished with imprisonment for life, or with imprisonment of either description for a term which may extend to ten years, and shall also be liable to fine.” This means that whoever engages in anal sexual activities can be and will be jailed. This includes consensual sex as well.
At this point in time, India was still under British rule. It seems strange that counties that are seen as more progressive now are the same countries that imposed the sanctions against LGBT people. It is thought that the British included this because anal sex was seen as impure. Historically, India has recognized transgender people because in the Hindu religion, there is a recognized third gender and gender is seen as fluid. The British tried to erase all of that, but India's views on transgender people are similar today.
Sources: http://www.thehindu.com/specials/in-depth/LGBT-rights-the-journey-till-now/article14056726.ece
https://factly.in/tracing-the-history-of-ipc-section-377/