Some advice that hasn't been mentioned yet: make sure your publisher supports you. Writing a book is difficult and you want to spend your time on the content -- not making sure that there are no typos, teaching the publishers how to run the code, doing the layout, maintaining different versions of the book, etc.
I recently wrote a book, and I got very little support from the publisher. It was a disaster, although I didn't realize that until after publication. I was the first person to write a Perl book for them, and they had nobody on staff that could even install Perl. The editors didn't do any significant editing, and in fact weren't even native speakers of English. They didn't even manage to publish the right version of some chapters. When they made the code from the book available, they did so as a word document containing code snippets from the book (instead of the runnable tarballs from each example and chapter that I provided for them).
Basically, your name goes on the book, so any fuck-up will be attributed to you. If something goes wrong, like things did with my book, you will get hate mail every day, criticizing you for things that you had no control over.
Oh yeah, and when the code you wrote the book about goes out of date, people will send you hate mail for that too. It is also apparently a hobby of people to nit-pick things like your variable names ("OMG you used $c instead of $ctx, YOU ARE SO DUMB!"). I'm not sure what motivates people to send me e-mail about things like this. Do they I think I care what they think? Do they want me to magically change every copy of the book? I don't get it.
In case you haven't noticed, the experience has changed my life. Before writing the book, I was really excited about open source projects and talking to people. I genuinely wanted to help people learn to program better. Now I am bitter and misanthropic because of the constant influx of hate mail. (I could go on and on about this, how people don't want to learn, how teaching is a waste of time, etc., etc... but I will save that for another day.)
I'm not trying to talk you out of writing a book, of course, but just make sure that your publisher cares about your book. If they don't, you may regret writing it.