Edit: for future readers: please always prefer to install the binary version of ROS packages, if at all possible.
Building ROS packages from sources is not the recommended way to install them and should not be your default.
Easiest way to find source code for released (and indexed) packages would be index.ros.org.
For turtlesim
specifically: index.ros.org/p/turtlesim:
In the screenshot:
- the main link to the source repository. In this case: https://github.com/ros/ros_tutorials.git
- the name of the branch. In this case:
galactic-devel
- tabs which allow you to switch to a different ROS release. Note how GALACTIC is selected here. To switch to the Foxy version of the package/page, click the FOXY tab. The VCS Version field in the Repository Summary will automatically update to show the correct branch name (for Foxy it would switch to
foxy-devel
) - a convenience button which will take you to the source repository and open the correct branch for the package version selected using the tabs (3). In this case: https://github.com/ros/ros_tutorials/...
@Zhoulai Fu: would it perhaps be an idea to make your question (title) a little more generic? Something like "how to find the source code for ROS 2 packages?", and then use the turtlesim package as an example for which you'd like to find the source?
I feel that would help future users find the Q&A more easily.