エックスサーバーにLinuxbrewでPythonをインストール

XSERVER(エックスサーバー)でPythonを動かしてみます。

XSERVERではデフォルトでPythonが利用できるよう用意されています。バージョンは(2.7.x / 3.4.x)です。

ただ、エックスサーバーは共有レンタルサーバーなので、管理者権限がありません。そのため、パッケージ管理ツールのpipが使えず少々使いづらい。

ということで、LinuxbrewでPythonをインストールしpipでパッケージ管理できるpython環境を構築してみました。

エックスサーバーにSSH接続

まずは、エックスサーバーにSSH接続でログインします。
SSHの設定、接続方法は公式サイトにマニュアルがあるので、ここでの説明は割愛します。

Linuxbrewのインストール

ログインできたらインストールしていきます。公式サイトの手順に従って以下コマンドを実行します。


$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"

実行すると下記のようにsudoのパスワードを入力するよう求められますが、権限がありません。こういう場合はユーザのホームディレクトリにインストールしてくれるようなので、何も入力せずEnterキーを押してパスワードを間違えてみます。すると以下のようにインストールが始まりました。

sudo: 3 回パスワード試行を間違えました
==> This script will install:

ずらずらーっと色々インストールしてくれ、以下が表示されたのでインストール成功したようです。

Already up-to-date.
==> Installation successful!

Linuxbrewの設定

インストールは成功したようですが以下のようにNext stepsと記載されています。この通りに設定します。

==> Next steps:
- Install the Linuxbrew dependencies if you have sudo access:
 Debian, Ubuntu, etc.
   sudo apt-get install build-essential
 Fedora, Red Hat, CentOS, etc.
   sudo yum groupinstall 'Development Tools'
 See http://linuxbrew.sh/#dependencies for more information.
- Configure Linuxbrew in your ~/.profile by running
   echo 'eval $(/home/XXXX/.linuxbrew/bin/brew shellenv)' >>~/.profile
- Add Linuxbrew to your PATH
   eval $(/home/XXXX/.linuxbrew/bin/brew shellenv)
- We recommend that you install GCC by running:
   brew install gcc
- Run `brew help` to get started
- Further documentation: 
   https://docs.brew.sh
Warning: /home/XXXX/.linuxbrew/bin is not in your PATH.

以下が実行したコマンドです。(XXXXの部分はユーザIDになります。)

$ echo 'eval $(/home/XXXX/.linuxbrew/bin/brew shellenv)' >>~/.profile
$ eval $(/home/XXXX/.linuxbrew/bin/brew shellenv)
$ brew install gcc
Warning: gcc 5.5.0_4 is already installed and up-to-date
To reinstall 5.5.0_4, run `brew reinstall gcc`

brewコマンドが使えるようになってるので、インストールされているんでしょう。問題がないかbrew doctorしてみます。

$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!
Warning: Your Homebrew's prefix is not /home/linuxbrew/.linuxbrew.
Some of Homebrew's bottles (binary packages) can only be used with the default
prefix (/home/linuxbrew/.linuxbrew).
You will encounter build failures with some formulae.
Please create pull requests instead of asking for help on Homebrew's GitHub,
Discourse, Twitter or IRC. You are responsible for resolving any issues you
experience, as you are running this unsupported configuration.

Warningは出ていますが、これは仕方がないのでOKとします。

Pythonのインストール

Linuxbrewがインストールできたので、次にPythonをインストールします。Python 3系で比較的新しいバージョンであれば良いかなと思ったので、マイナーバージョンは指定せず以下でコマンドでインストールしました。

$ brew install python3

ずらずら表示されて、以下にインストールしてくれたようです。

Python has been installed as
 /home/XXXX/.linuxbrew/bin/python3

それではPythonのバージョンを確認してみます。

$ python --version
Python 2.7.5

あれ?2系がインストールされてる??

$ python3 --version
Python 3.7.3

いや、エイリアスの設定の問題でしたね。python 3.7.3がインストールされたようです。Linuxbrewのインストール、LinuxbrewでPythonのインストールが完了しました。最後にシェルの設定ファイルを反映させました。

$ source ~/.profile

この記事が気に入ったらサポートをしてみませんか?