This question could be marked as a duplicate of How do I disable middle mouse button click paste? although unfortunately none of the many upvoted answers for that question answer the exact question as asked. Indeed they all give solutions that only work in specific programs or solutions that don't work for any current version of ubuntu. Or they tell you how to disable the middle mouse button altogether even though that is not what you asked. I have answered the following there also but since there it will be buried amongs upvoted answers that don't actually answer the exact question I will consider this question unique since it explicitly asks for a solution that works for all programs which the other question did not.
The solution is given on yet another duplicate but now on another stackexchange site at https://unix.stackexchange.com/a/277488/288916 by Radivarig (All credits go to him). Note that I had to make one change though to make it work:
Install xbindkeys:
sudo apt-get install xbindkeys xsel xdotool
Create a configuration file for xbindkeys ~/.xbindkeysrc
with the text:
"echo -n | xsel -n -i; pkill xbindkeys; xdotool click 2; xbindkeys"
b:2
Load the configuration file using
xbindkeys -p
Add this line to ~./bashrc so that xbindkeys always loads on startup.
This is what works for me but what Radivarig suggests is to instead use the line
"echo -n | xsel -n -i; pkill xbindkeys; xdotool click 2; xbindkeys"
b:2 + Release
This last one does not work for me but if version does not work I suggest to try the other.
After a long time looking this is the only solution I found to work for me that turns off the middle mouse paste button universally without having to disable the middle mouse button completely.