Few-mode erbium-doped fiber amplifier (FM-EDFA) is a key element to realize signal gain compensation in a longdistance mode division multiplexing (MDM) system. The differential modal gain (DMG) between modes directly affects the communication quality of the MDM system. In this paper, the particle swarm optimization (PSO) method is applied to design the erbium ion doping profile for high gain and low DMG simultaneously. By adjusting the doping radius and concentration concurrently, both high signal gains and low DMG can be obtained. In the conditions of the core pump and cladding pump respectively, the erbium ion with a multi-layered doping profile is automatically optimized by the PSO for a few-mode erbium-doped fiber (FM-EDF). Results show that as a three-layered ion adjustment, the gain is higher than 20 dB and DMG is lower than 0.15 dB in a four-mode step-index fiber. PSO is easy to implement and simple to operate. Compared with the other intelligent methods, such as the genetic algorithm or gradient descent optimization algorithm, PSO has no "crossover" and "mutation". The optimization time is greatly reduced. The PSO-based fiber design provides new guidance for the improvement of fiber gain equalization.