A method to calculate the effective spin Hamiltonian for a transition metal impurity in a nonmagnetic insulating host is presented and applied to the paradigmatic case of Fe in MgO. In the first step we calculate the electronic structure employing standard density functional theory (DFT), based on generalized gradient approximation (GGA), using plane waves as a basis set. The corresponding basis of atomic-like maximally localized Wannier functions is derived and used to represent the DFT Hamiltonian, resulting in a tight-binding model for the atomic orbitals of the magnetic impurity. The third step is to solve, by exact numerical diagonalization, the N electron problem in the open shell of the magnetic atom, including both effects of spin-orbit and Coulomb repulsion. Finally, the low energy sector of this multi-electron Hamiltonian is mapped into effective spin models that, in addition to the spin matrices S, can also include the orbital angular momentum L when appropriate. We successfully apply the method to Fe in MgO, considering both the undistorted and Jahn-Teller (JT) distorted cases. Implications for the influence of Fe impurities on the performance of magnetic tunnel junctions based on MgO are discussed.which is fundamentally different from the multiplet nature of the real system. In this context, we find it convenient to have a constructive theoretical approach to derive the effective spin Hamiltonian, starting from an atomistic DFT description of the electronic properties of the system, but describing the electronic properties of the system with a multi-electron approach that captures the multiplet nature of the relevant electronic states.Here we propose a method to obtain an effective spin Hamiltonian for a magnetic atom in an insulating host, starting from density functional calculations, in four well-defined steps. First, a density functional calculation of the electronic properties of the magnetic atom inside the non-magnetic host is performed. The second step is to represent the effective DFT Hamiltonian with a basis of localized atomic orbitals, which allows us to obtain the crystal and ligand fields terms of the atomic orbitals of the relevant open shell of the magnetic atom, defining thereby a multi-orbital Hubbard Hamiltonian. Since our DFT approach makes use of a plane-wave basis, we implement this step by means of the wannierization [13] technique. Up to this point, the methodology is very similar to previous work [14][15][16][17][18][19][20][21]. In the third step we add to the Hubbard model the intra-atomic Coulomb repulsion and the spin-orbit coupling for the electrons in the open-shell. The final step is a symmetry analysis of the spectrum and wave functions, obtained by numerical diagonalization of the effective Hubbard model. The resulting multi-electron state analysis permits the construction of an effective spin Hamiltonian for the system.Below we describe in more detail the method and apply it to the paradigmatic case of Fe 2+ as a substitutional impurity of Mg in MgO [1], a band insu...