The chemical reactions and pathways resulting in the formation of a hydrocarbon, a compound consisting of carbon and hydrogen only.