The assembly of a flagellum. In bacteria, this is a whiplike motility appendage present on the surface of some species; in eukaryotes, flagella are threadlike protoplasmic extensions used to propel flagellates and sperm. Flagella are composed of flagellin and have the same basic structure as cilia but are longer in proportion to the cell and present in much smaller numbers.