Using the first-principle pseudopotential plane wave (PPW) method based on the density functional theory, structural optimization was conducted on MgEr, Mg2Er and Mg24Er5 intermetallic compounds of the binary Mg-Er alloys, and the internal relations of the stability and crystal structure of the Mg-Er intermetallic compounds were analyzed by calculating the formation heat, binding energy and electronic structure. The results show that in Mg-Er alloys, the formation heat and binding energy of three intermetallic compounds are all negative, and the alloying ability and structural stability of Mg-Er compounds are in decline with decreasing the content of Er. In low-energy region of Fermi level, the energy band is mainly dominated by hybridization of 4f and 5d orbits of Er with the 2p and 3s orbits of Mg, while in high-energy region of the Fermi level, those bonds are mainly contributed by electrons of 5d orbits of Er and 2p orbits of Mg. As the Er content decreases, the average quantities of bonding electron of each atom in low-energy region of Fermi level drops, which results in the weakened interaction among valence electrons and the reduced stability. There are a large number of charges around Mg and Er, suggesting the characteristics of typical metal bond. Meanwhile, Mg and Er share some charges to form covalent bond, while the distortion of the charge at the junction is little. Therefore, the valence bond of Mg-Er intermetallic compound has a characteristic of the duality wherein the metal bond predominates. With decreasing the Er content, the amount of charge transfer between Mg and Er atoms reduces gradually, which leads to the decrement of the proportion of covalent bond and the structural stability.