The widespread use of portable nuclear magnetic resonance (NMR) technology in many fields has attracted lots of researchers’ attention. Nevertheless, it has a few drawbacks such as large volume, poor uniformity and end effect limit. Therefore, this paper designs a new three-dimensional structure of 9-layer permanent magnet based on the traditional Halbach structure, and proposes an innovative optimization scheme of adjusting the height of the magnetic block by pair and optimizing step by step to adjust the uniformity of the main magnet system. Each layer of the instrument developed in this study consists of 16 trapezoidal permanent magnets, with a total volume of Φ240×141.8mm. The quality factor (Q) represents the uniformity of magnetic field at both ends of the central cylinder region. Through simulation, it is found that in the central area of the optimized structure (Φ20×20mm), the resultant magnetic field density is 1.092T and the uniformity is 419ppm. The structure which is developed in this paper has the advantages of small size, compact form and good uniformity so that it is suitable for being adequated for the portable NMR systems.