I signed up for Amazon EC2 and launched a new Amazon Linux AMI Instance. I wanted to host a Python 3 project I was working on, but I couldn't find python3 in the package manager... so I decided to try to build from source.

I tested these instructions in a fresh install of
Amazon Linux AMI 2014.09 - ami-fd4724c7 (64-bit)

Hint: If using Putty on Windows, use right-click to paste.

1. Fetch Python

(check www.python.org/downloads/ for newer releases)
wget https://www.python.org/ftp/python/3.4.1/Python-3.4.1.tgz

We now extract it:
tar zxvf Python-3.4.1.tgz
cd Python-3.4.1

This directory contains the Python source. I always like to look at the README

2. Install Dependencies

All you need in order to install the core Python interpreter itself is a C compiler
sudo yum install gcc

However, the Python standard library (which you almost certainly need) has some additional dependencies.

I found a (slightly outdated) list of packages that the Python standard library needs at www.unixmen.com/howto-install-python-3-x-in-ubuntu-debian-fedora-centos/
sudo yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel

For the lzma module (new in Python3.3):
sudo yum install xz-devel

The '-devel' indicates that these packages contain headers files and everything else needed so that we can link to them when building code from source.

Optionally, you might like to install some common development packages to increase your chances of success if you try to build something else from source later on (similar to Ubuntu's 'build-essential' package):
sudo yum groupinstall "Development tools"

3. configure


4. make

Hint: use 'make -s' to prevent make from flooding the console with every command it runs, but be prepared to wait a few minutes.

If you are missing a dependency for a module, the output of make will let you know (scroll up to the line that starts with 'Python build finished successfully!'). Here is what happens if you don't bother to install any of the dependencies (see the 'Install Dependencies' section above) other than a C compiler:
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2                  _curses               _curses_panel
_dbm                  _gdbm                 _lzma
_sqlite3              _ssl                  _tkinter
readline              zlib
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

If this happens to you, use the source code to track down the missing libraries. For example, 'setup.py' tells us that the 'lzma' module depends on the 'lzma' C library. If we do an internet search for the terms: 'lzma rpm', we learn that the package we need is called 'xz-devel'. Install the missing package, and start back at the configure step.

The final make result:
Python build finished successfully!
The necessary bits to build these optional modules were not found:
To find the necessary bits, look in setup.py in detect_modules() for the module  

Don't worry about 'tkinter', it's a GUI, and we're only running a command line.

5. make test

(Running the tests takes a while, so you may want to skip this step)
make test

6. make (alt)install

sudo make altinstall
The 'altinstall' argument (as opposed to just 'install') prevents conflicts with other Python versions. Accidentally interfering with the system version of Python will break many Linux packages (including the package manager)... don't say I didn't warn you!

7. Find Python

An 'altinstall' installs Python to /usr/local/bin/python3.4
whereis python*
python: /usr/bin/python2.6 /usr/bin/python /usr/lib/python2.6 /usr/lib64/python2.6 /usr/local/bin/python3.4m /usr/local/bin/python3.4m-config /usr/local/bin/python3.4 /usr/local/lib/python3.4 /usr/include/python2.6

Python 3.4 includes 'pip' (the Python package manager) by default:
whereis pip*
pip: /usr/local/bin/pip3.4

8. Create Symlinks

For time being, you will need to type 'python3.4' in order to use Python (mildly annoying). Also, since /usr/local isn't on root's path, it won't be found when you try to use python with sudo, unless you provide the full path to python (really annoying).
sudo python3.4
sudo: python3.4: command not found

To fix this, we can create our own symlink in /usr/bin
sudo ln -s /usr/local/bin/python3.4 /usr/bin/python3
sudo ln -s /usr/local/bin/pip3.4 /usr/bin/pip3

9. Use it

python3 -c "import __hello__"
sudo pip3 install virtualenv


Thanks to the following blogs/howtos/posts for easing the path: