Developing With Python 3.8 on Ubuntu 18.04 LTS

Every version of Python brings along cool new features and improvements. But really recent versions are not available from default system package sources - you need to do some extra work to get to them.

Here are complete instructions how to install Python3.8 on Ubuntu 18.04 LTS, and a few more steps to setup an enjoyable Python development environment on a fresh machine.

Make Python 3.8 Available

Right now (18. May 2019), Python 3.8 is not available from Ubuntu’s default repositories. We’ll need to add a PPA, which contains packages for Python3.8.

Now we can add the PPA and update the information on available packages right afterwards.

--$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update

Install Packages

After we’ve added the PPA, the 3.8 version becomes available. We can install it now.

$ sudo apt-get install python3.8

If we’ll want to install packages through pip, we’ll need the right Python headers in place:

$ sudo apt-get install python3.8-dev
# this one is needed because of a current packaging bug
$ sudo apt-get install python3.8-distutils

We’ll also go ahead and install the Python 3 pip package. We’ll use it as a globally-installed tool, and it’s a nice way to get useful dependencies in place for free:

$ sudo apt-get install python3-pip

Virtualenv and Virtualenvwrapper

We’ll use virtualenv in any case, to create isolated Python environments, so our projects don’t mess with each other’s dependencies, nor the OS packages.

$ pip3 install virtualenv virtualenvwrapper

Notice that we’re not using sudo to install packages via pip - they’ll be installed into your ~/.local folder, and won’t be accessible to any other users on the system.

I still really like virtualenvwrapper to interact with different virtualenvs.

You’ll need to add the following lines to your ~/.bashrc file to make everything work together:

# add pip-installes executables accessible
export PATH=~/.local/bin:$PATH

# virtualenvwrapper settings
export WORKON_HOME=~/.envs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source ~/.local/bin/virtualenvwrapper.sh

Open a new terminal session (close the window and open a new one, or switch to a new tab) for the changes to take effect. You’ll see a few lines of virtualenvwrapper creating files - that’s expected and a good thing.

Showtime

Now, we can create a new virtualenv anytime we want

$ mkvirtualenv -p python3.8 a-good-name

Once you’re in your virtualenv, you won’t have to specify pip3 nor python3.8 on every command, as those will be used by default.

If you’re done working in your new virtualenv, you can get out of it with

$ deactivate

To enable it again (from anywhere), just type

$ workon a-good-name

In Conclusion

Now you have a functional Python 3.8 setup on your Ubuntu 18.04 machine. If you want to install something system-wide, make sure to use pip3.

If you need more control over Python versions, or want to set a certain Python version as default, without messing with system packages, check out pyenv as a convenient tool for switch between different Python versions dynamically.

Subscribe to my newsletter!
You'll get notified via e-mail when new articles are published. I mostly write about Docker, Kubernetes, automation and building stuff on the web. Sometimes other topics sneak in as well.

Your e-mail address will be used to send out summary emails about new articles, at most weekly. You can unsubscribe from the newsletter at any time.

Für den Versand unserer Newsletter nutzen wir rapidmail. Mit Ihrer Anmeldung stimmen Sie zu, dass die eingegebenen Daten an rapidmail übermittelt werden. Beachten Sie bitte auch die AGB und Datenschutzbestimmungen .

vsupalov.com

© 2024 vsupalov.com. All rights reserved.