Pyenv in Ubuntu 22.04: ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?
Asked Answered
S

11

26

Moving to Ubuntu 22 with a fresh install (I have Ubuntu 20 in another partition) and the last piece I need to use it for working it to have pyenv running fine.

When trying to pyenv install x.xx.x it fails with this error: ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

I "tried to try" what the official docs proposes:

I said "tried to try" because I don't understand what I have to do. They give these commands:

CPPFLAGS="-I<openssl install prefix>/include" \
LDFLAGS="-L<openssl install prefix>/lib" \
pyenv install -v <python version>

But I don't know what the "openssl install prefix" means. I assumed it's the openssl installation dir, so I did:

openssl version -d
OPENSSLDIR: "/home/linuxbrew/.linuxbrew/etc/[email protected]"

Even though I see that in this folder there arent' any include or lib subfolders, I tried it anyway by doing this:

CPPFLAGS="-I/home/linuxbrew/.linuxbrew/etc/[email protected]/include" \
LDFLAGS="-L/home/linuxbrew/.linuxbrew/etc/[email protected]/lib" \
pyenv install -v 3.10.0

Which ended up with the same error message.

Gotta say, I tried a lot of other stuff that I found in other places and nothing worked, but for the sake of simplicity I'm sticking with the official indications.

If you faced the same problem and the solution came from somewhere else than these commands, please share!

Many thanks :)

Update

<openssl install prefix> just refers to the base path or base folder, just that Homebrew decided to invent a new name because... well, some very important reason I guess.

This part gets solved by just replacing <openssl install prefix> by $(brew --prefix openssl).

See my own answer to the question below to see how it worked for me, but judging for all the different solutions that I've been seeing and trying, every case might need a different solution. Good luck!

Smedley answered 2/7, 2022 at 19:59 Comment(0)
S
20

About the doubt on what <openssl install prefix> is, I'll edit the question clarifying it.

About how to make pyenv install versions successfully, after trying everything I found about the topic that's the only thing that worked for me:

LDFLAGS="-Wl,-rpath,$(brew --prefix openssl)/lib" \
CPPFLAGS="-I$(brew --prefix openssl)/include" \
CONFIGURE_OPTS="--with-openssl=$(brew --prefix openssl)" \
pyenv install -v 3.9.5

For every new version, I use this command and it works.

Note that:

  • I tried to set these vars at startup, in a couple of different ways, and it didn't work by just doing pyenv install xx.xx, I always have to use the full command no matter what.
  • It won't install any 3.7.x version. I know in the docs there's a specific variation of the command setting a variable for the 3.7: not only I tried that and still doesn't work, but the variation that's suposed to work with 3.7, is what I needed to use in order to install the other versions. Luckily I could update the only project I had with 3.7 to a newer Python version so I don't need 3.7 anymore.

Not happy at all with all this mess but at least now I can use 22.04 for working.

Edit: I tried Jakob's suggestion to add it to .bashrc, like this:

export LDFLAGS="-Wl,-rpath,$(brew --prefix openssl)/lib"
export CPPFLAGS="-I$(brew --prefix openssl)/include"
export CONFIGURE_OPTS="--with-openssl=$(brew --prefix openssl)"

Then I did $SHELL to reload it, and then pyenv install xx.xx.xx worked fine.

Smedley answered 12/7, 2022 at 7:29 Comment(4)
does this work if you export the flags and conf in your .bashrc? – Trutko
Jakob I'm not sure, I remember trying it and not working but probably it was with different variables, next time I need to use I'll try again with the ones that proved to work for me. Thanks for the idea anyway! – Smedley
I fixed it by installing the python headers specific for my version python3.10-dev 😊 – Trutko
just tried and worked fine! I'm updating the answer.. – Smedley
F
19

Had same trouble, for me it worked after ran following command(check https://github.com/pyenv/pyenv/wiki#suggested-build-environment).

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

Could install without additional flags.

pyenv install -v 3.9.13
Formulary answered 9/7, 2022 at 3:39 Comment(4)
Many thanks for answering, I already tried that and didn't work at all. But i discovered a couple of things, I'm going to add the details... – Smedley
This worked fine for me on Ubuntu 22.04. I installed pyenv by cloning from repos, then addining the environment exports to .bashrc and that was it. – Bardo
This works for my Ubuntu 22.04 Linode – Assuage
This worked for me as well for my wsl Ubuntu 22.04. Many thanks. – Krissy
P
9

I managed to solve this error by installing libssl-dev on Ubuntu 22.04.1 LTS

   sudo apt install libssl-dev 
Pyxis answered 30/9, 2022 at 11:30 Comment(0)
E
5

Context

I had the same error from Pop!_OS 22.04 (based on Ubuntu 22.04), which I use Homebrew as one of the package managers for.

I could not get any of the answers above to work, nor any of the suggestions https://github.com/pyenv/pyenv/wiki/Common-build-problems to work.

Workaround

What worked for me was (temporarily) uninstalling the Homebrew openssl package, making sure openssl v3 was installed via apt-get, and then (optionally) reinstalling openssl via Homebrew.

  1. (temporarily) uninstall Homebrew openssl
    brew uninstall --ignore-dependencies [email protected]
    
  2. make sure openssl v3 is installed via apt-get
    sudo apt-get update
    sudo apt-get install openssl libssl-dev
    
  3. (optionally) reinstall Homebrew openssl
    brew install [email protected]
    

Note: This isn't a solution so much as a workaround. It's not for everyone, but hopefully it's helpful for someone beyond me.

Elbertina answered 10/11, 2022 at 1:37 Comment(2)
Exact same situation for me on Ubuntu 18. I didn't even bother re-installing [email protected] for brew (might break something else later), but at least the Python install worked with this solution. Thanks! – Ieshaieso
Glad this worked for you @TimKlein ! Even though you don't actively reïinstall [email protected], you may find the Homebrew does so the next time you run brew upgrade. – Elbertina
I
2

I struggled with the same issue for a whole afternoon, with none of the solutions above working, and with a little bit of digging, tracked the problem down to issues with the gcc that pyenv install uses. (See, e.g., here and here.)

In short, Python is being installed with Homebrew-managed build dependencies; however, pyenv install seems to use system gcc instead of Homebrew's gcc, which causes issues. A simple solution that worked for me is to point at Homebrew's gcc with pyenv install to let pyenv operate in the world it knows:

CC="$(brew --prefix gcc)/bin/gcc-12" \
pyenv install --verbose 3.9.7

Note: check the correct gcc version (gcc-11, gcc-12, etc.) from the directory $(brew --prefix gcc)/bin.

To avoid having to manually point to Homebrew's gcc every time running pyenv install, create an alias for Homebrew-managed pyenv installation:

alias pyenv-install-with-brew='CC="$(brew --prefix gcc)/bin/gcc-12" pyenv install'
Irrupt answered 7/1, 2023 at 11:16 Comment(0)
L
2

I also had this problem and it took unnecessarily long to solve it. So I hope this helps.

1- Make sure that you followed the steps in the build guide of pyenv for Ubuntu (https://github.com/pyenv/pyenv/wiki#suggested-build-environment) and installed all the necessary apt packages.

2- If the first item in where openssl shows a path to brew dirs. You might want to change this. You can simply uninstall openssl@3 and [email protected] via brew. Then it should use the apt installation of openssl by default.

I first tried to make it work with brew openssl installation but nothing really worked. Only after uninstalling openssl from brew it worked without any issues.

Loon answered 28/1, 2023 at 11:10 Comment(0)
M
2

I had your same exact issue. I am using linuxbrew on Ubuntu 20.04. None of the above answers worked for me, so I opted for keeping it simple and simply not using pyenv provided by brew

uninstall pyenv

brew uninstall pyenv

Install pyenv from source

git clone https://github.com/pyenv/pyenv.git $HOME/.pyenv
    
#install needed dependencies (without brew)
sudo apt-get install -y make 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


#setup path
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

now you can install

pyenv install 3.11.2

note: pyenv will stop working as soon as you open a new shell. You therefore need to force initialisation. Just update your ~/.profile or ~/.bash_profile ( in this case you need to logout after that) or update your ~/.bashrc

### Add pyenv if exists 
if [ -d "$HOME/.pyenv" ]; then
    #setup python
    export PYENV_ROOT="$HOME/.pyenv"
    command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init -)"
fi

Hope it works for the folks reading like it did for me. It took me longer than a day to figure out the best solution was simply avoid brew for pyenv. This solution is also elegant because it does not require you to add any alias or uninstall openssl from your brew.

Miseno answered 8/3, 2023 at 11:24 Comment(0)
F
1

on my host I have to do a brew install openssl, it install openssl@3 but brew --prefix openssl still links to openssl@1

I need to fully type out the path to build python 3.9.13. maybe there is a better way to do a prefix link.

LDFLAGS="-Wl,-rpath,/home/linuxbrew/.linuxbrew/opt/openssl@3/lib" \
CPPFLAGS="-I/home/linuxbrew/.linuxbrew/opt/openssl@3/include" \
CONFIGURE_OPTS="--with-openssl=/home/linuxbrew/.linuxbrew/opt/openssl@3" \
pyenv install -v 3.9.13
Flameproof answered 22/7, 2022 at 2:8 Comment(2)
In my case the --prefix returned the right one, so I could use the variable instead, but for sure this will be helpful for others, thank you! – Smedley
Currently (2022-11), the openssl formula is an alias for openssl@3. [email protected] is a different formula. You can specify which one to use with brew --prefix openssl@3 or brew --prefix [email protected]. – Fortieth
A
1
  1. wget http://www.openssl.org/source/openssl-3.0.7.tar.gz (because https://www.openssl.org/news/secadv/20221101.txt )
  2. tar -xzf openssl-3.0.7.tar.gz
  3. make && sudo make install
  4. pyenv install 3.x.x (all fine)
Athwart answered 23/12, 2022 at 8:6 Comment(0)
T
0

Just to create an answer as an option. The problem is most likely that the dev headers for python3.10 is not yet in python3-dev. Install python3.10-dev and you should be good to go.

Trutko answered 1/9, 2022 at 4:38 Comment(0)
C
0

This solved the issue for me after attempting several other options.

echo 'export PATH="/home/linuxbrew/.linuxbrew/opt/openssl@3/bin:$PATH"' >> /home/*user*/.bashrc
Charil answered 19/2, 2023 at 22:47 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.