Reconfigurable intelligent surface (RIS) and hybrid beamforming have been envisioned as promising alternatives to alleviate blockage vulnerability and enhance coverage capability for terahertz (THz) multi-user multiple-input multiple-output systems that suffer from severe propagation attenuation and poor diffraction. Considering that the joint beamforming with large-scale array elements at transceivers and RIS is extremely complicated, the codebook based beamforming can be employed in a computationally efficient manner. However, the codeword selection for analog beamforming is an intractable combinatorial optimization (CO) problem. To this end, an iterative alternating search (IAS) algorithm is developed to achieve a near-optimal sum-rate performance with low computational complexity in contrast with the optimal exhaustive search algorithm. According to the THz channel dataset generated by the IAS algorithm, a multi-task learning based analog beam selection (MTL-ABS) framework is developed to further decrease the computation overhead. Specifically, we take the CO problem as a multi-task classification problem and implement multiple beam selection tasks at transceivers and RIS simultaneously. Remarkably, residual network and self-attention mechanism are used to combat the network degradation and mine intrinsic THz channel features. Finally, blockwise convergence analysis and numerical results demonstrate the effectiveness of the MTL-ABS framework over search based counterparts.