Upgrading to Ubuntu 16.04, Python 3.6, and Django 1.11
I've finally decided to perform major version upgrades of the main components of this blog: the operating system (Ubuntu), the programming language (Python), and the web framework (Django). Doing these upgrades to the latest LTS (long-term support) versions will future-proof the stack for at least the next 3 years.
One of the main reasons you'd want to do this is to get the latest security updates. It's hard to keep up with security as vulnerabilities are discovered all the time. While there is no such thing as 100% security, just installing security patches for the main components that run your app could make it a lot more secure.
The other reason, of course, is new features and sometimes improved performance. Just switching from Python 2.7 to Python 3.6 alone is an easy win due to big improvements to the "dict" type which is used everywhere in Python.
For this site, here's what I've upgraded:
- Ubuntu 14.04 -> Ubuntu 16.04
- Python 2.7 -> Python 3.6
- Django 1.7 -> Django 1.11
The upgrades went pretty smoothly. It definitely helped that this site is quite simple and there's not many dependencies. If you have a big project to upgrade, then it might not be so easy.
Below is a list of things to think about when upgrading.
Upgrading to Ubuntu 16.04
One thing to be aware of when upgrading to Ubuntu 16.04 is that it doesn't have Python 2 installed by default. This release comes with Python 3.4 pre-installed.
If you're like me and use Ansible to automate server provisioning and deployment, you'll need to tell Ansible to use Python 3 as the current version still defaults to Python 2. This can be done easily by overriding the "ansible_python_interpreter" variable:
I'd also recommend that you upgrade the pre-installed Python 3.4 to Python 3.6 due to many improvements in this specific version. There's no official Ubuntu 16.04 PPA for Python 3.6, however, but you can use this one instead: https://launchpad.net/~fkrull/+archive/ubuntu/deadsnakes
Upgrading to Python 3.6
This is pretty straightforward, just add the deadsnakes PPA mentioned above and install Python 3.6.
sudo add-apt-repository ppa:fkrull/deadsnakes sudo apt-get update supo apt-get install python3.6
In your code, things to watch out for are unicode references (such as u'hello') and changes in built-in packages (such as urllib). Also, when creating your virtualenv, make sure to specify to use python3.6.
Upgrading to Django 1.11
Since I'm upgrading from Django 1.7, I expected a bunch of things were deprecated. In my case, these are things I had to deal with:
- Upgraded third-party packages
- Changed TEMPLATES setting (it's now a list of dictionaries instead of separate settings)
- Minor changes in urlpatterns
- Other changes in the built-in Django modules
Overall, the upgrades were actually quite painless as this project is simple and I have very few dependencies. I still have a few projects to upgrade, but my most active ones are already done. Using LTS versions and upgrading once every 3 years is not so bad. :)