Installing Python 3.6 inside a Ubuntu 16.04LTS VPS
At the time of writing, Ubuntu 16.04LTS is one of the most common options available for installation in a new VPS. Providers like DigitalOcean, Hetzner and OpenCloud all have this distro as an option for automatic installation.
Being one of the most popular distros means that most tutorials and tool documentation cover it, making it a safe default choice for someone that has no special needs for their server.
The Python 3.6 issue
Ubuntu 16.04LTS supports up to version 3.5 of Python, and that’s what python3
links to.
# python3 --version
Python 3.5.2
After apt update
and apt upgrade
the above result remains the same, and searching for python3.6 returns no results.
Possible solutions
Installing from deadsnakes’ PPA
While there’s no Python 3.6 in the official repositories, we can add a PPA that contains it. The deadsnakes’ PPA contains older and newer versions of Python, up to 3.7 at the time of this writing.
# add-apt-repository ppa:deadsnakes/ppa -y
# apt update
# apt install python3.6 -y
# python3.6 --version
Python 3.6.4
Running python3
at this point will still use python 3.5, and that’s often perfectly fine.
At this point you could set python 3.6 as python3 globally, but that doesn’t mean you should do it. The reason is that every Python version has its own site_packages directory in /usr/local/lib/python{ver}. That means that any package installed globally while using 3.5 will no longer be available for use after executing update-alternatives
with python3
.
It doesn’t matter if you didn’t install any Python package on your own accord. The preinstalled python3-gdbm Ubuntu package breaks after doing the change, and any program that depends on it breaks too.
So, if you…
- …mistype a command,
- …connect remotely via ssh,
- …or use virtualenvwrapper correctly
You will get a gory stacktrace in the console telling you of the problem. Reinstalling the package “““solves””” the problem, but leaves the doubt regarding what else broke. Maybe nothing else breaks, but I’ll not check. There are better ways of working, after all.
Using pyenv for Python version management
pyenv, despite its name, has not much to do with Python Virtual Environments, but rather, is a tool that enables you to easily change between Python versions. It integrates well with pipenv and that makes it my favorite way of managing Python versions.
To install it, you must first install its dependencies:
# apt-get install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncurses5-dev libncursesw5-dev xz-utils tk-dev
And then, you can install pyenv itself:
$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
Load pyenv automatically, assumming default installation in home directory:
echo 'export PATH="~/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
Installing Python 3.6 becomes:
$ pyenv install 3.6.4
And to use that Python version, you have several options:
- [Setting a Python version to use in your current shell.] (https://github.com/pyenv/pyenv/blob/master/COMMANDS.md#pyenv-shell)
- [Setting a Python version to use when in a given directory.] (https://github.com/pyenv/pyenv/blob/master/COMMANDS.md#pyenv-local)
- [Setting a Python version to use globally.] (https://github.com/pyenv/pyenv/blob/master/COMMANDS.md#pyenv-global)
And those versions don’t conflict with the rest of the system, even when setting a version globally. Pretty good, ya?
Closing notes
I didn’t cover installing directly from a tarball, but that’s because it’s almost self explanatory (download, extract, run the python executable).
Now, installing Python is just the first step. Save for interactive scripting or for small programs without external dependencies, pretty much every Python program uses virtual environments, which will be the topic of the next post.