Skip to content

Instantly share code, notes, and snippets.

@ygorth
Forked from iDevPy/centos_python_env_setup
Last active July 24, 2022 13:26
Show Gist options
  • Save ygorth/974a01f33dbd9bcf63b2 to your computer and use it in GitHub Desktop.
Save ygorth/974a01f33dbd9bcf63b2 to your computer and use it in GitHub Desktop.
CentOS 7: Install Python 3.6.1, pip, virtualenv, and virtualenvwrapper
#!/bin/bash
#####################################################################
## IMPORTANT ##
# Run this script with root (sudo su -), wont work if run as sudo.
# Change the variables as needed.
######################################################################
USER=youruser # User that will have ownership (chown) to /usr/local/bin and /usr/local/lib
USERHOME=/home/${USER} # The path to the users home, in this case /home/youruser
PYSHORT=3.6 # The Python short version, e.g. easy_install-${PYSHORT} = easy_install-3.6
PYTHONVER=3.6.1 # The actual version of python that you want to download from python.org
cd ${USERHOME}
# Install development tools and some misc. necessary packages
yum -y groupinstall "Development tools"
yum -y install zlib-devel # gen'l reqs
yum -y install bzip2-devel openssl-devel ncurses-devel # gen'l reqs
yum -y install mysql-devel # req'd to use MySQL with python ('mysql-python' package)
yum -y install libxml2-devel libxslt-devel # req'd by python package 'lxml'
yum -y install unixODBC-devel # req'd by python package 'pyodbc'
yum -y install sqlite sqlite-devel xz-devel
yum -y install readline-devel tk-devel gdbm-devel db4-devel
yum -y install libpcap-devel xz-devel # you will be sad if you don't install this before compiling python, and later need it.
yum -y install libjpeg-devel
# Alias shasum to == sha1sum (will prevent some people's scripts from breaking)
echo 'alias shasum="sha1sum"' >> ${USERHOME}/.bashrc
# Install Python ${PYTHONVER} (do NOT remove 2.7, by the way)
wget --no-check-certificate https://www.python.org/ftp/python/${PYTHONVER}/Python-${PYTHONVER}.tgz
tar -zxvf Python-${PYTHONVER}.tgz
cd ${USERHOME}/Python-${PYTHONVER}
./configure --prefix=/usr/local LDFLAGS="-Wl,-rpath /usr/local/lib" --with-ensurepip=install
make && make altinstall
# Install virtualenv and virtualenvwrapper
cd ${USERHOME}
chown -R ${USER} /usr/local/bin
chown -R ${USER} /usr/local/lib
easy_install-${PYSHORT} virtualenv
easy_install-${PYSHORT} virtualenvwrapper
echo "export WORKON_HOME=${USERHOME}/.virtualenvs" >> ${USERHOME}/.bashrc # Change this directory if you don't like it
echo "export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3.6" >> ${USERHOME}/.bashrc
echo "export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv" >> ${USERHOME}/.bashrc
echo 'source /usr/local/bin/virtualenvwrapper.sh' >> ${USERHOME}/.bashrc # Important, don't change the order.
source ${USERHOME}/.bashrc
mkdir -p ${WORKON_HOME}
chown -R ${USER} ${WORKON_HOME}
chown -R ${USER} ${USERHOME}
# Done!
# Now you can do: `mkvirtualenv foo`
@pidzej
Copy link

pidzej commented Nov 6, 2017

Nice thx!
Consider add yum -y install wget ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment