Skip to content

Instantly share code, notes, and snippets.

@ned2
Last active July 13, 2021 14:47
Show Gist options
  • Save ned2/ea093619e2b9d93ccae6ac5ae3d424d4 to your computer and use it in GitHub Desktop.
Save ned2/ea093619e2b9d93ccae6ac5ae3d424d4 to your computer and use it in GitHub Desktop.
A Bash script for initialising a clean Ubuntu installation and user account with a pyenv-based workflow.
#!/usr/bin/env bash
# Author: Ned Letcher
#
# This bash script is designed to configure a clean Ubuntu installation (eg new
# EC2 instance) with a somewhat opionated set of packages and tools for working
# on Python projects. In addition to installing a range of packages for dev
# work, it installs pyenv for the current user, overwriting the current .profile
# and modifying the existing .bashrc to make sure pyenv is configured correctly.
#
# Usage:
# bash ubuntu-ec2-init.sh
# make sure system is updated
sudo apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get -yq \
-o Dpkg::Options::="--force-confdef" \
-o Dpkg::Options::="--force-confnew" \
dist-upgrade
# install pyenv/python deps
sudo DEBIAN_FRONTEND=noninteractive apt-get install \
-y -q --no-install-recommends \
make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libreadline-dev \
libsqlite3-dev wget curl llvm xz-utils tk-dev libxml2-dev \
libxmlsec1-dev libffi-dev liblzma-dev
# install dev tools
sudo DEBIAN_FRONTEND=noninteractive apt-get install \
-y -q --no-install-recommends \
git docker.io npm htop byobu emacs vim direnv awscli jq
# install data deps
sudo DEBIAN_FRONTEND=noninteractive apt-get install \
-y -q --no-install-recommends \
libsnappy-dev libsqlite3-dev
# get scripts for user's $HOME/bin
mkdir -p $HOME/bin
curl -L https://github.com/git/git/raw/master/contrib/completion/git-prompt.sh > $HOME/bin/git-prompt.sh
# install pyenv
if [ ! -d "$HOME/.pyenv" ] ; then
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
fi
# create a new .profile that initialises pyenv *before* sourcing .bashrc
cat > $HOME/.profile <<'EOF'
if [ -d ${HOME}/.pyenv/bin ]; then
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
fi
if [ -n "$BASH_VERSION" ]; then
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi
EOF
# Append needed pyenv initialisation to current .bashrc
cat >> $HOME/.bashrc <<'EOF'
if [ -x "$(command -v pyenv)" ]; then
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
fi
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment