Install Python 3.7 without sudo on Ubuntu 22.04

This guide will show you how you can install Python 3.7 (or any other version) for a single user without using sudo. Note that it will only be available for that user, and can be convenient if you don't want to affect other users on the same machine.

1. Download the source code

First download the source. As I'm writing this guide, the latest version is 3.7.9.

Also, extract the source.

wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tar.xz
tar -xf Python-3.7.9.tar.xz

2. Install build dependencies

To build python will all its features, we will install the minimum build dependencies, plus some optional once to get features like ssl for python. You can choose to ignore some of these if you want.

sudo apt install -y build-essential libreadline-dev libssl-dev libsqlite3-dev libgdbm-dev libgdbm-compat-dev libbz2-dev tk-dev liblzma-dev libffi-dev

3. Compile the source code

Enter the source directory, and run the commands to build the source. You should replace the home directory with your own user (I used "simen")

cd Python-3.7.9
./configure --enable-optimizations --prefix=/home/simen/.local
make

4. Install it for the user

Finally we will install the compiled binaries for the user. Make sure not to use sudo here, or it will be installed for all users.

In the same directory as before run:

make install

Finally, we have to add the user-specific bin folder to our path, unless it is already.

Check if it's in your path with:

echo $PATH

It should look something like, and should start with /home/simen/.local/bin:

/home/simen/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

If it doesn't include your .local/bin folder, edit the /home/simen/.bashrc file and add to the top:

export PATH=$HOME/.local/bin:$PATH

It's important that $HOME/.local/bin is at the beginning of the path, so that it overrides the python3 binary if it installed somewhere else in your system.

Save and close the file.

Log out and log back in to refresh your PATH variable.

5. Test that it works

Now, finally run

python3

You should be running Python 3.7!

Leave a Reply

Your email address will not be published.