We study the lattice dynamics effects on the phase stability and magnetocrystalline anisotropy (MCA) energy of CaCu5-type YCo5 at finite temperatures using first-principles calculations based on density functional theory (DFT). Harmonic lattice dynamics (HLD) calculations indicate that YCo5 with 56 full valance electrons is dynamically unstable and this instability can be cured by reducing the number of electrons (Ne). Crystal orbital Hamilton population analysis reveals that the observed phonon instability originates from the large population of antibonding states near the Fermi level, which is dominated by the Co atoms in the honeycomb layer. The antibonding state depopulates with decreasing Ne, resulting in stable phonons for hole-doped YCo5 with Ne$łeq$55. We then evaluate the temperature-dependent MCA energy using both HLD and ab initio molecular dynamics (AIMD) methods. For the pristine YCo5, we observe a very weak temperature decay of the MCA energy, indicating a small effect of lattice dynamics. Also, the MCA energies evaluated with AIMD at all target temperatures are larger than that of the static hexagonal lattice at 0 K, which is mainly attributed to the structural distortion driven by soft phonon modes. In the hole-doped YCo5, where the distortion is suppressed, a considerable temperature decay in MCA energy is obtained both in HLD and AIMD methods, showing that lattice dynamics effects on MCA energy are non-negligible.