At room temperature, PbTe and SnTe are efficient thermoelectrics with a cubic structure. At low temperature, SnTe undergoes a ferroelectric transition with a critical temperature strongly dependent on the hole concentration, while PbTe is an incipient ferroelectric. By using the stochastic self-consistent harmonic approximation, we investigate the anharmonic phonon spectra and the occurrence of a ferroelectric transition in both systems. We find that vibrational spectra strongly depend on the approximation used for the exchange-correlation kernel in density-functional theory. If gradient corrections and the theoretical volume are employed, then the calculation of the phonon frequencies as obtained from the diagonalization of the free-energy Hessian leads to phonon spectra in good agreement with experimental data for both systems. In PbTe we evaluate the linear thermal expansion coefficient $\ensuremath{\gamma}=2.3\ifmmode\times\else\texttimes\fi{}{10}^{\ensuremath{-}5}\phantom{\rule{4.pt}{0ex}}{\text{K}}^{\ensuremath{-}1}$, finding it to be in good agreement with experimental value of $\ensuremath{\gamma}=2.04\ifmmode\times\else\texttimes\fi{}{10}^{\ensuremath{-}5}\phantom{\rule{4.pt}{0ex}}{\text{K}}^{\ensuremath{-}1}$. Furthermore, we study the phonon spectrum and we do reproduce the transverse optical mode phonon satellite detected in inelastic neutron scattering and the crossing between the transverse optical and the longitudinal acoustic modes along the $\mathrm{\ensuremath{\Gamma}}\mathrm{X}$ direction. The phonon satellite becomes broader at high temperatures but its energy is essentially temperature independent, in agreement with experiments. We decompose the self-consistent harmonic free energy in second-, third-, and fourth-order anharmonic terms. We find that the third- and fourth-order terms are small. However, treating the third-order term perturbatively on top of the second-order self-consistent harmonic free energy overestimates the energy of the satellite associated with the transverse optical mode. On the contrary, a perturbative treatment on top of the harmonic Hamiltonian breaks down and leads to imaginary phonon frequencies already at 300 K. In the case of SnTe, we describe the occurrence of a ferroelectric transition from the high-temperature $Fm\overline{3}m$ structure to the low-temperature $R3m$ one. The transition temperature is, however, underestimated with respect to the experimental one. No satellites are present in the SnTe phonon spectra despite a not negligible anharmonic broadening of the zone-center TO mode.