Writing all custom code as a plugin is a best practice, but it's really not needed in many circumstances.
You can create a folder anywhere in the folder that contains the folders and files for your theme or its child theme and keep your library's code there. That's going to be somewhere under the wp-content folder.
Note: even though we can put our own code into the theme's folders it is a best practice to always use a child theme for our code whether our code is a plugin or not.
Then once in a child-theme, you need to learn how to "enqueue" the PHP library so it can be loaded into memory and used by your code that calls on its functionality. That's done in the functions.php file that must be in the root of every theme or child theme folder.
That's why IMO it can be rather specious to argue against keeping assets in the theme/child theme, because we have to go there to write the code to use the library anyway and if you need portability you can use that thing called copy and paste out of your child theme into whatever you may need in the future. Furthermore, the maintenance is not affected at all. It's like saying I'm smart enough to write my code, but I don't know where it is when I may need to maintain it.
If you have the income I recommend buying into some training from Tonya Mork because the question asked is not so easy to understand by those with no coding skills yet and her instructional style is top shelf.