DEV Community

Cover image for Automate Your Initial OS Setup
Preslav Mihaylov
Preslav Mihaylov

Posted on • Edited on • Originally published at pmihaylov.com

Automate Your Initial OS Setup

There was a time in my life when a huge part of my time was spent reinstalling my Linux OS. Wonder why?

Well, the first time you install Linux, they warn you to never run rm- rf / as this would delete your entire system. Fair enough, that's simple to follow.

What they don't tell you is that you have another million ways to effectively do the same thing with commands which seem harmless at first glance.

However, there were some benefit form my misfortunes.

What irritated me the most was installing all the software I use from scratch every single time. I often forgot to install one or two programs I use and had to do it on the fly once I actually needed them which was very disturbing.

Hence, I came up with the idea to create a script which would automate this process via a single command. Every time I reinstall my OS, I simply run the script, go get myself a coffee and once I'm back, I have my OS all setup with what I need.

If you're in a situation where you often have to do this yourself, read on.

Also, be aware that this guide is specific to installing Linux and Mac OS. You could probably apply the same concept in Windows, but I only speak bash, not bat.

Bash to the rescue

Now, there are tools out there specifically designed to achieve this. For example, you have Puppet.

Additionally, you could create docker images which have your setup bundled in an image. You could also use Arch Linux, which has a package manager enabling you to automate this more reliably, so I've been told.

But, I'm a simple man. I like simple tools. Hence, I've encapsulated this entire convoluted process in a single bash script.

You can find my entire OS setup script here. Nowadays, I have two different versions - one for Ubuntu and one for Mac.

But, to get started yourself, here's what you have to do:

  1. Reinstall your OS
  2. Create a new empty script & add it to some kind of a repo (e.g. in Github)
  3. Every time you install a new program or enable some setting, add a line for that in the script
  4. Over time, when your needs or setup changes, keep your script up to date

That's really the gist of it.

The important thing is to keep your script up to date, otherwise, you might abandon it altogether and stick to the old cumbersome ways of installing stuff manually.

What follows is a set of tips & tricks to have in mind

Automate your settings as well

Apart from enumerating all the software you need in sudo apt-get install lines, you can also add any additional settings not related to software install that you might need.

For example, I need Bulgarian Phonetic installed on my Ubuntu, which is not there by default, hence I've figured out how to do it from the terminal and added it to my script.

I no longer have to google "add keyboard mapping ubuntu" every time I reinstall the OS.

The good part is that most of these settings, especially in Linux, are totally scriptable. They often have GUI ways to do it (e.g. Go to Settings -> Keyboard -> ..., etc) but I always search for the terminal one-liners in order to add it to the script.

When the settings aren't scriptable...

There might be times when there isn't an alternative to setting something up via the GUI. Or at least, Google isn't helpful enough to show you how to do it in sensible time.

For these cases, I still suggest to "automate" them, but not via a script, but via written instructions in your README.md which you have to manually follow.

This is a bit cumbersome, but still better than trying to rediscover how to setup something every time you reinstall the OS.

Here's an example from my Mac setup:

I've found this to be more common for Mac, since most answers in stackoverflow regarding how to setup something are via a GUI interface, which sucks.

I'm pretty sure there is a way to achieve all these settings via the terminal, but I've decided not to invest too much time in finding them out. However, if you happen to know how to do it, then feel free to open a pull request, it would be much appreciated. :)

Store a repo of common configurations

In addition to automating the settings and software you need, if you rely on software like vim, tmux or anything else which has some convoluted configuration, I suggest creating a separate repo for storing your configurations.

Getting it "installed" can be part of your OS setup script. This is exactly what I've done with my dotfiles repo.

In it, I keep my latest vim configuration, along with some auxiliary configs for tmux and various plugins.

Due to this, after I run my setup script, not only do I have all my necessary software installed, but I also have my vim setup with my favorite plugins and configuration the way I like it.

Conclusion

Tired of installing the same software all the time? Tired of setting up vim every time you get a new laptop or reinstall your OS?

Well, now you have your solution - automate the process.

Here's how in a nutshell:

  1. Create a script
  2. Add your installations + settings + common configurations
  3. Repeat step 2 infinitely

Once you do this, you will escape the insidious process of setting up everything from scratch all the time.

Top comments (3)

Collapse
 
kayis profile image
K

NixOS NixOS NixOS!!! :D

Collapse
 
pmihaylov profile image
Preslav Mihaylov

Ahh, I think that was indeed the OS/package manager I referred to, but haven't tried.

A colleague of mine is praising it all the time and pushing me to try it out. I think I will yield soon. :)

Collapse
 
hellovietduc profile image
Duc Nguyen

I found this interactive installation script, you can take a look!