The mildest and most thoughtful of men, Galt MacDermot, who has died aged 89, might be considered an unlikely composer of hippy anthems, ballads protesting against the Vietnam war, and the scatological interracial sex hymns of Hair (1967). That show changed musical theatre on Broadway and, in London in September 1968, celebrated the end of the lord chamberlain’s protracted reign of censorship in the theatre.

MacDermot, a Canadian who was primarily a jazz musician, studied music in Cape Town, where his father, a diplomat, was Canada’s high commissioner to South Africa. He blended his obsession with the music of Duke Ellington with that of artists such as Miriam Makeba and Hugh Masekela.

He regarded Hair, which betrays these influences, as “a total funk show”, even though the subject matter of the libretto and songs by Gerome Ragni and James Rado encompassed psychedelic trips, tribal chorales, poignant ballads and the soulful rock splendour of the sensational opening and closing numbers, Age of Aquarius and Let the Sunshine In. The pop group the Fifth Dimension remastered these two songs as a 1969 medley and, with Good Morning Starshine, they gave MacDermot three solid hits in a year.

Hair was a phenomenon. It had been turned down by every major Broadway producer until Michael Butler moved it uptown to the Biltmore from Joe Papp’s Public Theater on Lafayette Street. Diane Keaton was in the cast that broke through the fourth wall, stripped off at the end of the first act, and laid siege to the sophisticated urban musical theatre of Leonard Bernstein and Richard Rodgers (both of whom loathed the show).

MacDermot with James Rado in 2009. Photograph: MediaPunch/Rex/Shutterstock

In London, the run was even longer, halting only in 1973 when the ceiling collapsed overnight in the Shaftesbury theatre. The cast – which included Elaine Paige, Marsha Hunt, Paul Nicholas and Tim Curry – were joined on stage by members of the audience at every performance. Even Princess Anne was caught jiggling around with the cast of free spirits. The show has always revived well as a period piece.

And MacDermot’s music – not just from Hair – was acknowledged by the emergent hip-hop crowd in the 1990s as an inspiration, as they beat a path to his grand old brick house on Staten Island, New York, to sample his soundtracks and jazz recordings. Even before Hair he had established his own label, Kilmarnock Records, which, through the new millennium, released one album a year featuring music he performed with his jazz group, New Pulse, in concert at Carnegie Hall.

MacDermot was born in Montreal, the son of Elizabeth (nee Savage) and Terence MacDermot, and educated at Upper Canada college, a private school in Toronto, and at Bishop’s University in Sherbrooke, Quebec. The family moved in 1950 to Cape Town, where MacDermot made a specialist study of African music at the university, and met and in 1956 married Marlene Bruynzeel. Returning to Montreal, the couple started a family and MacDermot took a job as an organist in a Baptist church while playing in bands on the side.

His first theatre music, in 1955, led to a record deal and he won his first Grammy in 1961 for the album African Waltz, for the jazz saxophonist Cannonball Adderley. The title number had been a hit single for Johnny Dankworth, whom MacDermot had met and been encouraged by on a brief stay in London. There he also met a fellow Canadian expat, William Dumaresq, who would collaborate as librettist on almost all of his later musicals, apart from Hair.

In 1964 MacDermot settled in New York, where a record industry colleague introduced him to Rado and Ragni, and he started setting their scribbles to music. When they presented him with a script of Hair, he composed the score in three weeks.

The cast rehearsing for the 1968 production of Hair before it opened at the Shaftesbury theatre in London’s West End. Photograph: Central Press/Getty

Only one of his subsequent musicals – Two Gentlemen of Verona (1971), adapted from Shakespeare by John Guare, and starring Raúl Juliá – had a success anywhere comparable to Hair’s. It won two Tony awards, and was deemed best musical ahead of Grease and Follies. When the show came to the Phoenix in London in 1973, it did well, but lacked the outdoor summer zing and sparkle of Papp’s production in the Delacorte theatre in Central Park.

The flops piled up: a grim Brothers Grimm fable, Isabel’s a Jezebel (1970), premiered at the Duchess in London, with Carole Hayman unwisely advancing to the front of the stage, the night I went, to ask, “What is all this, anyway?” The corporate response from the gallery was almost deafening, accompanied by the mass tipping up of seats all over the house. It lasted for 13 performances on Broadway.

Dude (1972), adjudged an incomprehensible Everyman allegory, written with Ragni, managed 16 performances, while Via Galactica (1972), a futuristic space odyssey about social outcasts directed by Peter Hall, to open the spanking new Uris (now the Gershwin), made social outcasts of its creative team after just seven performances.

However, another failure, The Human Comedy (1983), based on a William Saroyan novella about a small wartime community in California, has a wonderful folk opera score, as revealed in a 2010 revival by the Young Vic and the Opera Group. It is possible that MacDermot’s inventive, jazzy eclecticism has been blanked out by the predominance of Hair, which sounds better every time you hear it. The Human Comedy is full of great gospel and blues, fine and touching ballads, and the choral writing is superb.

His soundtrack for the documentary Woman Is Sweeter has been sampled, while other movie work included Cotton Comes to Harlem (1970), which starred Godfrey Cambridge; Rhinoceros (1974) adapted by Julian Barry from Ionesco’s play, and starring Zero Mostel, Karen Black and Gene Wilder; and Mistress (1992), with Robert De Niro. Miloš Forman’s 1979 movie of Hair, scripted by Mike Weller, with choreography by Twyla Tharp, is pretty good, though Ragni and Rado disapproved of some rewrites.

Hair itself returned in triumph to Broadway and London in 2010 in a wonderful revival by Diane Paulus, while earlier this year, the little Hope Mill musical theatre in Manchester sent out a spunky, affectionate new kaftan-rich version on tour and to the Vaults in Waterloo to celebrate the show’s 50th anniversary.

MacDermot’s wife survives him, as do their five children, Vincent, Molly, Yolanda, Sarah and Elizabeth, seven grandchildren and two great-grandchildren, and a sister, Anne.

• Arthur Terence Galt MacDermot, composer and musician, born 18 December 1928; died 17 December 2018