Installing Python on your Linux distribution of choice can be done on a few ways
- Building from source
- Via the package manager
- Using third party tools
Building from source can be quite slow if you're on a less powerful machine and installing through your distributions package repository doesn't always offer the latest version of Python.
To solve this, we're going to use a fantastic third party library called Pyenv.
What is Pyenv?
Pyenv is a tool for Linux and Mac that makes installing and managing any version on your system easy and simple. It has a short but sweet list of functions that make light work of installing and managing multiple versions of Python, through a set of simple commands.
With Pyenv, you can:
- Install any version of Python
- Quickly set and switch your global version of Python on a per-user basis
- Switch versions per project
It doesn't depend on itself as it's made purely from shell scripts and doesn't attempt to manage your virtual environments (Thankfully)
Another nice point to note is that Pyenv can be uninstalled extremely easily if for some reason you decide you want to get rid of it (Although I can't find a good reason you would!)
Enough talk, let's get started.
Clone the Pyenv repo
Go ahead and run the following to clone the repo into your home directory
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
Modify your .bashrc
Next, we need to add a few lines to our
.bashrc file to load Pyenv when a new shell is spawned. Go ahead and run the following:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
Tip - Zsh and Fish users, follow the guide here
Now we need to reload the shell with:
And just like that, Pyenv is installed! However before you download any versions of Python, you'll need some other dependencies..
Installing Python build dependencies
Python requires some libraries to be built by Pyenv and will vary from distro to distro.
Go ahead and install the packages for your Linux distribution with the following:
Ubuntu, Debian & mint
apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
CentOS & Fedore 21 (and below)
yum install gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel
Fedora 22 (and above)
dnf install -y make gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel
zypper install gcc automake openssl-devel ncurses-devel readline-devel zlib-devel tk-devel libffi-devel sqlite3-devel
pacman -S base-devel openssl zlib
If your distro isn't listed here, head over to the official Pyenv docs for a complete list.
Once all of the dependencies have installed, it's time to install Python
Installing Python with Pyenv
Installing Python is now just a single command away. With Pyenv, you use the following command syntax:
pyenv install <version number>
For example, say we want Python 3.7.2, we would run the following:
pyenv install 3.7.2
Once Pyenv has downloaded and built the version of Python, we set our global version using the following command syntax:
pyenv global <version number>
Again, let's set our version to Python 3.7.2:
pyenv global 3.7.2
Now when you run the
python command, you should see:
Python 3.7.2 (default, Jan 23 2019, 16:31:34) [GCC 7.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
To switch to another version, just enter the same command with an alternative version number.
Managing Python versions
To see the current global version that's been set, we use the following syntax:
You'll see something similar to the following:
3.7.2 (set by /home/julian/.pyenv/version)
To see all of the versions you've installed and managed by Pyenv, we use:
You'll see something similar to the following:
system 3.7.1 * 3.7.2 (set by /home/julian/.pyenv/version)
Tip - The current global version is indicated with the
*and parenthesis letting you know who set it
Uninstalling a version is just as simple
pyenv uninstall <version_number>
To disable Pyenv, delete the 3 lines you added to your
If you decide you want to uninstall Pyenv, just run the following command:
rm -rf $(pyenv root)
As you can see, Pyenv is a powerful tool for quickly installing and managing Python on most Linux distributions and sure beats doing it all yourself.
For a full list of functions and commands, head over to the official Pyenv docs here and be sure to drop a star on their Github page!