DEV Community

Jack Lin
Jack Lin

Posted on


Writing a bash script to automatically setup Ubuntu 22.04

It is annoying to setup a newly installed Ubuntu machine, so I wrote a shell script to install my favorite softwares. You can replace the statements with your favorite commands. 🥰

The script automatically installs google-chrome, vscode, openssh-server, docker, autojump. If any target command exists, it will skip installing it like this:
Image description

MyUser=lin            # Replace this with your username

echo "${Green}Change working directory to /home/${MyUser}/Downloads.${Reset}"
cd /home/${MyUser}/Downloads

if ! [ -x "$(command -v google-chrome)" ]; then
    echo "${Green}Install Google Chrome.${Reset}"
    dpkg -i google-chrome-stable_current_amd64.deb
    echo "${Green}Google Chrome is already installed.${Reset}"

if ! [ -x "$(command -v code)" ]; then
    echo "${Green}Install VS Code.${Reset}"
    wget "" --output-document vscode.deb
    dpkg -i vscode.deb
    echo "${Green}VS Code is already installed.${Reset}"

if ! [ -x "$(command -v sshd)" ]; then
    echo "${Green}Install openssh-server.${Reset}"
    apt install -y openssh-server
    systemctl enable ssh
    systemctl start ssh
    echo "${Green}Run ssh-keygen as your user after installation.${Reset}"
    echo "${Green}openssh-server is already installed.${Reset}"

if ! [ -x "$(command -v docker)" ]; then
    echo "${Green}Install Docker.${Reset}"
    apt install -y ca-certificates curl gnupg lsb-release
    mkdir -p /etc/apt/keyrings
    curl -fsSL | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
    chmod a+r /etc/apt/keyrings/docker.gpg
    apt update
    apt install -y docker-ce docker-ce-cli docker-compose-plugin
    groupadd docker
    usermod -aG docker $MyUser
    systemctl enable docker
    systemctl start docker
    echo "${Green}You might logout or restart for docker group to take effect.${Reset}"
    echo "${Green}Docker is already installed.${Reset}"

if ! [ -x "$(command -v autojump)" ]; then
    echo "${Green}Install autojump.${Reset}"
    apt install autojump
    echo "# Setup autojump" >> /home/$MyUser/.bashrc
    echo "[[ -s /usr/share/autojump/ ]] && source /usr/share/autojump/" >> /home/$MyUser/.bashrc
    echo "${Green}Autojump is already installed.${Reset}"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git