In order to distinguish different varieties of Lycium barbarum effectively, a fast nondestructive detection method based on hyperspectral imaging technology was proposed. Six varieties of L. barbarum were selected as the research objects to obtain hyperspectral images. With the whole L. barbarum taken as the object, the region of interest was obtained by threshold segmentation, and the average spectra value of the image points of a single L. barbarum was extracted as the spectral data of the sample. Initially, standard normalized variate was used to preprocess the original spectral data. Furthermore, compared with other methods, competitive adaptive reweighted sampling (CARS) was chosen to extract the characteristic wavelengths. Additionally, the model of support vector machine (SVM) was set. The results showed that the SVM model based on CARS had the best classification effect. The training set accuracy was 100%, and the prediction set accuracy was 85%. Finally, in order to improve the classification accuracy, the whale optimization algorithm (WOA) was introduced. The accuracy of training set and prediction set obtained by WOA‐SVM model were 89.44 and 88.33% respectively. Therefore, it was feasible to use hyperspectral imaging technology combined with CARS‐WOA‐SVM model to identify different varieties of L. barbarum.