I bought mine at 36 weeks, and we finally set it up when I was 40 weeks. But it was only a 5 day shipping, and we knew that the baby was going to be in our room for the first few weeks anyway, so we weren't in a rush. We were actually going to wait until we were ready to move baby into her own room, but there was a really good deal on the one we wanted, so we purchased it. It is really up to you, and if you are the type of person that feels the nursery HAS to be done before the baby is born or not.
I waited until after we got good results at our anatomy scan at 20 weeks. I figured even if the crib took a while to arrive, I planned on having DS room-in with us in our bedroom, since I planned to nurse him, so it wasn't necessary to have the crib assembled before he was born.
We put ours up at 2ish weeks. After our 20 week anatomy scan and gender reveal, we registered and took off all holds of buying things. I wouldn't wait too long -- we ordered ours from Target and the back was cracked so we had to wait another week or so for a new part before we could assemble it.
Re: when to buy a crib?
Baby Boy due October 2017