Portable nuclear magnetic resonance (NMR) instruments are widely used in many fields. However, the large volume, low uniformity and end effect limits the application of portable NMR instruments. In order to improve the uniformity and compensate the end effect, a Halbach structure with 9-layer permanent magnet is proposed, which is optimized by axially adjusting the magnet height based on the Halbach array principle and Quality factor (Q) is introduced to represent the magnetic field uniformity at both ends of the central cylinder region. Each layer consists of 16 permanent magnets with trapezoidal cross section and the total volume is Φ240 × 141.8 mm. Through simulation, it is found that the final magnetic flux density is 1.09 T and the uniformity is 418 ppm in the central region (Φ20 × 20 mm) of the optimized structure. The proposed structure has the advantages of small size, compactness in structure and homogeneity, which is very suitable for portable NMR systems.