Installing Python 3 from Source#

The purpose of this guide is to allow you to install python 3.x into Ubuntu Linux (or its variants) without affecting the system python installation used for system scripts. We’ll install to:

$ mkdir ~/opt
$ cd ~/opt

The reason for opt in our home folder is simple. We won’t need sudo permissions and it is easier to deal with multiple versions depending on our needs.

Update#

Versions#

This article was updated to work with Ubuntu 20.04 and Python 3.9.1 as of 2020-12-15.

Note

The instructions are mostly identical for newer version of python. Probably the only changes would be the version numbers

References#

Dependencies#

In order to build from source you will need the build essentials. Install them with the following command:

$ sudo apt install build-essential \
                   bzip2 \
                   checkinstall \
                   libbz2-dev\
                   libc6-dev \
                   libgdbm-dev \
                   libncursesw5-dev \
                   libncurses5-dev \
                   libreadline-dev \
                   libsqlite3-dev \
                   libssl-dev \
                   libffi-dev \
                   libbz2-dev \
                   sqlite3 \
                   tk-dev \
                   libnss3-dev \
                   liblzma-dev \
                   zlib1g-dev \
                   libgdbm-compat-dev \
                   wget \
                   xz-utils

Or (This may or may not work - it didn’t for Ubuntu 20.04):

$ sudo apt-get build-dep python3.9

Note

You may need to install other dependencies later on in order to properly build python from source.

Download Source#

Create a temporary folder:

$ mkdir ~/tmp
$ cd ~/tmp

Download the source files:

$ wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tar.xz

Extract the source code:

$ tar -xvf Python-3.9.1.tar.xz
$ cd Python-3.9.1

Install Python#

Run configure and specify the prefix path, that is the path to our target folder in ~/opt:

$ ./configure --enable-optimizations --prefix=/home/troy/opt/python_3.9.1

Note

Set the –prefix option to point to the folder where we want to install python, in our case ~/opt/python_3.9.1.

$ make -j 16

Note

For faster build times, modify the number of available cores (logical threads) with -j switch.

Note

After running make, check to see if it complains about any dependencies that might be missing. Once the dependencies are installed, run make again to confirm. The dependencies can be installed from the Ubuntu repos.

$ make test
$ make altinstall

Python Shortcut#

Since we have installed python to ~/opt, it isn’t on our path yet. Let’s add it! We’ll need to modify .bashrc, adding some aliases so that we can use the newly installed python. I like setting up python3 as the python to run:

# Python Path
python_path="~/opt/python_3.9.1/bin"
alias python3="$python_path/python3.9"
alias pip3="$python_path/pip3.9"
alias virtualenv="$python_path/virtualenv"
type python3
python3 is aliased to `~/opt/python3.9.1/bin/python3.9`
type pip3
pip3 is aliased to `~/opt/python3.9.1/bin/pip3.9`

Virtualenv#

Install virtualenv so that we can create virtual environments easily.

pip3 install virtualenv
type virtualenv
virtualenv is aliased to `~/opt/python3.9.1/bin/virtualenv`

Note

type will show us where a command binary is located or where it points to. This helps to confirm our aliases are setup correctly.

Summary#

It is straight forward to install python from source. I like this approach because I can control where it is installed and what version I install. It doesn’t depend on your O/S’s package maintainers.