Be very explicit, from the beginning, that you do not do fixed price contracts (which is essentially really what clients always want) unless the job is obviously trivial enough to estimate properly - and even then, there is always a chance something might occur.<p>Make better estimates to account for such unknowns like having to install packages because you're dealing with a codebase someone else developed - be very detailed in these estimates you provide as well and provide not a single number but a range.<p>If they still grumble about the cost, then maybe offer to reduce the cost of trivial work like setting up the necessary environment by 1/3 or something (only if the client is a decent one and just needs a bit of education...)<p>Provide good info about the reasons for extra time - you're trying to get the client to ultimately trust you, that you know what you're doing, and are focused on trying to not spend as much of their money as you'd like. So keep them in the loop as much as possible w/o being annoying about what is going on. A daily or weekly update? A heads-up before diving into something time consuming? E.g. if the code you're dealing with is garbage, explain that in a professional way (and clearly, but not too much detail) so they understand the issue (to the extent they need to understand it)<p>So basically it's a process of educating the client, over time or however efficiently enough, about the true nature of IT work. Metaphorically, you're a plumber or a general contractor, or a carpenter, not a factory worker. Everyone knows that sort of work always involves more time than expected sometimes. If they still complain, then they're a bad client and let them go find someone on Upwork instead.