Kanchanaburi began as a military post. In the late 18th century King Rama I set it up in what is now the Lat Ya area, as a defence against Burmese attacks. Through the Ayutthaya, Thonburi and Rattanakosin periods it stayed an outpost on the Burmese side of the kingdom.
In the early 1830s King Rama III moved the town about 16 km southeast, to the point where the Khwae Yai and Khwae Noi rivers meet. Sources give 1831 and 1833 for the move.
A royal decree in 1935 set up the two subdistricts of Ban Nuea and Ban Tai, covering about 2 square kilometres. The population was 25,651 in 2017.
In 1942 the area was under Japanese control, and the Japanese army pushed a railway from Nong Pla Duk in Ban Pong, through Kanchanaburi and across the Khwae Yai, to Thanbuyuzayat in Burma. Asian forced labourers and Allied prisoners of war built it. The work ran from October 1942 to October 1943, and about 300 km of the line lay in Thai territory.
The cost was enormous. Some 16,000 prisoners of war and 90,000 Asian workers died, and almost half the prisoners succumbed to disease, ill-treatment or accidents. Four prisoner camps stood in or near the city, and when the line was finished most prisoners were gathered around Kanchanaburi. The bridge you see now is the second wartime one: the Allies bombed three sections, and two central box spans replaced them after the war.
After the war the Allies moved the graves of prisoners buried along the line into two war cemeteries in Kanchanaburi, so they could be kept up more easily. Parts of the track were pulled up, and some sections now lie under the lake behind the Khao Laem Dam. Hellfire Pass was only relocated in the 1980s and reclaimed from the jungle, and the Thailand-Burma Railway Centre opened in March 2003. Films fictionalised the story: The Bridge on the River Kwai in 1957, and later Return from the River Kwai and The Railway Man.