Ubud's story starts at Campuhan, just west of today's centre, where two branches of the Wos river meet. In the 8th century, the legend goes, a Javanese priest called Rsi Markandya meditated at the confluence, a spot Hindus hold sacred, and founded a temple on the valley floor. Nirartha, the priest credited with shaping Bali's religious rites, later enlarged it. The area was known then for natural medicine and healing, and that gave the town its name: ubad is Balinese for medicine. The temple, Pura Gunung Lebah, still stands by the Campuhan bridge, and pilgrims still come.
Temples and monasteries spread through the river valleys east and northeast of town over the next four hundred years or so. Two survive that you can visit: the cave at Goa Gajah and the cliff shrines of Gunung Kawi. Many of the dances and rituals still performed in Ubud began in this period. King Airlangga, who ruled Java and Bali together, kept his seat of government at what is now Batuan, just southeast of Ubud.
In 1343 the Majapahit kingdom of Java conquered Bali, and the battle that settled it was against the Pejeng dynasty at Bedulu, just east of Ubud. Balinese culture flowered in the years that followed, and Ubud's aristocratic families trace their ancestry back to this time. In the 16th century, as Islam spread through Java, the whole Majapahit court moved east to Bali. By the late 19th century Ubud was the seat of lords of the Kshatriya caste, who owed loyalty to Gianyar's king, whose state was once the strongest in southern Bali. They were generous patrons of the village's painters and dancers.
Ubud asked to become a Dutch protectorate in 1900. The colonial rulers left it mostly alone, so the old arts carried on with little change. In the 1930s the royal family invited foreign artists to come and live in the town, and from here Walter Spies and Rudolf Bonnet did much to make Balinese art understood around the world. The palace took in paying guests in the same decade, which made it one of Ubud's first hotels. Tjokorda Gede Agung Sukawati, the last ruling king, died in 1978, and his family still owns the palace.
More artists followed. The Spanish-American painter Antonio Blanco moved here in 1952 and stayed until he died in 1999. In the 1960s the Dutch painter Arie Smit arrived and the Young Artists movement took off, just as the first travellers began to find their way in over roads that were still poor. Murni's Warung opened by the Campuhan bridge in 1974, the first real restaurant in town. Since the late 1960s Bali's tourist boom has built Ubud up, and you can see it in the guesthouses, galleries and cafés packed along Jalan Monkey Forest.
The terrorist bombings of 2002 drove visitors away from all of Bali, Ubud included. The town's answer was a writers' festival, set up to bring them back. They came back in numbers. The central village, home to about 12,000 people, now receives more than three million foreign tourists a year. By 2018 Ubud was drawing more visitors than Denpasar. Parts of the centre are struggling to cope. Along the rivers, though, the terraced fields are still there, and once you're off the main roads, village life goes on much as it always has.