Setup Poetry and Pyenv to use Python 3.11.0 on Linux Mint 21

Michael Shoemaker
7 min readNov 9, 2022

TL,DR — Go to then Bonus section at the end. Copy and run the script. Done. :-) You will still need to download and install VS Code though.

I tend to make a bit of a mess of my environments so a few years back I adopted creating virtual machines. In keeping with the times of re-producability and being like the “cool” kids I decided to go with current best practices and start using virtual environments.

It seems like many people are using conda virtual environments, but I ran into some issues using conda install. Specifically Prefect. While I could install it using pip, the conda install failed. Additionally now that Python 3.11 is out I wanted to play around with it so using Pyenv seemed like the logical choice to try it without needlessly burning an OS to the ground.

After discussing this in the Chipy (Chicago Python Users Group) on Slack I decided it was probably best to use Pyenv and Poetry. Then came the “stepping on the rakes”… oh so many many rakes. Well without further ado here we go!

Linux Post-Install Prep

You can do this on your main OS, but I would suggest spinning up a VMware Virtual Machine.

Here we have a nice shiny new Mint 21 Mate install on VMWare. As with any new Linux Install let’s update and upgrade before anything else.

Maybe now is a good time to grab a coffee, it may take 5–10 minutes.

Installing Pyenv

Once it is done we can install Pyenv by simply running.

curl https://pyenv.run | bash

I guess not. Luckily we have a helpful error message. It appears that Git is missing. Let’s be logical and try:

sudo apt-get install git

Now pyenv seems to be happy

But let’s look at the lines at the bottom. It looks like it is telling us that we need to add some lines to our .bashrc file.

As long as you are in your home directory, copy the lines it is telling you and then run

nano .bashrc

Ctrl + End to get to the bottom and Ctrl + Shift + Vto paste. You can delete the commented lines.

You will need to execute:

source ~/.bashrc

for these changes to take effect and for your OS to be able to recognize pyenv as a command.

Notice that we only see the default system listed. That is because we haven’t installed any other versions yet.

Installing Poetry

Now let’s install poetry. We can go to the poetry website found here: https://python-poetry.org/docs/ for the latest install command. At the time of this writing it is:

curl -sSL https://install.python-poetry.org | python3 -

Oops again. Now we see that a directory is missing or something like that. It’s a bit cryptic.

Fortunately a Google Search and a few links later we find that this seems to have something to do with python3-distutils not being installed. Let’s see if that makes it happy.

That seems a lot better. Again, pay attention to the output. It seems that it wants us to add another export to our .bashrc file.

Don’t forget to source your .bashrc file.

Creating a Poetry Project and Resolving Pyenv Issues

Now lets create our first Poetry Project. Switch to the directory where you would like your project and then run:

poetry new your-project-name

We can then cd into it and start working, but first we will need to install and activate the version of python that we want to use. You can list available versions of python in pyenv by running:

pyenv install --list

or be more precise using grep

pyenv install --list |grep 3.11

In this case I’m looking for 3.11 versions, but you can substitute this with whatever you’d like.

3.11.0 looks good to me. Let’s install that.

Oh for the love of….. *Sigh. Time to Google. Looks like it may want:

sudo apt install libc6-dev

Trying again.

Ok, a different error. That might be progress. Another Google adventure shows it may want this now:

sudo apt install \
build-essential \
curl \
libbz2-dev \
libffi-dev \
liblzma-dev \
libncursesw5-dev \
libreadline-dev \
libsqlite3-dev \
libssl-dev \
libxml2-dev \
libxmlsec1-dev \
llvm \
make \
tk-dev \
wget \
xz-utils \
zlib1g-dev

Now it seems a bit happier. :-)

Now we can set the pyenv to use 3.11.0.

We also will want to update the pyproject.toml file that poetry created to indicate that the supported version is Python 3.11 or higher.

Now we’re in the home stretch. Lets get a nice IDE to work with our project. I’ll use VS Code so we just open a browser and Google VS Code Download to get to their page. Once their choose the .deb file and click download.

Once it’s done you can click on the file shown above and choose “Install Package”

Once that is done we will activate our virtual environment with:

poetry shell

and start VS Code with

code .

Not much going on here

Let’s make a simple py file to let VS Code know we’re using python. As soon as you make a .py file under your project folder you should get a helpful pop up to install the python extension.

After the install you can click in the lower left hand corner where the python version is displayed (or click Ctrl + Shift + P and type Python: Select interpreter) and choose a different interpreter)

We are going to look for the interpreter that contains “pypoetry” in the file path. Notice in the image above this is an option. If it is not showing in yours look back at the terminal where you activated poetry and copy the file path after “Spawning shell within” Click Enter interpreter path.. in VS Code and paste the path there.

That’s it for now. Hopefully this helped some of you out there and cut down on the setup time needed for poetry.

Further Reading:

Poetry Documentation: https://python-poetry.org/

Best Practices for Python Development: https://mitelman.engineering/blog/python-best-practice/automating-python-best-practices-for-a-new-project/

Bonus:

You can simply copy the script below into a file in your home directory.

chmod +x <whatever you called the file>

and then ./<whatever you called the file> If for whatever reason you need to run it more than once you will need to remove the extra exports, evals etc. added to the end of your .bashrc file.

#!/bin/bash
sudo apt-get update -y && sudo apt-get upgrade -y
sudo apt install \
build-essential \
curl \
libbz2-dev \
libffi-dev \
liblzma-dev \
libncursesw5-dev \
libreadline-dev \
libsqlite3-dev \
libssl-dev \
libxml2-dev \
libxmlsec1-dev \
llvm \
make \
tk-dev \
wget \
xz-utils \
zlib1g-dev \
git \
python3-distutils
echo “export PYENV_ROOT=\”\$HOME/.pyenv\””>>.bashrc
echo “command -v pyenv >/dev/null || export PATH=\”\$PYENV_ROOT/bin:\$PATH\””>>.bashrc
echo “eval \”\$(pyenv init -)\””>>.bashrc
echo “eval \”\$(pyenv virtualenv-init -)\””>>.bashrc
curl -sSL https://install.python-poetry.org | python3 -echo “export PATH=\”/home/${USER}/.local/bin:\$PATH\””>>.bashrcsource ~/.bashrc

--

--