Skip to content

Instantly share code, notes, and snippets.

@jgibbard
Last active November 10, 2020 08:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jgibbard/3917da6d4c8303915f301e4bda5265f4 to your computer and use it in GitHub Desktop.
Save jgibbard/3917da6d4c8303915f301e4bda5265f4 to your computer and use it in GitHub Desktop.
CentOS7 - Install Python 2.7 and 3.6 from source
#!/bin/bash
# Python 2.7 and 2.6 install script for CentOS 7
# Instructions mainly taken from tutorial by Daniel Eriksson
# https://danieleriksson.net/2017/02/08/how-to-install-latest-python-on-centos/
python27_version=2.7.15
python36_version=3.6.6
build_dir=python_build_temp
example_environment_dir=pythonEnv
echo "Install prerequisites"
sudo yum groupinstall -y "Development Tools"
sudo yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel expat-devel
echo "Create temp directory to build python"
mkdir $build_dir
cd $build_dir
echo "Download Python 2.7"
wget http://python.org/ftp/python/$python27_version\/Python-$python27_version\.tar.xz
tar xf Python-$python27_version\.tar.xz
cd Python-$python27_version
./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
echo "Build and install Python 2.7"
make && sudo make altinstall
cd ..
echo "Download Python 3.6"
wget http://python.org/ftp/python/$python36_version\/Python-$python36_version\.tar.xz
tar xf Python-$python36_version\.tar.xz
cd Python-$python36_version
./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
echo "Build and install python 3.6"
make && sudo make altinstall
cd ..
wget https://bootstrap.pypa.io/get-pip.py
echo "Install pip for Python 2.7"
sudo /usr/local/bin/python2.7 get-pip.py
echo "Install pip for Python 3.6"
sudo /usr/local/bin/python3.6 get-pip.py
echo "Install virtualenv for Python 2.7"
sudo /usr/local/bin/pip2.7 install virtualenv
# Note: Python 3.3+ has the ability to create virtual environments built in so virtualenv is not required
echo "Clean up installation files"
cd ..
sudo rm -rf $build_dir
echo "Create example environment for Python 2.7 and 3.6"
mkdir $example_environment_dir
cd $example_environment_dir
# Using full paths to be certian that the correct binaries are used
/usr/local/bin/virtualenv -p /usr/local/bin/python2.7 py2.7env
/usr/local/bin/python3.6 -m venv py3.6env
echo "For Python 2.7 use: source py2.7env/bin/activate"
echo "Once active use: pip install numpy matplotlib"
echo "To deactivate type: deactivate"
echo "For Python 3.6 use: source py3.6env/bin/activate"
echo "Once active use: pip install numpy matplotlib"
echo "To deactivate type: deactivate"
echo "Install complete!"
@jgibbard
Copy link
Author

Use at your own risk
All thanks should go to Daniel Eriksson
All errors are my own.

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