Skip to content

Environment

Chapter 2: Environment Setup on Arch Linux

Section titled “Chapter 2: Environment Setup on Arch Linux”

This chapter covers setting up your Arch Linux environment for optimal bash scripting. We’ll configure the shell, install essential tools, and set up a productive development environment. This is specifically tailored for DevOps, SRE, and SysAdmin workflows.


Arch Linux uses pacman as its package manager. Understanding pacman is crucial for any Arch Linux system administrator.

Terminal window
# Synchronize database and update system
sudo pacman -Syu
# Install a single package
sudo pacman -S package_name
# Install multiple packages
sudo pacman -S package1 package2 package3
# Search for packages
pacman -Ss search_term
pacman -Qs search_term # Search installed
# Remove package only
sudo pacman -R package_name
# Remove package and its dependencies
sudo pacman -Rs package_name
# Remove packages that were installed as dependencies but no longer needed
sudo pacman -Rsn $(pacman -Qdtq)
# List packages
pacman -Q # All installed
pacman -Qi package_name # Package info
pacman -Ql package_name # List files
# Clean cache
sudo pacman -Scc # Clean all cache
Terminal window
# Core utilities - absolutely essential
sudo pacman -S \
bash \
bash-completion \
coreutils \
findutils \
gawk \
sed \
grep \
util-linux \
procps-ng \
which \
tree \
htop \
tmux \
vim \
nano \
git \
curl \
wget \
rsync \
cron \
logrotate \
strace \
lsof \
net-tools \
dnsutils \
inetutils \
openssh

Bash completion is essential for productivity, especially when working with complex commands like kubectl, docker, and terraform.

Terminal window
# Install bash-completion
sudo pacman -S bash-completion

Add to your ~/.bashrc:

Terminal window
# Enable bash completion
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
# Additional completions for common DevOps tools
# kubectl completion
if command -v kubectl &> /dev/null; then
source <(kubectl completion bash)
fi
# docker completion
if command -v docker &> /dev/null; then
source /usr/share/bash-completion/completions/docker
fi
# terraform completion
if command -v terraform &> /dev/null; then
complete -C terraform terraform
fi
Terminal window
# Example: Add completion for custom scripts
_complete_myapp() {
local cur prev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
case ${COMP_CWORD} in
1)
COMPREPLY=($(compgen -W "start stop restart status logs" -- ${cur}))
;;
2)
COMPREPLY=($(compgen -W "dev staging production" -- ${cur}))
;;
esac
return 0
}
complete -F _complete_myapp myapp

Your ~/.bashrc is executed for interactive non-login shells. This is where you configure your daily working environment.

~/.bashrc
#===============================================================================
# HISTORICAL CONFIGURATION
#===============================================================================
# Don't put duplicate lines in history
HISTCONTROL=ignoredups:ignorespace
# Append to history file (don't overwrite)
shopt -s histappend
# History size - important for DevOps work
HISTSIZE=10000
HISTFILESIZE=20000
# Check window size after each command
shopt -s checkwinsize
# Enable color support
export CLICOLOR=1
#==============================================================================
# PROMPT CONFIGURATION
#==============================================================================
# Color prompt with Git branch
parse_git_branch() {
git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/ (\1)/'
}
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
#==============================================================================
# ALIASES - Essential for DevOps
#==============================================================================
# Safety aliases
alias rm='rm -iv'
alias cp='cp -iv'
alias mv='mv -iv'
# List aliases
alias ll='ls -lah'
alias la='ls -A'
alias l='ls -CF'
alias lr='ls -lahR' # Recursive list
# Grep with color
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
# DevOps-specific aliases
alias k='kubectl'
alias kgp='kubectl get pods'
alias kgs='kubectl get services'
alias kgd='kubectl get deployments'
alias kga='kubectl get all'
alias klf='kubectl logs -f'
alias kex='kubectl exec -it'
alias d='docker'
alias dps='docker ps'
alias dpsa='docker ps -a'
alias di='docker images'
alias dex='docker exec -it'
alias dlogs='docker logs -f'
alias tf='terraform'
alias tfp='terraform plan'
alias tfa='terraform apply'
alias tfd='terraform destroy'
# System aliases
alias ports='netstat -tulanp'
alias meminfo='free -m -l -t'
alias diskspace='df -h'
alias folders='du -h --max-depth=1'
#==============================================================================
# FUNCTIONS - Reusable for DevOps
#==============================================================================
# Create directory and cd into it
mkcd() {
mkdir -p "$1" && cd "$1"
}
# Extract common archive formats
extract() {
if [ -f "$1" ]; then
case "$1" in
*.tar.bz2) tar xjf "$1" ;;
*.tar.gz) tar xzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xf "$1" ;;
*.tbz2) tar xjf "$1" ;;
*.tgz) tar xzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Quick kubectl context switch
kctx() {
if [ -z "$1" ]; then
kubectl config get-contexts
else
kubectl config use-context "$1"
fi
}
# Docker cleanup
dclean() {
echo "Stopping all containers..."
docker stop $(docker ps -aq) 2>/dev/null || true
echo "Removing all containers..."
docker rm $(docker ps -aq) 2>/dev/null || true
echo "Removing dangling images..."
docker image prune -f
echo "Cleanup complete!"
}
#==============================================================================
# ENVIRONMENT VARIABLES
#==============================================================================
# Editor
export EDITOR=vim
export VISUAL=vim
export PAGER=less
# Less colors
export LESS='-R'
# Language
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
# PATH additions
export PATH="$HOME/bin:$HOME/.local/bin:$PATH"
export PATH="$HOME/go/bin:$PATH"
# Add Cargo (Rust) if installed
[ -d "$HOME/.cargo/bin" ] && export PATH="$HOME/.cargo/bin:$PATH"
# Add Flutter if installed
[ -d "$HOME/flutter/bin" ] && export PATH="$HOME/flutter/bin:$PATH"
# Kubernetes
export KUBECONFIG="$HOME/.kube/config"
# Docker
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1

For login shells, use ~/.bash_profile:

~/.bash_profile
# Source .bashrc if it exists
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

For broader compatibility:

~/.profile
# Set PATH
export PATH="$HOME/bin:$HOME/.local/bin:$PATH"
# Load .bashrc for interactive shells
if [ -n "$BASH_VERSION" ]; then
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi

Terminal window
# Enable extended globbing - essential for advanced filename matching
shopt -s extglob
# Case-insensitive globbing
shopt -s nocaseglob
# Null glob expansion - empty if no match
shopt -s nullglob
# Fail on glob expansion errors
shopt -s failglob
# Append to history
shopt -s histappend
# Check window size
shopt -s checkwinsize
# Check current options
shopt
# Check specific option
shopt -q extglob && echo "Enabled" || echo "Disabled"
Terminal window
# Common set options
set -e # Exit on error
set -u # Exit on undefined variable
set -o pipefail # Pipeline fails on first error
set -x # Debug mode (trace)
set -v # Print input lines

Terminal window
# Install vim
sudo pacman -S vim
# Create ~/.vimrc with DevOps-friendly settings
cat > ~/.vimrc << 'EOF'
"===============================================================================
" VIM CONFIGURATION FOR DEVOPS
"===============================================================================
" Basic settings
set number
set relativenumber
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set smartindent
set wrap
set linebreak
" Search settings
set incsearch
set hlsearch
set ignorecase
set smartcase
" Visual settings
syntax on
colorscheme default
" Key mappings
let mapleader = " "
" Enable mouse support
set mouse=a
" Show line numbers
set number
" Highlight current line
set cursorline
" Better backup settings
set nobackup
set nowritebackup
set noswapfile
" Enable persistent undo
set undofile
set undodir=~/.vim/undodir
" Custom mappings
nnoremap <leader>w :w<CR>
nnoremap <leader>q :q<CR>
nnoremap <leader>h :nohlsearch<CR>
" DevOps-specific
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab
autocmd FileType json setlocal ts=2 sts=2 sw=2 expandtab
autocmd FileType sh setlocal ts=4 sts=4 sw=4 expandtab
autocmd FileType dockerfile setlocal ts=4 sts=4 sw=4 expandtab
EOF
# Create undodir
mkdir -p ~/.vim/undodir

Terminal window
# Install git
sudo pacman -S git
# Configure git
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global core.editor vim
git config --global init.defaultBranch main
# Useful git aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.last "log -1 HEAD"
git config --global alias.unstage "reset HEAD --"
# Color settings
git config --global color.ui auto
# Pull strategy
git config --global pull.rebase false
# Default push
git config --global push.default current

Terminal window
# Install tmux
sudo pacman -S tmux
# Create ~/.tmux.conf
cat > ~/.tmux.conf << 'EOF'
#===============================================================================
# TMUX CONFIGURATION
#===============================================================================
# Prefix key - use Ctrl+a instead of Ctrl+b
set -g prefix C-a
unbind C-b
# Allow nested tmux sessions
bind-key C-a send-prefix
# Start window numbering at 1
set -g base-index 1
set -g pane-base-index 1
# Status bar
set -g status-interval 5
set -g status-left-length 30
set -g status-right-length 50
set -g status-bg black
set -g status-fg white
set -g status-left "#[fg=green]#S #[fg=blue]• #[fg=white]"
set -g status-right "#[fg=white]%Y-%m-%d %H:%M #[fg=blue]• #[fg=white]"
# Window status
setw -g window-status-format " #I: #W "
setw -g window-status-current-format " #I: #W "
setw -g window-status-current-style fg=green,bg=black
# Mouse support
set -g mouse on
# Better pane splitting
bind-key v split-window -h -c "#{pane_current_path}"
bind-key s split-window -v -c "#{pane_current_path}"
# Pane navigation
bind-key h select-pane -L
bind-key j select-pane -D
bind-key k select-pane -U
bind-key l select-pane -R
# Resize panes
bind-key -r H resize-pane -L 5
bind-key -r J resize-pane -D 5
bind-key -r K resize-pane -U 5
bind-key -r L resize-pane -R 5
# Copy mode
setw -g mode-keys vi
bind-key -T copy-mode-vi v send -X begin-selection
bind-key -T copy-mode-vi y send -X copy-selection-and-cancel
# Reload config
bind-key r source-file ~/.tmux.conf \; display "Config reloaded!"
EOF

Terminal window
# Install base-devel (required for building from AUR)
sudo pacman -S --needed base-devel git
# Clone and install yay
git clone https://aur.archlinux.org/yay.git
cd yay
makepkg -si
Terminal window
# Search for packages
yay -Ss package_name
# Install packages
yay -S package_name
# System upgrade (including AUR)
yay -Syu
# Remove package and its dependencies
yay -Rsn package_name
Terminal window
# Install common DevOps tools from AUR
yay -S \
terraform \
ansible \
packer \
kubectl \
helm \
docker-compose \
k9s \
flux-bin \
argocd

Terminal window
# Docker
sudo pacman -S docker docker-compose
sudo systemctl enable docker
sudo systemctl start docker
sudo usermod -aG docker $USER
# Kubernetes tools
sudo pacman -S kubectl
# Terraform
yay -S terraform
# Ansible
sudo pacman -S ansible
# Python and pip
sudo pacman -S python python-pip
# Node.js and npm
sudo pacman -S nodejs npm
# Go
sudo pacman -S go
# AWS CLI
pip install awscli --break-system-packages
# GCP CLI
yay -S google-cloud-sdk
# Azure CLI
yay -S azure-cli

~/bash-scripts/
├── bin/ # Executable scripts
│ ├── deploy.sh
│ ├── backup.sh
│ └── monitor.sh
├── lib/ # Shared libraries
│ ├── common.sh
│ ├── utils.sh
│ └── config.sh
├── config/ # Configuration files
│ ├── app.conf
│ └── .env
├── logs/ # Log files
│ └── .gitkeep
├── tests/ # Test scripts
│ └── test_utils.sh
├── docs/ # Documentation
├── README.md # Project README
├── Makefile # Build automation
└── .gitignore # Git ignore rules
Terminal window
mkdir -p ~/bash-scripts/{bin,lib,config,logs,tests,docs}
touch ~/bash-scripts/logs/.gitkeep

Terminal window
# Add to ~/.bashrc
# History configuration
export HISTSIZE=10000
export HISTFILESIZE=20000
export HISTCONTROL=ignoredups:ignorespace
# Language
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
# Editor
export EDITOR=vim
export VISUAL=vim
export PAGER=less
# PATH additions
export PATH="$HOME/bin:$HOME/.local/bin:$PATH"
export PATH="$HOME/go/bin:$PATH"
# Kubernetes
export KUBECONFIG="$HOME/.kube/config"
# Docker
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
# Go
export GOPATH="$HOME/go"
export PATH="$PATH:$GOPATH/bin"
# AWS
export AWS_DEFAULT_REGION=us-east-1

┌─────────────────────────────────────────────────────────────────────┐
│ ARCH LINUX QUICK REFERENCE │
└─────────────────────────────────────────────────────────────────────┘
┌──────────────────┬─────────────────────────────────────────────────┐
│ Command │ Description │
├──────────────────┼─────────────────────────────────────────────────┤
│ pacman -Syu │ Full system upgrade │
│ pacman -S <pkg> │ Install package │
│ pacman -R <pkg> │ Remove package │
│ pacman -Qs <pkg> │ Search installed packages │
│ pacman -Qdt │ List orphans (unused dependencies) │
│ pacman -Qe │ List explicitly installed packages │
│ pacman -Qi <pkg> │ Package information │
│ pacman -Ql <pkg> │ List package files │
│ pacman -Fs <file>│ Find which package provides file │
│ yay -S <pkg> │ Install from AUR │
│ yay -Syu │ Full system upgrade (including AUR) │
│ systemctl │ Manage systemd services │
│ journalctl │ View system logs │
│ hostnamectl │ System information │
│ timedatectl │ Time and date settings │
│ localectl │ Locale settings │
│ loginctl │ User session management │
└──────────────────┴─────────────────────────────────────────────────┘

Terminal window
# Check bash version - should be 5.x on Arch Linux
bash --version
# Verify bash features
echo $BASH_VERSION
Terminal window
# Source bashrc
source ~/.bashrc
# Test completion
sudo pacman -S<tab><tab>
kubectl<tab><tab>
docker<tab><tab>
# Create test script
cat > /tmp/test.sh << 'EOF'
#!/bin/bash
# Test script for environment verification
set -euo pipefail
echo "==============================================="
echo "Bash Environment Test"
echo "==============================================="
echo "Bash version: $BASH_VERSION"
echo "Working directory: $(pwd)"
echo "Home directory: $HOME"
echo "User: $USER"
echo "Hostname: $HOSTNAME"
echo "==============================================="
# Test essential commands
echo -n "Testing essential commands: "
commands=("grep" "awk" "sed" "find" "curl" "wget" "git")
all_ok=true
for cmd in "${commands[@]}"; do
if command -v "$cmd" &> /dev/null; then
echo -n ""
else
echo -n "✗ ($cmd missing) "
all_ok=false
fi
done
echo
if [ "$all_ok" = true ]; then
echo "All essential commands available!"
exit 0
else
echo "Some commands are missing. Install them with: sudo pacman -S ..."
exit 1
fi
EOF
chmod +x /tmp/test.sh
/tmp/test.sh

Your Arch Linux environment is now configured for bash scripting:

  • ✅ Pacman and AUR configured
  • ✅ Bash completion enabled
  • ✅ Custom prompt with Git support
  • ✅ Essential DevOps tools installed
  • ✅ Text editor configured
  • ✅ Git configured
  • ✅ Tmux configured
  • ✅ Project structure created

  1. Configure your bash environment with aliases for kubectl and docker
  2. Install additional tools you need for your daily work
  3. Create your project directory structure
  1. Install and configure tmux with custom settings
  2. Set up vim with YAML and Python support
  3. Configure kubectl completion
  1. Create a script that auto-installs your preferred environment
  2. Set up a custom prompt with Kubernetes context display
  3. Configure dotfiles repository for your configuration

In the next chapter, we’ll write your first bash script and learn the basic syntax.


Previous Chapter: Introduction to Bash Next Chapter: Basic Syntax and First Script