Skip to content

Instantly share code, notes, and snippets.

@danielalvarenga
Last active April 8, 2024 09:13
Show Gist options
  • Save danielalvarenga/29829f997915a00d8f7dda51b8c33df0 to your computer and use it in GitHub Desktop.
Save danielalvarenga/29829f997915a00d8f7dda51b8c33df0 to your computer and use it in GitHub Desktop.
Script to consigure new ubuntu instalation
#!/bin/bash
# Author: Daniel Alvarenga Lima
# Ubuntu version supported: 20.04 LTS amd64
# EXECUTE
# sudo chmod +x script-configure-ubuntu18.94-amd64.sh
# ./script-configure-ubuntu20.04-amd64.sh
#INSTRUCTIONS
# Set 'true' in items that you want to install
# Required only in the first time to execute this script
UPDATE_REPOSITORIES=true
UPGRADE_PACKAGES=true
# build-essential, git, curl
REQUIRED=true
# Add different colors and format terminal label
# Execute only once as true. If you need customize, see: https://gist.github.com/danielalvarenga/2df8cabbd6f3041c2378
# Example with host: user@host:~/current_folder_path(branch)
# Example without host (short): user:~/current_folder_path(branch)
COLORIZE_TERMINAL=false
# recommend to use false
WITH_HOST=false
# Configure GIT
CONFIGURE_GITHUB=false
GITHUB_NAME="My commit name"
GITHUB_MAIL="email_in_github@any.com"
GITHUB_USER="github_username"
# Docker
DOCKER=false
DOCKER_COMPOSE=false
DOCKER_COMPOSE_VERSION=1.27.4
# Java oracle
# Get JDK_URL from version that you want in: https://www.oracle.com/java/technologies/javase-downloads.html
ORACLE_JAVA_SE=false
JDK_URL=https://download.oracle.com/otn-pub/java/jdk/15+36/779bf45e88a44cbd9ea6621d33e33db1/jdk-15_linux-x64_bin.deb
JDK_FILE=jdk-15_linux-x64_bin.deb
JDK_PATH_INSTALLATION=/usr/lib/jvm/jdk-15
# Golang
GOLANG=false
GO_VERSION=1.15.2
# Install Node, npm
# https://github.com/nodesource/distributions/blob/master/README.md#debinstall
NODEJS=false
NODEJS_VERSION=lts # use "lts", "current" or the number of version
YARN=false
# Ruby
RUBY=false
RUBY_VERSION=2.7.1
RUBY_VERSION_PATH=2.7
RUBY_VERSION_MANAGER=RBENV # Use RVM or RBENV or false to source instalation
RAILS=false
RAILS_VERSION=6.0.3.3
# Terminal text editor
VIM=false
# Visual studio code ide
VISUAL_STUDIO_CODE=false
# For files compression
COMPRESS_7ZIP=false
# Image editor
GIMP=false
# Player and codecs
VLC=false
# qBitTorrent
TORRENT=false
# Online music player
SPOTIFY=false
# Terminal
TERMINATOR=false
# Partitions manager
GPARTED=false
# Rest api simulations
POSTMAN=false
# Messenger
SKYPE=false
# Google chrome web browser
GOOGLE_CHROME=false
# Database sqlite
SQLITE=false
# Sqlite database manager
SQLITE_BROWSER=false
# Database postgresql
POSTGRESQL=false
# Postgresql database manager
PGADMIN=false
# Database redis
REDIS=false
# Redis database manager
REDIS_DESKTOP_MANAGER=false
# Database mysql
MYSQL=false
MYSQL_USER=root
MYSQL_PASSWORD=root
# Database manager mysql
WORKBENCH=false
# Manage extra options for mouse, keyboard and other resources of ubuntu
TWEAKS=false
# Paths
LOG_SCRIPT=./log_script.txt
GITCONFIG=/etc/gitconfig
SSH_FOLDER=~/.ssh
SSHCONFIG="$SSH_FOLDER/config"
DOWNLOADS=~/Downloads
BASHRC=~/.bashrc
RBENV_DIR=~/.rbenv
RUBY_BUILD_DIR="$RBENV_DIR/plugins/ruby-build"
function execsudo {
sudo "$@" >> $LOG_SCRIPT 2>> $LOG_SCRIPT 3>> $LOG_SCRIPT
}
function exececho {
echo "$@" >> $LOG_SCRIPT 2>> $LOG_SCRIPT 3>> $LOG_SCRIPT
}
function exececho2 {
echo "$@"
exececho "$@"
}
function dpkginstall {
exececho "*** Installing package $1"
shift
execsudo dpkg -i "$@" >> $LOG_SCRIPT 2>> $LOG_SCRIPT 3>> $LOG_SCRIPT
}
function aptinstall {
exececho "*** Installing $1"
shift
execsudo apt-get -y -f install "$@"
}
function aptremove {
exececho "*** Removing $1"
shift
execsudo apt-get -y remove "$@"
}
function snapinstall {
exececho "*** Installing $1"
shift
execsudo snap install "$@"
}
function deletefile {
exececho "*** Removing file $@"
if [ -f "$@" ]; then execsudo rm "$@"; fi;
}
function deletedir {
exececho "*** Removing folder $@"
if [ -d "$@" ]; then execsudo rm -rf "$@"; fi;
}
function cleanpackages {
exececho "*** Fix and clean packages"
execsudo apt-get -y --fix-broken install
execsudo apt-get -y autoremove
execsudo apt-get clean
}
deletefile $LOG_SCRIPT
touch $LOG_SCRIPT
exececho "***"
exececho "*** Execution logs in '$(pwd)/$LOG_SCRIPT'"
exececho "*** Execute 'tail -F $(pwd)/$LOG_SCRIPT' in another terminal to see logs in real time"
exececho "***"
if $UPDATE_REPOSITORIES
then
exececho "*** Update Respositories"
sudo apt-get update
fi
if $UPGRADE_PACKAGES
then
exececho "*** Upgrade Packages"
execsudo apt-get -y upgrade
fi
if $REQUIRED
then
aptinstall "Build Essencial" build-essential
aptinstall Git git-core
aptinstall CURL curl
aptinstall Wget wget
fi
if $DOCKER
then
aptremove "Existent docker versions" docker docker-engine docker.io containerd runc
aptinstall "Docker dependencies" apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | execsudo apt-key add -
execsudo apt-key fingerprint 0EBFCD88
execsudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
execsudo apt-get update
aptinstall "Docker" docker-ce docker-ce-cli containerd.io
if ! $(grep -q coisa /etc/group); then execsudo groupadd docker; fi;
execsudo usermod -aG docker $USER
docker --version
fi
if $DOCKER_COMPOSE
then
DOCKER_COMPOSE_RUNNER=/usr/local/bin/docker-compose
DOCKER_COMPOSE_LINK=/usr/bin/docker-compose
exececho "*** Removing existent docker-compose versions"
deletefile $DOCKER_COMPOSE_RUNNER
deletefile $DOCKER_COMPOSE_LINK
exececho "*** Installing docker-compose"
execsudo curl -L "https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_VERSION/docker-compose-$(uname -s)-$(uname -m)" -o $DOCKER_COMPOSE_RUNNER
execsudo chmod +x $DOCKER_COMPOSE_RUNNER
execsudo ln -s $DOCKER_COMPOSE_RUNNER $DOCKER_COMPOSE_LINK
docker-compose --version
fi
if $ORACLE_JAVA_SE
then
aptremove "OpenJDK" --purge openjdk-*
exececho "*** Installing Oracle Java"
JDK_DOWNLOADED_FILE=$DOWNLOADS/$JDK_FILE
if [ -f $JDK_DOWNLOADED_FILE ]
then
exececho "--- $JDK_FILE already exists"
else
cd $DOWNLOADS
wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" $JDK_URL -O $JDK_DOWNLOADED_FILE
fi
dpkginstall "Java Oracle" $JDK_DOWNLOADED_FILE
exececho "*** Setting java version"
execsudo update-alternatives --install /usr/bin/java java $JDK_PATH_INSTALLATION/bin/java 1
execsudo update-alternatives --install /usr/bin/javac javac $JDK_PATH_INSTALLATION/bin/javac 1
execsudo update-alternatives --config java
execsudo update-alternatives --config javac
java -version
fi
if $GOLANG
then
exececho "*** installing GOlang"
GO_FILE=$DOWNLOADS/go$GO_VERSION.linux-amd64.tar.gz
if [ -f $GO_FILE ]
then
exececho "--- $GO_FILE already exists"
else
cd $DOWNLOADS
wget https://dl.google.com/go/go$GO_VERSION.linux-amd64.tar.gz
fi
execsudo tar -C /usr/local -xzf $GO_FILE
echo '' >> $HOME/.profile
echo '# Add GO lang path environment variable' >> $HOME/.profile
echo 'export PATH=$PATH:/usr/local/go/bin' >> $HOME/.profile
mkdir -p $HOME/go/src
fi
if ( $NODEJS || $RUBY )
then
curl -sL https://deb.nodesource.com/setup_$NODEJS_VERSION.x | execsudo -E bash -
execsudo apt-get update
aptinstall NodeJS nodejs
node -v
fi
if ( $YARN || $RUBY )
then
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | execsudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | execsudo tee /etc/apt/sources.list.d/yarn.list
execsudo apt-get update
aptinstall Yarn yarn
yarn -v
fi
if $VIM
then
aptinstall VIM vim
fi
if $VISUAL_STUDIO_CODE
then
snapinstall "Visual Studio Code" code --classic
fi
if $COMPRESS_7ZIP
then
aptinstall p7zip p7zip-full
aptinstall "7zip rar extension" p7zip-rar
fi
if $GIMP
then
aptinstall Gimp gimp
fi
if $VLC
then
snapinstall VLC vlc
fi
if $TORRENT
then
aptinstall qBitTorrent qbittorrent
fi
if $SPOTIFY
then
snapinstall Spotify spotify
fi
if $TERMINATOR
then
aptinstall Terminator terminator
fi
if $GPARTED
then
aptinstall GParted gparted
fi
if $POSTMAN
then
snapinstall Postman postman
fi
if $SKYPE
then
snapinstall Skype skype --classic
fi
if $GOOGLE_CHROME
then
exececho "*** installing Google Chrome 64bits"
CHROME_FILE=$DOWNLOADS/google-chrome-stable_current_amd64.deb
if [ -f $CHROME_FILE ]
then
exececho ""
else
cd $DOWNLOADS
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
fi
dpkginstall "Google chrome" $CHROME_FILE
fi
if $SQLITE
then
aptinstall SQLite sqlite3 libsqlite3-dev
fi
if $SQLITE_BROWSER
then
aptinstall "SQLite Browser" sqlitebrowser
fi
if $POSTGRESQL
then
aptinstall PostgreSQL postgresql postgresql-contrib libpq-dev
fi
if $PGADMIN
then
curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | execsudo apt-key add
execsudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'
aptinstall "PG Admin" pgadmin4
fi
if $MYSQL
then
uninstallmysql
execsudo debconf-set-selections <<< "mysql-server mysql-server/root_password password $MYSQL_USER"
execsudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $MYSQL_PASSWORD"
aptinstall MySQL mysql-server mysql-client libmysqlclient-dev
mysql -uroot -proot <<SQL
CREATE USER 'rails'@'localhost';
CREATE DATABASE activerecord_unittest DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
CREATE DATABASE activerecord_unittest2 DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
GRANT ALL PRIVILEGES ON activerecord_unittest.* to 'rails'@'localhost';
GRANT ALL PRIVILEGES ON activerecord_unittest2.* to 'rails'@'localhost';
GRANT ALL PRIVILEGES ON inexistent_activerecord_unittest.* to 'rails'@'localhost';
SQL
fi
if $WORKBENCH
then
cleanpackages
aptinstall "Workbench dependencies" mysql-client libmysqlclient-dev libatkmm-1.6-1v5 libcairomm-1.0-1v5 libglibmm-2.4-1v5 libgtkmm-3.0-1v5 libpangomm-1.4-1v5 libsigc++-2.0-0v5 libssl-dev libssl1.1 libproj-dev
exececho "*** Installing Workbench"
cd $DOWNLOADS
WORKBENCH_FILE=$DOWNLOADS/mysql-workbench-community_8.0.21-1ubuntu20.04_amd64.deb
if [ -f $WORKBENCH_FILE ]
then
exececho ""
else
wget https://dev.mysql.com/get/Downloads/MySQLGUITools/mysql-workbench-community_8.0.21-1ubuntu20.04_amd64.deb
fi
dpkginstall Workbench $WORKBENCH_FILE
fi
if $RUBY
then
cd
aptinstall "Ruby on Rails dependences" git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn
if [ $RUBY_VERSION_MANAGER == "RVM" ]
then
exececho "installing RVM with RUBY $RUBY_VERSION"
if ! type rvm >> $LOG_SCRIPT 2>> $LOG_SCRIPT 3>> $LOG_SCRIPT; then
aptinstall "RVM dependencies" libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
fi
rvm requirements
rvm install $RUBY_VERSION
rvm use $RUBY_VERSION --default
elif [ $RUBY_VERSION_MANAGER == "RBENV" ]
then
exececho "Installing Rbenv"
aptinstall "Rbenv dependencies" autoconf bison libssl-dev libyaml-dev libreadline-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev
deletedir $RBENV_DIR
git clone https://github.com/rbenv/rbenv.git $RBENV_DIR
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
git clone https://github.com/rbenv/ruby-build.git $RUBY_BUILD_DIR
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
exececho "Installing Ruby $RUBY_VERSION"
rbenv install $RUBY_VERSION -s >> $LOG_SCRIPT 2>> $LOG_SCRIPT 3>> $LOG_SCRIPT
rbenv global $RUBY_VERSION
else
exececho "*** installing Ruby from source"
cd $DOWNLOADS
RUBY_FILE=ruby-$RUBY_VERSION.tar.gz
RUBY_DOWNLOADED_FILE=$DOWNLOADS/$RUBY_FILE
if [ -f $RUBY_DOWNLOADED_FILE ]
then
exececho "--- $RUBY_FILE already exists"
else
wget http://ftp.ruby-lang.org/pub/ruby/$RUBY_VERSION_PATH/ruby-$RUBY_VERSION.tar.gz
fi
tar -xzvf $RUBY_DOWNLOADED_FILE
cd $DOWNLOADS/ruby-$RUBY_VERSION/
./configure
make
make install
fi
ruby -v
exececho "Installing Bundler"
exececho "gem: --no-ri --no-rdoc" > ~/.gemrc
gem install bundler -N >> $LOG_SCRIPT 2>> $LOG_SCRIPT 3>> $LOG_SCRIPT
if $RAILS
then
exececho "installing Rails $RAILS_VERSION"
gem install rails -v $RAILS_VERSION -N >> $LOG_SCRIPT 2>> $LOG_SCRIPT 3>> $LOG_SCRIPT
fi
fi
if $REDIS
then
aptinstall Redis redis-server
fi
if $REDIS_DESKTOP_MANAGER
then
snapinstall "Redis Desktop Manager" redis-desktop-manager
fi
if $TWEAKS
then
aptinstall Tweaks gnome-tweaks
fi
if $COLORIZE_TERMINAL
then
echo -e "" >> $BASHRC
echo -e "# Colorize terminal" >> $BASHRC
echo -e "function parse_git_branch () {" >> $BASHRC
echo -e " git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'" >> $BASHRC
echo -e "}" >> $BASHRC
echo -e 'RED="\[\033[01;31m\]"' >> $BASHRC
echo -e 'YELLOW="\[\033[01;33m\]"' >> $BASHRC
echo -e 'GREEN="\[\033[01;32m\]"' >> $BASHRC
echo -e 'BLUE="\[\033[01;34m\]"' >> $BASHRC
echo -e 'NO_COLOR="\[\033[00m\]"' >> $BASHRC
if $WITH_HOST
then
echo -e 'PS1="$GREEN\u@\h$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "' >> $BASHRC
else
echo -e 'PS1="$GREEN\u$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "' >> $BASHRC
fi
source $BASHRC
fi
cleanpackages
if $CONFIGURE_GITHUB
then
deletefile $GITCONFIG
execsudo touch $GITCONFIG
execsudo chmod 777 $GITCONFIG
echo -e "[user]" >> $GITCONFIG
echo -e " name = $GITHUB_NAME" >> $GITCONFIG
echo -e " email = $GITHUB_MAIL" >> $GITCONFIG
echo -e "[core]" >> $GITCONFIG
echo -e " editor = vim -f" >> $GITCONFIG
echo -e "[alias]" >> $GITCONFIG
echo -e " df = diff" >> $GITCONFIG
echo -e " st = status" >> $GITCONFIG
echo -e " cm = commit" >> $GITCONFIG
echo -e " ch = checkout" >> $GITCONFIG
echo -e " br = branch" >> $GITCONFIG
echo -e " lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative" >> $GITCONFIG
echo -e " ctags = !.git/hooks/ctags" >> $GITCONFIG
echo -e "[color]" >> $GITCONFIG
echo -e " branch = auto" >> $GITCONFIG
echo -e " diff = auto" >> $GITCONFIG
echo -e " grep = auto" >> $GITCONFIG
echo -e " interactive = auto" >> $GITCONFIG
echo -e " status = auto" >> $GITCONFIG
echo -e " ui = 1" >> $GITCONFIG
echo -e "[branch]" >> $GITCONFIG
echo -e " autosetuprebase = always" >> $GITCONFIG
echo -e "[github]" >> $GITCONFIG
echo -e " user = $GITHUB_USER" >> $GITCONFIG
if [ -f $SSH_FOLDER/id_rsa_github ]
then
exececho "SSH key '$SSH_FOLDER/id_rsa_github' already exists"
else
ssh-keygen -t rsa -b 4096 -C "$GITHUB_MAIL" -N "" -f $SSH_FOLDER/id_rsa_github
fi
touch $SSHCONFIG
echo -e "#Github (default)" >> $SSHCONFIG
echo -e " Host github" >> $SSHCONFIG
echo -e " HostName github.com" >> $SSHCONFIG
echo -e " User git" >> $SSHCONFIG
echo -e " IdentityFile $SSH_FOLDER/id_rsa_github" >> $SSHCONFIG
exececho2 ""
exececho2 ""
exececho2 "**********************"
exececho2 "CONFIGURE GIT USER"
exececho2 ""
exececho2 "Adicione a public ssh key em 'https://github.com/settings/ssh':"
exececho2 ""
exececho2 $(cat $SSH_FOLDER/id_rsa_github.pub)
exececho2 ""
exececho2 "**********************"
fi
if $POSTGRESQL
then
exececho2 ""
exececho2 ""
exececho2 "**********************"
exececho2 "CONFIGURE O POSTGRES"
exececho2 ""
exececho2 "Para definir usuário e senha no PostgreSQL, execute manualmente os seguintes comandos:"
exececho2 "sudo -u postgres createuser NOME_DO_USUARIO -s"
exececho2 "sudo -u postgres psql"
exececho2 "\password NOME_DO_USUARIO"
exececho2 "**********************"
fi
echo ""
echo ""
echo "***************************"
echo "*** See instalation logs in '$(pwd)/$LOG_SCRIPT'"
echo "***************************"
exececho2 ""
exececho2 ""
exececho2 "***************************"
exececho2 "***RESTART YOUR COMPUTER***"
exececho2 "***************************"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment