We report first-principles calculations of the phonon dispersion spectrum, thermal expansion, and heat capacity of uranium dioxide. The so-called direct method, based on the quasiharmonic approximation, is used to calculate the phonon frequencies within a density functional framework for the electronic structure. The phonon dispersions calculated at the theoretical equilibrium volume agree well with experimental dispersions. The computed phonon density of states (DOS) compare reasonably well with measurement data, as do also the calculated frequencies of the Raman and infrared active modes including the LO/TO splitting. To study the pressure dependence of the phonon frequencies we calculate phonon dispersions for several lattice constants. Our computed phonon spectra demonstrate the opening of a gap between the optical and acoustic modes induced by pressure. Taking into account the phonon contribution to the total free energy of UO$_2$ its thermal expansion coefficient and heat capacity have been {it ab initio} computed. Both quantities are in good agreement with available experimental data for temperatures up to about 500 K.