The insertion of the M2 transmembrane peptide from influenza A virus into a membrane has been studied with molecular-dynamics simulations. This system is modeled by an atomically detailed peptide interacting with a continuum representation of a membrane bilayer in aqueous solution. We performed replica-exchange molecular-dynamics simulations with umbrella-sampling techniques to characterize the probability distribution and conformation preference of the peptide in the solution, at the membrane interface, and in the membrane. The minimum in the calculated free-energy surface of peptide insertion corresponds to a fully inserted, helical peptide spanning the membrane. The free-energy profile also shows that there is a significant barrier for the peptide to enter into this minimum in a nonhelical conformation. The sequence of the peptide is such that hydrophilic amino acid residues at the ends of the otherwise primarily hydrophobic peptide create a trapped, U-shaped conformation with the hydrophilic residues associated with the aqueous phase and the hydrophobic residues embedded in the membrane. Analysis of the free energy shows that the barrier to insertion is largely enthalpic in nature, whereas the membrane-spanning global minimum is favored by entropy.