Install Python 3.7 on Ubuntu 16.04

Aug 28, 2019 2 min read
Install Python 3.7 on Ubuntu 16.04

First than anything make sure you have uninstall and clean all other python installation tries if you've done any, otherwise the build might fail.

Install requirements

# You can copy/paste it on the terminal:
sudo apt-get install -y build-essential \
checkinstall \
libreadline-gplv2-dev \
libncursesw5-dev \
libssl-dev \
libsqlite3-dev \
tk-dev \
libgdbm-dev \
libc6-dev \
libbz2-dev \
zlib1g-dev \
openssl \
libffi-dev \
python3-dev \
python3-setuptools \
wget

Prepare to build

mkdir /tmp/Python37
cd /tmp/Python37

Pull down Python 3.7, build, and install

wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
tar xvf Python-3.7.0.tar.xz
cd /tmp/Python37/Python-3.7.0
./configure
sudo make altinstall

Then you would just call Python like so:
python3.7 ./yourScript.py

Pip should have been installed with this installation as well. To install packages use this format:

pip3.7 --version

Bonus (conf aliases)

Install pip first:
sudo apt-get -y install python3-pip
check you have it: pip3 --version

Change aliases for python and pip:
If you type: python --version it's most likely to give you 2.7 or other different than 3.7. To change this with a simple safe way would be to use an alias.

Place this into ~/.bashrc and ~/.bash_aliases file:

alias python=python3.7
alias python3=python3.7
alias pip=pip3.7
alias pip3=pip3.7

After adding the above in the file, run source ~/.bashrc and source ~/.bash_aliases

now if you check: python --version and pip --version you'll see 3.7

To avoid the error below, export these paths:
export PYTHONHOME=/usr/local/lib/python3.7/

export PYTHONPATH=/usr/local/lib/python3.7/

Posible errors:

Unable to get the locale encoding

Here’s the complete error message:

# python3.7
 Fatal Python error: Py_Initialize: Unable to get the locale encoding
 ImportError: No module named 'encodings'
 Current thread 0x00007ff41bf46700 (most recent call first):
 Aborted (core dumped)

Solution:

The problem might be due to multiple Python installations on your machine and the command ‘python3.7‘ might be referring to libraries that belong to a different version of Python. Well, there could be another reason – the command ‘python3.7‘ is not able to find PYTHON installation path. The below lines will be a good try for this error:

export PYTHONHOME=/usr/local/lib/python3.7/

export PYTHONPATH=/usr/local/lib/python3.7/

I hope it helps

Great! Next, complete checkout for full access to ArturoFM.
Welcome back! You've successfully signed in.
You've successfully subscribed to ArturoFM.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.