Chromakey Pancake

Synchronizing Development Environments

Posted on — Jun 23, 2019

The Problem

Having recently decided to install Ubuntu on my home computer, I quickly ran into the tedium of having to set up a new development environment exactly to my liking.

Since I am relatively new to the world of programming, there isn’t too much in the way of customisation that I need to worry about, but I have grown used to the functionality of the wonderful oh-my-zsh and certain plugins that do not come packaged with it by default. Configuration files for applications such as Git & Vim are also handy to quickly recreate when working on a new system. The problem becomes incredibly apparent if you like to work on multiple projects in their own virtual machines, as I do.

The Solution

After a bit of research I decided to host my configuration dotfiles in a git repository and include a small installation script inside to link everything to its right place when cloning to a new machine.

Setting Up The Repository

Create a directory where your configuration files will be stored. I created a directory called .dotfiles that lives in my home directory.

mkdir ~/.dotfiles

Initialise a git repository in this directory and copy all the configuration files you’d like to carry over to new environments.

cd ~/.dotfiles
git init

This is what my .dotfiles repository currently consists of:

.dotfiles
├── .gitconfig
├── .gitignore_global
├── .newinstall.sh
├── .vim
│   └── colors
│       └── solarized.vim
├── .vimrc
└── .zshrc

You’ll notice that I’ve included the .vim/colors directory with the solarized theme for Vim inside. My Vim configuration file .vimrc is set up to use this and will look for this file when copied to a new system. You can do this with any file dependencies you may have.

The Install Script

Once all your files are copied over, it’s time to set up the install script. My .dotfiles directory contains a file called newinstall.sh. This script automatically installs any applications I commonly use on my development environments and creates symbolic links to all my configuration files in the current user’s home directory. We’ll look at why we use symbolic links shortly.

This is what the .newinstall.sh script looks like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash

# Install packages
sudo apt-get install -y ack zsh vim fonts-powerline

# Link dotfiles to home directory
DIR=$HOME/.dotfiles

DOTFILES=(
	".gitconfig"
	".gitignore_global"
	".zshrc"
	".vimrc"
	".vim/colors/solarized.vim"
)

mkdir -p ~/.vim/colors

for dotfile in "${DOTFILES[@]}";do
	rm -rf "${HOME}/${dotfile}"
	ln -sf "${DIR}/${dotfile}" "${HOME}/${dotfile}"
done

# Install VimCompletesMe
git clone git://github.com/ajh17/VimCompletesMe.git ~/.vim/pack/vendor/start/VimCompletesMe

# Install oh-my-zsh
rm -rf ~/.oh-my-zsh
git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh

# Install zsh syntax highlighting
git clone --recursive https://github.com/zsh-users/zsh-syntax-highlighting.git ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting

# Install solarized directory colours for zsh
git clone --recursive git://github.com/joel-porquet/zsh-dircolors-solarized ~/.oh-my-zsh/custom/plugins/zsh-dircolors-solarized

Line 4 of the script just installs packages that I use often on all my machines.

Lines 7-22 are the most important part of the script. This is how we link all the files in our repository to our current user’s home directory. You’ll need to change the contents of the DOTFILES array to match the contents of your .dotfiles directory.

Lines 19-22 contain the loop which goes through this list of files and automatically creates a symbolic link for each one in the user’s home directory (removing the file if it already exists first). As the directory .vim.colors may not already exist, I create this before the loop to prevent any issues.

Finally there’s a few lines installing some plugins I like to use with Vim & oh-my-zsh.

Putting the solution in practice

Once your install script and configuration files are set up, all that’s left is to host your git repository somewhere (I use GitHub, and you can see my .dotfiles repository here). You’re now ready to install your development environment wherever you go!

On a new machine, all you need to do is clone the .dotfiles repository to your home directory…

git clone git clone yourdotfilesrepo.git ~/.dotfiles

… and run the install script (after making it executable).

sudo chmod +x ~/.dotfiles/.newinstall.sh
~/.dotfiles/.newinstall.sh

Once the script is finished you should be good to go.

TIP: It’s a good idea to restart your terminal if any of your config files affect your shell.

Suppose you make a tweak to one of your configuration files that you just can’t live without. All you have to do to push this to your other machines is go to the ~/.dotfiles directory, commit the changes you’ve made and push them to your hosting server of choice.

Once you log on to another system, all you need to do is a git pull in the .dotfiles directory and all the files in your home directory will also be updated automatically as they are just pointing to the files in ~/.dotfiles.

Conclusion

There are a number of different ways to synchronize your development environments, but this is the one that worked for me. I particularly enjoyed using this method as it helped me learn some of the basics of navigating a UNIX environment, using git and setting up bash scripts. Feel free to chip in if you have any suggestions on improvements, or any questions at all.

comments powered by Disqus