When a company is setting up a PBX and wants to use a SIP trunk, why does it have to buy it? Is there no way to provide itself with one on its own?
To "roll your own" SIP trunking, you need some sort of PSTN interface. Often this is a PRI for larger businesses. I will use PRI as an example in this situation.
1) PRI 2) PRI interface 3) SIP server 4) SIP client
To obtain a PRI, you need to contact your phone company, and they will install one on your premises. Once they have installed the PRI, you will connect it to your SIP server with your PRI interface. PRI interfaces come in many forms, but for simplicity, let's assume that you are using a PCI based PRI card. You would install this into your server, and then begin installing your SIP server software. Personally, I recommend Elastix, FreePBX, or basically anything based on Asterisk. There are other options out there, but I think Elastix is probably the best for ease of use. The actual procedure for creating SIP trunks is out of the scope of this post, but hopefully this is a good start for you!
You're buying SIP Trunking from a provider, so that you don't have to do all the work yourself of connecting to the global telephone network.
If you wanted to "roll your own" SIP trunking service connected to the PSTN, you certainly could. It would just be extremely cost prohibitive if that was not what you were in business to do. In 99% of cases it is simply not your core business, so it doesn't make any sense to waste resources creating that connectivity.
The only real times where you don't buy SIP trunking, is when you are connecting systems that you control. For example to connect your PBX to your voice mail system. In most other cases, you purchase the SIP trunk capacity from someone who has already done the work of connecting their systems to the PSTN.
Telephone numbers are part of the Public Switched Telephone Network (PSTN) which has been around since the advent of telephones. The only reason companies with PBX's lease PSTN circuits is to have their own telephone numbers, and the ability to dial other telephone numbers.
A company leases PSTN access, whether analog lines, SIP, PRI, or T1.