Hello!
This document provides a walkthrough to set up a Python development environment for CS41 on Linux. Alternate versions of this guide exist for macOS and Windows development.
In this course, you'll be writing lots of Python code, so it's important to get your development environment set up and ready-to-go. Moreover, we will sometimes need to install dependencies and packages, so we want to make sure that everything is installed to the right place as well. The tools we develop in this document will also be useful for any external development you might pursue in Python.
Throughout this document, we will:
- Install Python 3.10.3
- Create a virtual environment using this version of Python.
- Inside this environment, install useful packages.
Let's get started!
We assume that you have a basic familiarity with the command line. We understand that not everyone will feel comfortable with the command line, because it is covered starting in CS107. However, I highly recommend using Nick Troccoli's amazing CS107 resources for this quarter if you feel less experienced, particular the section titled "Common Unix Commands."
On different Linux systems, there are a couple of different ways to install and manage packages. We'll cover apt-get
here, as well as how you can install from source.
apt-get
is Linux's Advanced Package Tool, and is very useful for installing, managing, upgrading, and removing packages on Debian, Ubuntu, and a few other Linux distributions. We'll use Felix Krull's PPA to install specific Python versions.
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.10
$ sudo apt-get install python3.10-venv
On Debian, we'll just have to sudo apt-get install python3
and hope for the best. Also note that this might install a different version of Python 3.10, but we won't worry about that here.
Other Linux distributions use a different package manager, yum
. We don't have any test devices with these Linux distributions, so you're on your own here. There is a reasonably good tutorial for CentOS here.
The world of Linux distributions is unfathomably large. If you can pull off a Python 3.10 install on your distribution of choice, more power to you. However, we recommend building from source.
Installing Python from source follows the same pattern as most other source installations.
First, download the source tarball (either gzipped or XZ compressed). Unzip the files and cd
into the unzipped directory.
To build Python, just execute the usual commands:
$ ./configure
$ make
$ make test
$ sudo make install
Note: If you want to install Python to a non-standard location, you can ./configure --prefix=/some/other/directory
in order to, say, not overwrite a system-installed Python, which might be useful for distributions like CentOS
More information on the actual installation process can be found in the tarball's README.
From here, the instructions are almost exactly the same as the macOS instructions. Follow those instructions, with the following differences.
Much of this handout was based on a similar handout written by Sam Redmond (@samredmond)