Hagenbach was born on July 2, 1771, to a well-known Basel family with origins in Mulhouse in Alsace.
His parents were the merchant Johann Rudolf Hagenbach and Katharina Margaretha Kaiser from Barr, Alsace.
Even as young man he was a member of the Helvetic Society where he wrote a poem Der Freye Schweizer (1790) showing a liberal leaning.
He studied medicine in Strasbourg, Erlangen and Göttingen and received a doctorate in 1795 with a thesis on respiration in plants and animals.
Only two students were awarded doctorates and along with Johann Jakob Stückelberger (1758–1838) he campaigned for shutting down the medical faculty.