Assuming the you have python3 and pip installed on ubuntu.
Run the command
pip3 install torch
Once you have installed you can verify by running the following code