Python Like A Pro: Installing Python
Why do you need this guide?
As a professional Python developer, you’ll be asked to work on a wide variety of projects. The tools you’ll use to do that work may need to change on a project-by-project basis. One day you may be maintaining some legacy code written in Python 2; the next day, you may be prototyping in the very latest version of Python 3.
And here lies the problem. The 2019 Python Developers Survey found that 66% of Python developers get their Python interpreter either from their operating system or from a downloaded installer. For these developers, switching between Python versions is not easy and could result in a broken system.
So in this guide, you’ll learn how to install many different versions of Python at the same time and discover how to switch between them with ease.
Before You Begin
There’s a lot of different OS and package management choices out there. But in the Python Survey mentioned earlier, Linux was shown to be the most popular OS. Ubuntu 20.04 has been chosen as the baseline for this guide because it’s the latest LTS of the most popular Linux distribution. Ubuntu is also used in the Windows Subsystem for Linux, so it’s available to most Windows 10 based developers. On Mac OS, most of the tools shown here can be added via Homebrew.
When following this guide, it helps if you’re starting from a clean system. That way, any existing or additional Python installs can’t cause issues or confusion as you follow the steps below. If your system isn’t clean, try following this guide in a new Ubuntu virtual machine first, just so you know what to expect.
Finally, if you’d prefer to watch a video on this guide, scroll down to the bottom of the page and hit ‘play.’
Installing Python Like A Pro
Having the ability to install and switch between Python versions in seconds — without affecting your operating system — will make your job much easier. Fortunately, there’s a tool that can help.
Pyenv is an open-source command-line tool that’s easy to install and takes care of the messy business of installing and managing multiple Python versions.
Pyenv lets you:
- install multiple versions of Python at the same time.
- change your global version at any time.
- use different Python versions ‘per-project’.
- override your Python version with an environment variable.
- search for commands present in multiple versions of Python at once.
Follow the steps below to install the
pyenv tool on your system.
Step 1: Getting Your System Ready
Ubuntu 20.04 ships with version 3.8.2 of Python pre-installed as the ‘system’ version of Python. You’ll want to keep this, but it’s helpful to make it respond to all
python commands. By default, it will only respond to
python3. To fix this, you can install the
python-is-python3 command like so:
sudo apt install python-is-python3
If you’re not using Ubuntu 20.04, your system may already respond to the command
Step 2: Installing Pyenv
Installation is simple. A script in the open-source pyenv-installer project installs everything you need onto your computer.
curl https://pyenv.run | bash
After the script has run, you’ll need to add the following lines to the bottom of your
export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
If you’re new to Linux, you can do this by opening the Terminal application and typing
nano .bashrc. Nano uses arrows to navigate,
ctrl-oto write the changes out, and
Once that’s done, close your terminal session (type
exit) and then open a new terminal.
Next, we need to add some system libraries so that Pyenv can build Python directly from the Python source code. This list may change from time to time, so always check for the latest advice in the Pyenv Wiki.
sudo apt-get install -y build-essential libssl-dev zlib1g-dev libbz2-dev \ libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \ xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
You can check if your installation is ready with the following command:
If this command reports,
"Congratulations! You are ready to build pythons!" then you’re good to go.
Step 3: Installing Python 3.8.5
First of all, to see the full list of the available Python versions — there are a lot — type the following command.
pyenv install --list
Let’s install the very latest version of Python — which is 3.8.5 — using the following command:
pyenv install 3.8.5
Pyenv will now download, build, and install the requested version of Python.
pyenv to rehash its list of ‘shims’ to make sure the new version of Python you just added is ready for use. Shims are the programs
pyenv uses to redirect your Python calls. Rehashing instructs
pyenv to install shims for all Python binaries known to
Step 4: Switching Between Python Versions
Now that Python 3.8.5 is installed, let’s take a look at the Python versions that
pyenv reports are available for use:
Your versions list might look something like this:
* system (set by /home/ben/.pyenv/version) 3.8.5
* next to ‘system’ tells you that the system version of Python is currently the global default. To change the global default to the 3.8.5 release that you just installed, type:
pyenv global 3.8.5
To check which version of Python your system is now using globally, ask Python for its version number like so:
The answer should be ‘Python 3.8.5.’
To switch back to your system version of Python, you can use the special ‘system’ keyword like this:
pyenv global system
And that’s it. You can now install and switch between many different versions of Python any time you like!
To find out more about what pyenv can do for you, check out the website or try
pyenv --help. To get help on a specific command type
pyenv <command> --help.
If you liked this guide, you might find these others in our ‘Python Like A Pro’ series useful:
- Managing Python Project Dependencies Like A Pro!
- Managing Python Global Packages Like A Pro
- Build Python Docker Containers Like A Pro!
- Run Python In Production Like A Pro!
Here’s the video to accompany this guide: