The properties of YMg in B2 structure have been comprehensively analysed using the first-principles plane-wave pseudopotential method. Specifically, the structural, electronic, elastic, vibrational, and optical properties were investigated using the generalized gradient approximation (GGA) method in the context of density functional theory. The Vienna ab initio simulation package (VASP) was utilized for these calculations. The computed lattice parameter (3.803 Å) and bulk modulus (41.33 GPa) are consistent with the earlier data on ambient pressure. The electronic band structure and energy-dependent density of states reveal the metallic nature of the titled compounds. The Born stability requirements confirmed the mechanical stability. The analysis of Pugh’s and Poisson’s ratios and Cauchy’s pressure reveals that YMg is ductile under the pressures in consideration. According to several anisotropy indices, the compound is noticeably anisotropic both in ambient and under pressure. Our investigation includes an analysis of several fundamental mechanical parameters of the material, including the bulk modulus, the pressure derivative of the Zener anisotropy factor, Poisson’s ratio, isotropic shear modulus and Young’s modulus with a particular focus on their dependence on pressure. We have determined that the elastic constants obtained remain mechanically stable, satisfying the Born Stability conditions even at high pressures of up to 60 GPa. To explore the dynamic stability of YMg, we analysed the material’s phonon dispersion curves. The examined compound displays stability under dynamic conditions from 0 GPa to 30 GPa, as evidenced by its positive vibration frequencies. However, this stability is not sustained under higher pressure, as the compound becomes unstable after 30 GPa to 70 GPa. The electronic band structure and density of states diagrams demonstrate YMg’s metallic properties. At atmospheric pressure (0 GPa), the total density of states (TDOS) near the Fermi level is approximately 1.63 states/eV, with pressure application reducing DOS. The dielectric function, refractive index, and energy loss spectra are examined within the 0–20 eV energy range. YMg exhibits its highest absorption between 4 and 11 eV. The peak optical conductivity is observed around 0.78 eV (equivalent to 1589.5409 nm), while the most significant energy loss occurs at 11.90 eV, roughly corresponding to 2.8 Hz in the ultraviolet spectrum. Moreover, we extensively analyzed the material’s phonon thermodynamic and optical properties, providing insights into its behavior under various conditions. The outcomes acquired at zero pressure are generally coherent with the current theoretical values.