A method is described to calculate the electron spin resonance (ESR) g tensor as a derivative of the energy. The method is formally based on the second derivatives of the square of the Zeeman splitting energy. This can be reduced to a first derivative, and then a Hellmann-Feynman formula can be used. A general Hartree-Fock (GHF) method to calculate the g tensor is described and implemented. Formula for the one and two electron spin-orbit contributions are presented. The GHF method is used to calculate g tensor elements for CO+, CN, MgF, NO2, NF2, CO2-, O-3(-), OH2+, HCO, and H2CO+ In most cases, the agreement is within or close to the expected experimental accuracy of 500 ppm for neon matrix data, but there are still some significant discrepancies. The results are in better agreement with configuration interaction (CI) type calculations than density functional theory (DFT) calculations, although the differences are small. The results are significantly different from perturbation theory predictions. It is found that the diamagnetic contributions to the g tensor are not significant to within the experimental errors, if matrix isolation data is used. The g values for the CoCl42- anion are also studied to show the methodology can be extended to larger systems. (C) 1998 American Institute of Physics.