2 minute read

Most of tutorials on the Internet about installing Python3.6 on Ubuntu are by using 3rd party PPA repositories. If for any reason, you cannot use them, hereunder a quick tutorial for installing it from the Python official source, you should in advance download the source to the Ubuntu.

Installing Python3.6 on Ubuntu 16.04

Disabling IPv6

IPv6 is enabled by default on Ubuntu 16.04, in some cases, your Ubuntu network connection might be very low due to IPv6. Use ip a | grep inet6 to check if IPv6 is enabled.

Ref: How to disable ipv6 address on ubuntu 18 04 bionic beaver linux

To disable IPv6 in a persist way, add following 2 lines in the file /etc/sysctl.conf and reload the sysctl by sudo sysctl --system or reboot the server:

net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1

Installing build packages

sudo apt install -y build-essential zlib1g-dev libssl-dev

without libssl-dev package, pip install will throw TLS/SSL error.

From this point of view, installing Python on Windows by Scoop is much more pleasant :)

Installing Python3.6 from official source

The latest Python3.6 version at the time of this writing is 3.6.9.

# You may download the Python source to a local shared location (S3 or Artifactory, etc.) if you need to deploy Python to many servers.
wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz
tar xzvf Python-3.6.9.tgz
cd Python-3.6.9
sudo ./configure --prefix=/opt/python3.6
make -j $(nproc)
sudo make install
sudo ln -s /opt/python3.6/bin/python3.6 /usr/bin/python3.6

Python3.5 is preinstalled by default on Ubuntu 16.04, python3 -V gives Python 3.5.2, many system tools rely on it, please DO NOT bind python3 to any versions other than Python3.5, otherwise your system might have unexpected problems.

For a general Python installation not only for this Python3.6, if you have gcc v8+, you can add the flag --enable-optimizations to ./configure to gain an extra runtime speed, otherwise you might encounter Could not import runpy module error

Using Python3.6 pip

python3.6 -m pip install [a python module]

Prevent pip install without an active venv

echo 'export PIP_REQUIRE_VIRTUALENV=true' >> ~/.bashrc

Installing Python3.7 on Ubuntu 16.04

Just tested installing Python3.7.5 with the same procedure, all works.

Installing Python3.10.10 with sqlite3 on Ubuntu 20.04 in WSL

# install build packages
sudo apt update
sudo apt install -y build-essential zlib1g-dev libssl-dev libffi-dev

# install sqlite3 from source, if you need a specific sqlite3 version in Python, you must install it before compiling Python, because the compilation needs the lib libsqlite3.so
mkdir ~/src
cd ~/src/
wget https://www.sqlite.org/2021/sqlite-autoconf-3400100.tar.gz
tar xvf sqlite-autoconf-3400100.tar.gz
cd sqlite-autoconf-3400100/
./configure --prefix=/usr/local
make -j $(nproc)
sudo make install
make clean
ll /usr/local/bin/sqlite*
ll /usr/local/lib/*sqlite*

# let below Python compilation to use the newly installed sqlite3 lib
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

# install python3.10.10 from source
cd ~/src/
wget https://www.python.org/ftp/python/3.10.10/Python-3.10.10.tgz
tar xvf Python-3.10.10.tgz
cd Python-3.10.10/

# ubuntu 20.04 has gcc v9, so you can add the flag --enable-optimizations to ./configure
# --with-bz2 is for pandas, otherwise modulenotfounderror: no module named '_bz2' pandas
./configure --prefix=$HOME/opt/python3.10 --with-bz2
make -j $(nproc)
sudo make install
make clean
sudo ln -s ~/opt/python3.10/bin/python3.10 /usr/bin/python3.10
ll $(which python3.10)
echo -e '\nexport PIP_REQUIRE_VIRTUALENV=true' >> ~/.bashrc
python3.10 -c 'import sqlite3 ; print(sqlite3.sqlite_version)'

Leave a comment