Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save drconopoima/e9102c965f8fc2a00b58bcd0dc86ec30 to your computer and use it in GitHub Desktop.
Save drconopoima/e9102c965f8fc2a00b58bcd0dc86ec30 to your computer and use it in GitHub Desktop.
Script for quick configuration of CentOS 7 to manage and use different python virtual environments. Configures locale to en_US.UTF-8, installs Python 3, development packages. Installs Pyenv and Pipenv
#!/bin/sh
DESIRED_UTF8_LOCALE='en_US.UTF-8'
DESIRED_PYTHON_VERSION='2.7.16 3.4.10 3.5.7 3.6.9 3.7.4'
# PYTHON_COLLECTION_VERSION='27'
# PYTHON_COLLECTION_VERSION='34'
# PYTHON_COLLECTION_VERSION='35'
PYTHON_COLLECTION_VERSION='36'
PYTHON_PACKAGE_COLLECTION="rh-python${PYTHON_COLLECTION_VERSION}"
SHELL_PROFILE='/etc/bashrc'
NUMBER_OF_THREADS=8
###################
## Update CentOS ##
###################
yum update -y
####################
## Manage locales ##
####################
## To list locales
# locale -a
## Other locales
## Locale en_US.UTF-8 may be overriden by the configuration of YUM. To circumvent this:
## First Delete the line that overrides conf:
# sed -i.bak '/override_install_langs/d' /etc/yum.conf && \
## It gets backed up to /etc/yum.conf.bak
## Finally reinstall glibc-common for being able to change the locale
# yum reinstall glibc-common
## Change LOCALE #
echo -e "\nexport LC_ALL=$DESIRED_UTF8_LOCALE\nexport LANG=$DESIRED_UTF8_LOCALE\nexport LANGUAGE=$DESIRED_UTF8_LOCALE\n" >> $SHELL_PROFILE
source $SHELL_PROFILE
############################################################################################################################
######################
## install Python 3 ##
######################
yum install -y @development
yum install -y centos-release-scl
yum install -y $PYTHON_PACKAGE_COLLECTION
## Install PyENV dependencies
yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel \
openssl-devel xz xz-devel libffi-devel findutils -y
## Enable RHSCL Python collection environment session (inc. variables python3, python36, pip3. Variable python will point to version of the installed collection)
## Add RHSCL Python For a single session
# scl enable $PYTHON_COLLECTION_VERSION bash
## Add RHSCL Python 3 by default to all login sessions
echo -e "source scl_source enable $PYTHON_PACKAGE_COLLECTION\n" >> $SHELL_PROFILE
source $SHELL_PROFILE
## Corroborate Python version
# which python
## Install Pipenv
PIP_COMMAND=(pip${PYTHON_COLLECTION_VERSION})
$PIP_COMMAND install pip
$PIP_COMMAND install --upgrade pipenv
## Install Pyenv
curl https://pyenv.run | bash
echo -e '\nexport PATH="/root/.pyenv/bin:$PATH"\neval "$(pyenv init -)"\neval "$(pyenv virtualenv-init -)"\n' >> $SHELL_PROFILE
source $SHELL_PROFILE
## Install desired Python versions in parallel up to number of threads specified above
# Based on answer by PSkocik at Stackexchange here: https://unix.stackexchange.com/questions/103920/parallelize-a-bash-for-loop
(
for python_version in $DESIRED_PYTHON_VERSION; do
((i=i%NUMBER_OF_THREADS)); ((i++==0)) && wait
pyenv install "$python_version" &
done
)
wait
echo "Script execution finished successfully."
#!/bin/sh
DESIRED_UTF8_LOCALE='en_US.UTF-8'
DESIRED_PYTHON_VERSION='2.7.16 3.4.10 3.5.7 3.6.9 3.7.4'
# PYTHON_COLLECTION_VERSION='27'
# PYTHON_COLLECTION_VERSION='34'
# PYTHON_COLLECTION_VERSION='35'
PYTHON_COLLECTION_VERSION='36'
PYTHON_PACKAGE_COLLECTION="rh-python${PYTHON_COLLECTION_VERSION}"
SHELL_PROFILE="$HOME/.bashrc"
NUMBER_OF_THREADS=8
###################
## Update CentOS ##
###################
sudo yum update -y
####################
## Manage locales ##
####################
## To list locales
# locale -a
## Other locales
## Locale en_US.UTF-8 may be overriden by the configuration of YUM. To circumvent this:
## First Delete the line that overrides conf:
# sudo sed -i.bak '/override_install_langs/d' /etc/yum.conf && \
## It gets backed up to /etc/yum.conf.bak
## Finally reinstall glibc-common for being able to change the locale
# sudo yum reinstall glibc-common
## Change LOCALE #
echo -e "\nexport LC_ALL=$DESIRED_UTF8_LOCALE\nexport LANG=$DESIRED_UTF8_LOCALE\nexport LANGUAGE=$DESIRED_UTF8_LOCALE\n" >> $SHELL_PROFILE
source $SHELL_PROFILE
############################################################################################################################
######################
## install Python 3 ##
######################
sudo yum install -y @development
sudo yum install -y centos-release-scl
sudo yum install -y $PYTHON_PACKAGE_COLLECTION
## Install PyENV dependencies
sudo yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel \
openssl-devel xz xz-devel libffi-devel findutils -y
## Enable RHSCL Python collection environment session (inc. variables python3, python36, pip3. Variable python will point to version of the installed collection)
## Add RHSCL Python For a single session
# scl enable $PYTHON_COLLECTION_VERSION bash
## Add RHSCL Python 3 by default to all login sessions
echo -e "source scl_source enable $PYTHON_PACKAGE_COLLECTION\n" >> $SHELL_PROFILE
source $SHELL_PROFILE
## Corroborate Python version
# which python
## Install Pipenv
PIP_COMMAND=(pip${PYTHON_COLLECTION_VERSION})
$PIP_COMMAND install pip
$PIP_COMMAND install --upgrade pipenv
## Install Pyenv
curl https://pyenv.run | bash
echo -e '\nexport PATH="/root/.pyenv/bin:$PATH"\neval "$(pyenv init -)"\neval "$(pyenv virtualenv-init -)"\n' >> $SHELL_PROFILE
source $SHELL_PROFILE
## Install desired Python versions in parallel up to number of threads specified above
# Based on answer by PSkocik at Stackexchange here: https://unix.stackexchange.com/questions/103920/parallelize-a-bash-for-loop
(
for python_version in $DESIRED_PYTHON_VERSION; do
((i=i%NUMBER_OF_THREADS)); ((i++==0)) && wait
pyenv install "$python_version" &
done
)
wait
echo "Script execution finished successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment