Eruptions at active silicic volcanoes are often cyclical. For example, at the Soufrière Hills volcano in Montserrat1, Mount Pinatubo in the Philippines2, and Sakurajima in Japan3, episodes of intense activity alternate with repose intervals over periods between several hours and a day. Abrupt changes in eruption rates have been explained with the motion of a plug of magma that alternatively sticks or slides along the wall of the volcanic conduit4, 5. However, it is unclear how the static friction that prevents the plug from sliding is periodically overcome. Here we use two-phase flow equations to model a gas-rich, viscous magma ascending through a volcanic conduit. Our analyses indicate that magma compaction yields ascending waves comprised of low- and high-porosity bands. However, magma ascent to lower pressures also causes gas expansion. We find that the competition between magma compaction and gas expansion naturally selects pressurized gas waves with specific periods. At the surface, these waves can induce cyclical eruptive behaviour with periods between 1 and 100 hours, which compares well to the observations from Soufrière Hills, Mount Pinatubo and Sakurajima. We find that the period is insensitive to volcano structure, but increases weakly with magma viscosity. We conclude that observations of a shift to a longer eruption cycle imply an increase in magma viscosity and thereby enhanced volcanic hazard.