The existence and stability of spin-liquid phases represent a central topic in the field of frustrated magnetism. While a few examples of spin-liquid ground states are well established in specific models (e.g. the Kitaev model on the honeycomb lattice), recent investigations have suggested the possibility of their appearance in several Heisenberg-like models on frustrated lattices. An important related question concerns the stability of spin liquids in the presence of small perturbations in the Hamiltonian. In this respect, the magnetoelastic interaction between spins and phonons represents a relevant and physically motivated perturbation, which has been scarcely investigated so far. In this work, we study the effect of the spin-phonon coupling on prototypical models of frustrated magnetism. We adopt a variational framework based upon Gutzwiller-projected wave functions implemented with a spin-phonon Jastrow factor, providing a full quantum treatment of both spin and phonon degrees of freedom. The results on the frustrated $J_1-J_2$ Heisenberg model on one- and two-dimensional (square) lattices show that, while a valence-bond crystal is prone to lattice distortions, a gapless spin liquid is stable for small spin-phonon couplings. In view of the ubiquitous presence of lattice vibrations, our results are particularly important to demonstrate the possibility that gapless spin liquids may be realized in real materials.