Skip to content

Commit eff4905

Browse files
committed
Initial commit
1 parent a608fd5 commit eff4905

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

python_setup.sh

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#!/bin/bash
2+
## shellcheck disable=SC2046
3+
4+
# For Ubuntu 20.4 LTS.
5+
#
6+
# Copyright (C) 2021 Danipulok <https://github.com/danipulok>
7+
#
8+
# You can redistribute this file and/or modify it under the terms of
9+
# the GNU Lesser General Public License as published by the
10+
# Free Software Foundation, either version 3 of the License, or
11+
# (at your option) any later version.
12+
#
13+
# This file is distributed in the hope that it will be useful,
14+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
# GNU Lesser General Public License for more details.
17+
#
18+
# You can see a copy of the GNU Lesser General Public License
19+
# at <http://www.gnu.org/licenses/>.
20+
21+
22+
# Pretty print
23+
function pretty_print {
24+
echo -e "\n############################################"
25+
echo "$1"
26+
echo -e "############################################\n"
27+
}
28+
29+
30+
# Update && upgrade all
31+
function update_upgrade {
32+
pretty_print "Updating && Upgrading"
33+
sudo apt update && sudo apt upgrade -y
34+
}
35+
36+
37+
# Add repository for neofetch, python3-pip, python3-venv
38+
function add_universe {
39+
pretty_print "Adding 'universe'"
40+
sudo add-apt-repository universe
41+
}
42+
43+
44+
# Install essential packages
45+
function install_essential_packages {
46+
pretty_print "Installing essential packages"
47+
sudo apt install -y \
48+
neofetch htop git curl \
49+
build-essential libssl-dev libffi-dev software-properties-common \
50+
python3-pip python3.8 python3-venv pipenv
51+
}
52+
53+
54+
# Install python-3.9 from PPA
55+
# Pass "-d" to delete PPA after installing python
56+
function install_python_from_ppa {
57+
pretty_print "Installing python from PPA"
58+
59+
if [ $# -eq 0 ]
60+
then
61+
pretty_print "Error. Please pass python version as '3.8' or similar to install one."
62+
return 1
63+
else
64+
python_version=$1
65+
python_to_install="python$python_version"
66+
fi
67+
68+
sudo add-apt-repository ppa:deadsnakes/ppa
69+
sudo apt update
70+
sudo apt install "$python_to_install"
71+
72+
if [[ $* == *-d* ]]
73+
then
74+
pretty_print "Deleting the PPA repository"
75+
sudo add-apt-repository --remove ppa:deadsnakes/ppa
76+
fi
77+
}
78+
79+
80+
# Build python from sources
81+
function build_python_from_sources {
82+
if [ $# -eq 0 ]
83+
then
84+
pretty_print "Please pass python version as '3.8.9' or similar to download and build one."
85+
return 1
86+
else
87+
python_version=$1
88+
python_url="https://www.python.org/ftp/python/$python_version/Python-$python_version.tgz"
89+
fi
90+
91+
pretty_print "Installing packages for buling Python."
92+
sudo apt update
93+
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
94+
95+
# ftp-url: https://www.python.org/ftp/python/
96+
wget "$python_url"
97+
tar -xf "Python-$python_version.tgz"
98+
99+
cd "Python-$python_version" || pretty_print "Error. Can't cd to 'Python-$python_version'." && return 1
100+
./configure --enable-optimizations
101+
102+
pretty_print "Start building python $python_version"
103+
make -j $(nproc) || make -j 6
104+
sudo make install
105+
}
106+
107+
108+
# Overwrite default `python` and `pip`
109+
function overwrite_default_python {
110+
if [ $# -eq 0 ]
111+
then
112+
pretty_print "Please pass python version to be set as default. Exiting function."
113+
return 1
114+
else
115+
username=$1
116+
fi
117+
118+
sudo update-alternatives --install "/usr/bin/python python /usr/bin/python($1)" 1
119+
sudo update-alternatives --set python "/usr/bin/python($1)"
120+
sudo ln -s /usr/bin/pip3 /usr/bin/pip
121+
}
122+
123+
# Create user and add him to the sudo group
124+
function create_sudo_user {
125+
if [ $# -eq 0 ]
126+
then
127+
pretty_print "Please pass user name to be added. Exiting function."
128+
return 1
129+
else
130+
username=$1
131+
fi
132+
133+
pretty_print "Adding new user with username '$username'"
134+
adduser $username
135+
136+
pretty_print "Adding '$username' to sudo group"
137+
adduser $username sudo
138+
}
139+
140+
141+
# Change PermitRootLogin to `without-password`
142+
#https://security.stackexchange.com/questions/174558/
143+
function secure_server {
144+
sudo sed -i '/^PermitRootLogin/s/yes/without-password/' /etc/ssh/sshd_config
145+
sudo sed -i '/^#PermitRootLogin prohibit-password/s/#PermitRootLogin prohibit-password/PermitRootLogin without-password/' /etc/ssh/sshd_config
146+
sudo sed -i '/^#PasswordAuthentication yes/s/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
147+
}
148+
149+
150+
# Install docker
151+
# https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04
152+
function install_docker {
153+
pretty_print "Installing docker"
154+
sudo apt update
155+
sudo apt install apt-transport-https ca-certificates curl software-properties-common
156+
157+
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
158+
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
159+
160+
sudo apt update
161+
sudo apt install docker-ce
162+
}
163+
164+
# Add current user to Docker running group
165+
function give_current_user_docker_privileges {
166+
pretty_print "Giving current user privileges to run docker"
167+
sudo usermod -aG docker $(whoami)
168+
}
169+
170+
# Essential
171+
update_upgrade
172+
add_universe
173+
install_essential_packages
174+
175+
# Install other python version
176+
# pass "-d" in case you want to delete PPA after installing Python
177+
install_python_from_ppa 3.8 -d
178+
# build_python_from_sources 3.8.9
179+
overwrite_default_python 3.8
180+
181+
# New user
182+
create_sudo_user danipulok
183+
su - danipulok
184+
185+
# Additional
186+
install_docker
187+
give_current_user_docker_privileges
188+
189+
# ONLY AFTER ADDING SSH KEYS.
190+
# secure_server

0 commit comments

Comments
 (0)