Docker Community Forums

Share and learn in the Docker community.

Pyenv: command not found

HI Dockerers,

I am trying to install python3 inside my docker image using pyenv but it gives the following error:

$ pyenv: command not found

My docker file related to this issue is as follows:

RUN curl https://pyenv.run | bash
ENV PYTHON_CONFIGURE_OPTS --enable-shared
ENV LC_ALL C.UTF-8

RUN echo ‘export PATH="/root/.pyenv/bin:PATH"'>> /root/.bashrc &&\ echo 'eval "(pyenv init -)"’>> /root/.bashrc &&
echo ‘eval “$(pyenv virtualenv-init -)”’>> /root/.bashrc

RUN cd root && /bin/bash -c “source .bashrc” && /bin/bash -c “pyenv install 3.7.1”

But when I go inside the container I can directly install python using pyenv.
Does anybody know what did I miss in my docker file?

Cheers,
Maisam

Hi @maisammd,

Pls, can you show us the entire Dockerfile and how you run the container; or with which command/entrypoint start the container.

As far as I can see, you are setting up all to en environment for the shell bash; but if you never execute bash, this enviroment will never exist.

Hi @mgvazquez,

Thank you for your reply. I did not get your point about executing bash. However, my entire docker file is as follow:

ARG BASE_IMAGE=ubuntu:18.04
FROM $BASE_IMAGE

MAINTAINER Maisam Email:maisam.m.dadkan@gmail.com

Install dependencies

RUN apt-get update &&
apt-get install -y cmake cmake-data g++ gcc gfortran
debianutils build-essential make patch sed
libx11-dev libxft-dev libxext-dev libxpm-dev libxmu-dev
libglu1-mesa-dev libgl1-mesa-dev
libncurses5-dev curl libcurl4-openssl-dev bzip2 libbz2-dev gzip unzip tar
subversion git xutils-dev flex bison lsb-release python-dev
libc6-dev-i386 libxml2-dev wget libssl-dev libkrb5-dev
automake autoconf libtool zlib1g-dev
libreadline-dev libsqlite3-dev llvm
libncursesw5-dev xz-utils liblzma-dev python-openssl &&
rm -rf /var/lib/apt/lists/*

#install tk-dev separately to set time zone manually
RUN ln -fs /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y tk-dev && dpkg-reconfigure --frontend noninteractive tzdata && rm -rf /var/lib/apt/lists/*

#install pyenv

RUN curl https://pyenv.run | bash
ENV PYTHON_CONFIGURE_OPTS --enable-shared
ENV LC_ALL C.UTF-8

RUN echo ‘export PATH="/root/.pyenv/bin:PATH"'>> /root/.bashrc &&\ echo 'eval "(pyenv init -)"’>> /root/.bashrc &&
echo ‘eval “$(pyenv virtualenv-init -)”’>> /root/.bashrc

RUN cd root && /bin/bash -c “source .bashrc” && /bin/bash -c “pyenv install 3.7.1”

First thing first: .bashrc is automatically sourced only for interactive non-login shells (see this article). Since your shell is non-interactive, you need to source it manually.

You seemem to have realized this and attempted to account for that by doing:

RUN cd root && /bin/bash -c “source .bashrc” && /bin/bash -c “pyenv install 3.7.1”

The line above runs 3 shells: the parent shell that runs the entire command, a child shell that runs source .bashrc and a second child shell that runs pyenv install 3.7.1.

Do you see the problem? The child shell that attempts to run pyenv is not the same child shell that sourced .bashrc and therefore knows where pyenv is located.

The proper command to run a single shell that knows where pyenv is located and can execute it would be:

RUN source /root/.bashrc && pyenv install 3.7.1

or
RUN ["/bin/bash", “-c”, “source /root/.bashrc && pyenv install 3.7.1”]

Note that the first variant will use /bin/sh to run your command. In most modern Linux systems this is an alias to either /bin/bash or /bin/dash. If you want to be 100% sure that you’re running Bash, use the second form.

See also documentation for RUN directive and documentation for Dash.

1 Like