Residing in the 5' untranslated region of the mRNA, the 2'-deoxyguanosine (2'-dG) riboswitch mRNA element adopts an alternative structure with the binding of the 2'-dG molecule, which terminates transcription. In general, RNA conformations are strongly affected by positively charged metal ions (especially Mg2+). We have quantitatively explored the combined effect of ligand (2'-dG) and Mg2+ binding on the energy landscape of the aptamer domain of the 2'-dG riboswitch with both explicit solvent all atom molecular dynamics simulations and SHAPE biochemical probing experiments. We show that both ligand and Mg2+ are required for the stabilization of the aptamer domain; however, the two factors function in different ways. While the addition of Mg2+ remodels the energy landscape and reduces its frustration by the formation of additional contacts, the binding of 2'-dG eliminates the metastable states by building a compact core for the aptamer domain. In particular, Mg2+ ions and ligand binding are required to stabilize the most unstable helix P1 (which needs to unfold to activate the transcription platform), and the riboswitch core formed by the backbone of the P2 and P3 helices. They also facilitate a more compact structure in the three-way junction region.