Before running the program after compiling, you should source the terminal:
source ~/ros2_ws/install/setup.bash